!******************************************************************************!
! A test that imitates portions of snapapp and snapper and should be adjusted
! with time to, within limits, better reflect Jeff's usage
!
! Arguments: throttle
!
! @bug      NXM-3821 Jeff's snapper plots were coming up zoomed
! @since    NeXtMidas 4.1.1
! @author:  Jeanette McCall
!******************************************************************************!
startmacro S:throttle["REAL"]
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1

  env get IOOptions origIOOptions

  ! Only allow interAct if PANEL command Exists - may be running in LIBNXM mode
  invoke panelExists reg.shell.M.dictionary.isDictionaryEntry(PANEL)

  if (interAct or coreGraphics) and panelExists then
    set instructions "This is designed to someone imitate Jeff's snapapp and snapper\n\n&
                      the frequency domain plots should render properly and not come up zoomed &
                      to the point you do not see a line rather than a frequency plot"

    call setDatDir

    set format sf
    set sr 4e3
    set over 0
    set sfreq sr/10
    set curfreq sfreq
    set amp 1e2
    set noiseamp 1e2
    set fsize 512
    set numelem inf
    calc tdlp_range sr 4e3 / ! this ratio works ...
    set firstClick true
    set dualSignals BOTH ! initPanel counts on this being default
    set panelDisplay TFR ! initPanel counts on this being default

    res fm_file  "^{datDir}.fm_capture_t1000.prm"

    set fmwave_plot_time 0.515 ! should figure out why this works best

    set setup_tbl_file_prefix "nxm.sys.test.test_snapper_"

    invoke tcur nxm.sys.lib.Time.current()

    pipe init

      call pipeaction ! all the waveform / fft  action
      call initPanel
      call setGControls

    pipe off
  endif

  env set IOOptions origIOOptions ! restore original IOOptions
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "WARM_START" then
    message send id=fdrp_wave name="OPENFILE" info=-1 data=_fftw
    message send id=fdrp_fm name="OPENFILE" info=-1 data=_fftfm
  elseif msg.name eqs "DUALSIG" then
    res dualSignals msg.data
    call closePlots
    call updatePanel
  elseif msg.name eqs "DISPLAY" then
    res panelDisplay msg.data
    info "Updating display to ^panelDisplay"
    call closePlots
    call updatePanel
  elseif msg.name eqs "PIPE_INIT" then
    info "TOFIX: Multiple PIPE INITs cause Waveform Time Plot to be lost until the plots (i.e.panel layout)"
    call shutdownSignals
    pause 0.5
    invoke tmptime nxm.sys.lib.Time.current()
    calc tcur ^tmptime 7 - ! go back this many seconds
    pipe init
      call pipeaction
    pipe run
  elseif msg.name eqs "MFREQ" then
    ! set frequency of both waveforms
    calc curFreq sfreq ^msg.data *
    set reg.wave.freq  ^curFreq
    set reg.wave.state "RESTART"
  elseif msg.name eqs "FRAMESZ" then ! since 3.7.2 for NXM-3040
    ! set framse size of both ffts
    res fsize msg.data
    set reg.fftw.nfft fsize
    set reg.fftfm.nfft fsize
  elseif msg.name eqs "UNZOOM"
  elseif msg.name eqs "ZOOM"
  else
    say "Unhandled message name=^msg.name info=^msg.info fid=^msg.fid data=^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

!***********************************************************************
! put the real action in the pipe in here
! so we can simulate Jeff's mode change
!***********************************************************************
procedure pipeaction
  ! Create waveform with adjustable frequency
  waveform/id=wave/throttle=^throttle _wave format elem=numelem cos freq=curfreq amp=amp delta=1/sr start=^tcur
  ! file containing capture of fm data
  noop/rt/wrap/id=fm fm_file _fmwave{ps=32k}
  ! Create ffts of the 2 waveforms (frame size can be adjusted using gcontrol)
  fft/dbf/id=fftw _wave _fftw{form=sf} fsize over=over
  fft/psd/log/id=fftfm _fmwave _fftfm fsize over=over
return

!***************************************************************************
! initial panel setup - 2 signals with Time, Frequency Line, and Raster Plots
!***************************************************************************
procedure initPanel
  call selectSetupTable 
  panel/tsetup=tsetup/menubar/controls=gc/logger
  call initPlots
return

!***************************************************************************
! initial plot setup - up to 2 signals with Time, Frequency Line, and Raster Plots
!***************************************************************************
procedure initPlots
  call updateActivePlots
  plot/id=tdlp_wave/msgid=MAIN _wave
  if fdlp_wave_active then plot/id=fdlp_wave/msgid=MAIN _fftw type=line
  if fdrp_wave_active then plot/id=fdrp_wave/msgid=MAIN _fftw type=raster view=iXY
  if tdlp_fm_active then plot/id=tdlp_fm/msgid=MAIN _fmwave
  if fdlp_fm_active then plot/id=fdlp_fm/msgid=MAIN _fftfm type=line
  if fdrp_fm_active then plot/id=fdrp_fm/msgid=MAIN _fftfm type=raster view=iXY
  pause 0.2
  call setTdlpLimits
return

!***************************************************************************
! load tsetup with appropriate panel setup table
!***************************************************************************
procedure selectSetupTable
  if dualSignals EQS "BOTH" then
    if panelDisplay eqs "TFR" then
      res setupfile "^{setup_tbl_file_prefix}2tfr.tbl"
    elseif panelDisplay eqs "TR" then
      res setupfile "^{setup_tbl_file_prefix}2tr.tbl"
    else
      res setupfile "^{setup_tbl_file_prefix}2tf.tbl"
    endif
  else
    if panelDisplay eqs "TFR" then
      res setupfile "^{setup_tbl_file_prefix}1tfr.tbl"
    elseif panelDisplay eqs "TR" then
      res setupfile "^{setup_tbl_file_prefix}1tr.tbl"
    else
      res setupfile "^{setup_tbl_file_prefix}1tf.tbl"
    endif
  endif
  table tsetup load setupfile
return

!***************************************************************************
! update panel setup
!***************************************************************************
procedure updatePanel
  sendto/sync panel "PANEL.CFG" DATA="CLEAR"
  pause 0.2
  call updateActivePlots
  call selectSetupTable

  ! configure the panel with setup table
  sendto/sync panel "LOADSETUPTABLE" DATA=tsetup

  ! send OPENFILE messages
  message FUNC=send ID=TDLP_WAVE NAME=OPENFILE INFO=-1 DATA=_wave
  if fdlp_wave_active then message FUNC=send ID=FDLP_WAVE NAME=OPENFILE INFO=-1 DATA=_fftw QUALS={TYPE=line}
  if fdrp_wave_active then message FUNC=send ID=FDRP_WAVE NAME=OPENFILE INFO=-1 DATA=_fftw QUALS={TYPE=raster}
  if tdlp_fm_active then message FUNC=send ID=TDLP_FM NAME=OPENFILE INFO=-1 DATA=_fmwave
  if fdlp_fm_active then message FUNC=send ID=FDLP_FM NAME=OPENFILE INFO=-1 DATA=_fftfm QUALS={TYPE=line}
  if fdrp_fm_active then message FUNC=send ID=FDRP_FM NAME=OPENFILE INFO=-1 DATA=_fftfm QUALS={TYPE=raster}
  pause 0.2

  ! attach plots to the panel
  call attachPlots

  pause 0.2
  call setTdlpLimits

return

!***************************************************************************
! update plots to panel
!***************************************************************************
procedure attachPlots
  call updateActivePlots
  invoke ,, reg.win.TDLP_WAVE.attach(reg.TDLP_WAVE,"TDLP_WAVE")
  if fdlp_wave_active then invoke ,, reg.win.FDLP_WAVE.attach(reg.FDLP_WAVE,"FDLP_WAVE")
  if fdrp_wave_active then invoke ,, reg.win.FDRP_WAVE.attach(reg.FDRP_WAVE,"FDRP_WAVE")
  if tdlp_fm_active then invoke ,, reg.win.TDLP_FM.attach(reg.TDLP_FM,"TDLP_FM")
  if fdlp_fm_active then invoke ,, reg.win.FDLP_FM.attach(reg.FDLP_FM,"FDLP_FM")
  if fdrp_fm_active then invoke ,, reg.win.FDRP_FM.attach(reg.FDRP_FM,"FDRP_FM")
  invoke ,, reg.win.LOGGER.attach(reg.LOGGER,"LOGGER")
  
return

!***************************************************************************
! GControls just need to be set up once
!***************************************************************************
procedure setGcontrols
  gcontrol label  macro   "INSTRUCTIONS"
  gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
  set gc.main.itemColors {PASS=GREEN,FAIL=RED}
  gcontrol text idtext "Instructions" "^instructions" 20 30 /title
  gcontrol label  plotsel  "PLOT SELECTION"
  gcontrol choice dualsig "Signals" "ONE,BOTH" "BOTH" /tleft
  gcontrol choice display "Plots (T)ime (F)req (R)aster:" &
                          "TFR,TF,TR" "TFR"
  gcontrol label  plotsel  "DSP CONTROLS"
  gcontrol choice mfreq "Set FREQ (multiplier):" ".25,.5,1,2,3,4" "1" /tleft
  gcontrol choice framesz "Set FFT FRAME Size:" "256,512,1k" "512" /tleft
  gcontrol label  plotsel  "PIPE RESTARTS"
  gcontrol button "WARM_START" "" "WARM_START"
!  gcontrol button "PIPE_INIT" "" "PIPE_INIT" ! disabled since can cause issues
return

!***************************************************************************
! remove registry entries that are plots
!***************************************************************************
procedure removePlots
  rem reg.tdlp_wave
  rem reg.tdlp_fm
  rem reg.fdlp_wave
  rem reg.fdlp_fm
  rem reg.fdrp_wave
  rem reg.fdrp_fm
return

!***************************************************************************
! shutdown signals
!***************************************************************************
procedure shutdownSignals
  invoke ,, reg.fftfm.close()
  invoke ,, reg.fftw.close()
  invoke ,, reg.fm.close()
  invoke ,, reg.wave.close()
  rem reg.fftfm
  rem reg.fftw
  rem reg.fm
  rem reg.wave
return

!***************************************************************************
! close all open plots (associated with a PANE)
!***************************************************************************
procedure closePlots 
  invoke tpanes reg.panel.getPanes()
  foreach ppane INTABLE tpanes
    if ppane NEQS "PANE0" then
      message FUNC=send ID=^PPANE NAME=CLOSEFILE DATA="*"
    endif
  endfor
return

!***************************************************************************
! calculate time domain plot X2 bound
!***************************************************************************
procedure setTdlpLimits
  res tldp_wave_orx1 reg.tdlp_wave.orx1
  calc new_orx2 tldp_wave_orx1 tdlp_range +
  set reg.tdlp_wave.setOuterLimits("X2") new_orx2
  if dualSignals EQS "BOTH" then
    res tldp_fm_orx1 reg.tdlp_fm.orx1
    calc new_orx2 tldp_fm_orx1 fmwave_plot_time +
    set reg.tdlp_fm.setOuterLimits("X2") new_orx2
  endif
return

!***************************************************************************
! update with plots the user has chosen to be active
!***************************************************************************
procedure updateActivePlots
  res fdlp_wave_active false
  res fdrp_wave_active false
  res tdlp_fm_active false
  res fdlp_fm_active false
  res fdrp_fm_active false
  if panelDisplay EQS "TFR" OR panelDisplay EQS "TF" then res fdlp_wave_active true
  if panelDisplay EQS "TFR" OR panelDisplay EQS "TR" then res fdrp_wave_active true
  if dualSignals EQS "BOTH" then
    res tdlp_fm_active true
    res fdlp_fm_active fdlp_wave_active
    res fdrp_fm_active fdrp_wave_active
  endif
return