! Test start time on PLOT when connecting to an existing PIPE (time should be
! when PLOT connects to pipe, not the pipe's initial start time (i.e. if n seconds
! elapsed before PLOT opens the pipe, then time at top of plot should be
! initial pipe time + n seconds.
!
! BUG   :  2265 PLOT: time off, when connecting to existing PIPE on real-time raster PLOT
! Author:  Neon Ngo
!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

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

  if interAct AND panelExists then
    set format sb
    set sr 1e4
    set over 0
    set sfreq sr/10
    set amp 1e4
    set maxTime 1e38
    res waitTime 5.0
    timex now curTime
    calc expectedStartTime curTime waitTime +
    invoke expTime nxm.sys.lib.Time.add(curTime,waitTime)
    set instructions "In ^waitTime seconds, the PLOT will open the real-time pipe.\n\n&
                      Time at top edge of the plot should be ^expTime.\n&
                      (^waitTime seconds after ^curTime)\n\n&
                      You can also click top of PLOT to capture time at clicked position to compare against expected time."
    pipe init
      panel/msgid=MAIN/setup/controls=gc

      waveform/id=wave/throttle=real _wave format 1e9 sin freq=sfreq amp=amp delta=1/sr
      fft/dbf _wave _waveb{form=sf} nfft=1k over=over
      plot/id=idplot/msgid=MAIN/READOUT=Show|OnAxis|T|Y

      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 14 /title

      gcontrol label  plot   "PLOT TIME/PIPE"
      gcontrol tval   expTime   "Expected Time  " expectedStartTime 0 maxTime /flags="NoMsg|NoEdit"
      gcontrol tval   timeAtPos "Time at clicked" 0 0 maxTime /flags="NoMsg|NoEdit"

      gcontrol label  debug   "DEBUG"
      gcontrol button plotPipe "PLOT PIPE" "OPEN,CLOSE" "CLOSE" /tleft
      gcontrol button pipeState "Pipe" "Run,Pause" 1 /tleft
      gcontrol pipemon pmon1 _wave
      gcontrol pipemon pmon2 _waveb

      set this.timer(0) waitTime
    pipe off
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg

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

  elseif msg.name eqs "PLOTPIPE"
    if msg.data eqs "OPEN"
      messag send idplot ,, "OPENFILE" -1 _waveb
      timex now mostCurTime
      res gc.expTime.value mostCurTime

    elseif msg.data eqs "CLOSE"
      messag send idplot ,, "CLOSEFILE" ,, "*"
    endif

  elseif msg.name eqs "PIPESTATE"
    if msg.data eqs "PAUSE"
      pipe pause
    elseif msg.data eqs "RUN"
      pipe wait
    endif

  elseif msg.name eqs "TIMER" then
    set this.timer(0) 0
    messag send idplot ,, "OPENFILE" -1 _waveb
    res gc.plotpipe.value "OPEN"
    timex now mostCurTime
    res gc.expTime.value mostCurTime

  elseif msg.name eqs "MARK" then
    res gc.timeAtPos.value msg.data.time

  else
    say "Unhandled message name=^msg.name info=^msg.info fid=^msg.fid data=^msg.data"
  endif

return
