!******************************************************************************!
! Jeff needs the ability to offset the readout and axis for the operators
! while an external XCVR is scanning from 1 to 18 GHz. To do this by changing
! the PSD pipe xstart and an openfile message unfortunately blanks the raster.
! The <scanning updates> are happening more than once per second and the operator
! needs to see all of the raster history. As long as nobody sets the XOff to a
! non-zero value, the code is identical, so should be safe. If you want to support
! a YOff, the member is there, but Jeff does not have a need or test case for that.
!
! @bug      <JIRA_ISSUE> <brief_issue_title>
! @since    <NXM-VERSION>
! @author:  First Last Name / Additional Author
! Author:  Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE     baseline     GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests

  if interAct then
    call setDatDir

    set samples 1e9
    set format sf
    set sr 1e5
    calc delta 1 sr /
    set over 0
    set sfreq1 sr/5
    set sfreq2 sr/10
    set sfreq3 sr/20
    res sfreq sfreq1
    set elem  inf
    res elem1 64k
    timex "2020:01:00::11:22:33" sec=xs1
    res xs1
    calc xs2   elem1 delta * xs1 + 
    res xs2
    calc xs3   elem1 1 + delta * xs2 +
    res xs3
    set amp 1e4
    set nexp 1
    res z:mytrue true
    set instructions "click 'UPDATE READOUT'\n\n&
                      Left Plot updates XOff to 'adjust' freq\n&
                      Right Plot does actual freq update\n\n&
                      frequencies are:\n &
                      start=^sfreq\n freq1=^sfreq1\n freq2=^sfreq2\n freq3=^sfreq3"

    pipe init
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label  macro   "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}

      gcontrol text idtext "Instructions" "^instructions" 24 12 /title

      waveform/id=wave1/throttle=real _wave1 format elem=elem sin freq=sfreq amp=amp delta=1/sr start=xs1
      status _wave1
      fft/id=fft1/dbf _wave1 _wave1b{form=sb} nfft=4k over=over
      waveform/id=wave2/throttle=real _wave2 format elem=elem sin freq=sfreq amp=amp delta=1/sr start=xs1
      fft/id=fft2/dbf _wave2 _wave2b{form=sb} nfft=4k over=over

      plot/id=idplot1/nice _wave1b /readout=+OnAxis|+DateString view=iyx
      plot/id=idplot2/nice _wave2b /readout=+OnAxis|+DateString view=iyx

      gcontrol choice newreadout "UPDATE frequency/READOUT" "^sfreq1,^sfreq2,^sfreq3" "^sfreq" /input
    pipe off
  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg
  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name " msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "NEWREADOUT" then

     res d:thisfreq msg.data

     ! invoke ,, reg.wave1.freq(thisfreq)  ! not doing this right now
     invoke ,, reg.wave2.freq(thisfreq)

     calc freqdiff thisfreq sfreq -
     res freqdiff
     invoke ,, reg.idplot1.setXoff(freqdiff)

  else
    ! do nothing
    say "Unhandled message name: ^msg.name from fid: ^msg.fid dats: ^msg.data"
  endif

return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
