!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! SD360 - Spectral Dynamics NeXtMidas Macro
!
! @author Jeff Schoen
! @version $Id: remoteapp.mm,v 1.1 2006/10/23 20:17:04 jph Exp $
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
startmacro/msgid=main/remote=localhost:9000 d:samples s:format 

timer set
pipe init /panel /controls=gc

set sr 1e5
set over 0
set sfreq sr/10
set cfreq 0
set amp 1e4
set nexp 1

set l:pktsize 3

gc label macro "SD360"

if /server neqs "NULL"
  switch SERVER l:port get 9000

  waveform/throttle=real _wave format 10e10 sin freq=sfreq amp=amp delta=1/sr
  fft/dbf _wave _waveb{form=sb} nfft=1k over=over
  rmif/http port sfreq|_waveb /ppt={RETRY=2,TIMEPING=3} 

  gc button state "" "Run,Pause,Exit" 1
  gc dvalue sfreq "SineFreq" sfreq -sr sr sr/20 
  gc dvalue cfreq "CentFreq" cfreq -sr sr sr/20 
  gc choice test  "Test    " "Get,Set,Reset,Ping,Keys,Send,Recv,PktSize"
  gc dvalue linkbw  "Link BW " 0 0 128e3 1 /edit=0 /fmt="###.### ?by/s"
  gc lvalue linklat "Link Lat" 0 0 100000 1 /edit=0 /fmt="##### msec"

elseif /client neqs "NULL"
  switch CLIENT l:port get 9002
  env get "SERVER" remote

  rmif/http/prange=5 port 

  gc button state "" "Open,Close,Exit" 2
  gc dvalue sfreq "SineFreq" 0 -sr sr sr/20 
  gc dvalue cfreq "CentFreq" 0 -sr sr sr/20
  gc choice test  "Test    " "Get,Set,Reset,Ping,Keys,Send,Recv,PktSize"
  gc choice comp  "Compress" "Off,Data,Plot" 1
  gc dvalue linkbw  "Link BW " 0 0 128e3 1 /edit=0 /fmt="###.### ?by/s"
  gc lvalue linklat "Link Lat" 0 0 100000 1 /edit=0 /fmt="##### msec"

else	! local version

  waveform _wave format samples sin freq=sfreq amp=amp delta=1/sr
  fft/dbf _wave _waveb nfft=1k navg=1 over=over

  gc button state "Action  " "Run,Pause,Exit" 1 /toff
  gc choice shape "Shape   " reg.waveform.shapeList 1
  gc choice wind  "Window  " reg.fft.windowList 2
  gc dvalue sfreq "Sine Freq" sfreq -sr sr sr/20
  gc dvalue cfreq "Center Freq" cfreq -sr sr sr/20
  gc choice fftsz "FFT Size" "250,512,768,1K,2000,2K,4000,4K,8K" "1K"
  gc dvalue over  "Overlap " over 0 1 .1 /mon=reg.fft.over /msg=F
  gc lvalue nexp  "Average "  nexp 1 100 1 /mon=reg.fft.nexp /msg=F
  gc choice form  "Format  " "SF,CF,SI,CI" reg.waveform.format

endif

if /headless isfalse then
  plot/id=plot1/nice _waveb{cl=8} cnt=motion type=line y1=-100 y2=80 
  plot/id=plot2/nice _waveb cnt=click z1=-100 z2=80 /lpb=16
  feature ,, mark1 {TYPE=SYM|VRIDE,SYMBOL=CIRCLE,NAME=MARK,COLOR=YELLOW,ENABLE=1,X=SR/10} plot1
  gc label  debug "Debug" 0
  gc choice tests "Test    " "PopUp,Active,Procedure,Subroutine,SubThread,Files,Dialog"
  gc pipemon pmon _waveb
endif


set this.timers 1
set this.timer(0) 2
if /server le 0 and /client le 0 pipe waitfor waveform
pipe off

timer elapse

endmacro


procedure processMessage

!say "Msg ^msg.name = ^msg.data from ^msg.fid"

if msg.name eqs "TIMER" AND msg.info EQ 0 then
  if reg.rmif rexists  ! /server or /client
    set gc.LINKBW.value reg.rmif.linkBW
    set gc.LINKLAT.value reg.rmif.linkLatency
  endif

elseif msg.name eqs "STATE"
 if msg.data eqs "EXIT"
  pipe stop
 elseif msg.data eqs "PAUSE"
  pipe pause
 elseif msg.data eqs "RUN"
  pipe wait
 elseif msg.data eqs "OPEN"
  message sendw RMIF ,, "OPEN" ,, {ID=XM,HP=^remote} 
  message send XM ,, "ADDC" 1 "Q:ACK"
  message send XM ,, "ADDC" 2 {_WAVEB=_WAVEB}
 elseif msg.data eqs "CLOSE"
  message send XM ,, "CLOSE"  
 endif

elseif msg.name eqs "GET"
  ! fill in the table values
  foreach key inTable msg.data
    set msg.data.^key gc.^{key}.value
  endfor
  ! send back the results to the client
  message send msg.fid ,, "RET" ,, msg.data

elseif msg.name eqs "RET"
  ! fill in the table values
  foreach key inTable msg.data
    set gc.^{key}.value msg.data.^key
  endfor

elseif msg.name eqs "SET"
  ! loop through the table values
  foreach key inTable msg.data
    set gc.^{key}.action msg.data.^key
  endfor
  ! send back the results to the client
  message send msg.fid ,, "ACK" ,, msg.data

elseif msg.name eqs "ACK"
  ! loop through the table values
  foreach key inTable msg.data
    set gc.^{key}.value msg.data.^key
  endfor

elseif msg.name eqs "PROPERTY"
  say "Got Remote Property ^msg.data"

elseif msg.name eqs "TEST"
 if msg.data eqs "PING"
  message send XM ,, PING
 elseif msg.data eqs "GET"
  message send XM ,, "GET" ,, {SFREQ=,CFREQ=}
 elseif msg.data eqs "SET"
  message send XM ,, "SET" ,, {SFREQ=20e3,CFREQ=10e3}
 elseif msg.data eqs "RESET"
  message send XM ,, "SET" ,, {SFREQ=^sfreq,CFREQ=^cfreq}
 elseif msg.data eqs "KEYS"
  message send XM ,, "KEYS"
 elseif msg.data eqs "SEND"
  message send XM ,, "TESTLINK" pktsize
 elseif msg.data eqs "RECV"
  message send XM ,, "TESTLINK" -pktsize
 elseif msg.data eqs "PKTSIZE"
  gc/temp menu pktsize "PktSize" "2,4,8,16,32,64,128,256,512,1K,2K,4K,8K,16K,32K" pktsize
 endif

elseif msg.name eqs "TESTS"
 if msg.data eqs "POPUP"
  gc/wait prompt timeup "Time Up: " 
  gc/wait prompt duration "Duration: " 
 elseif msg.data eqs "ACTIVE"
  set gc.DEBUG.active 0
 elseif msg.data eqs "PROCEDURE"
  call testproc 1 2 3
 elseif msg.data eqs "SUBROUTINE"
  call testsub 1 2 3 msg
 elseif msg.data eqs "SUBTHREAD"
  call/bg testsub 4 5 6 msg
 elseif msg.data eqs "DIALOG"
  gc/temp/handle=dlog dialog "DIALOG" "Test Dialog Box" 300 -6
  gc/panel=dlog choice shape "Shape   " reg.waveform.shapeList reg.waveform.shape
  gc/panel=dlog button wind  "Window  " reg.fft.windowList reg.fft.window /tleft /nc=3
  gc/panel=dlog dvalue sfreq "SineFreq" sfreq -sr sr sr/20 
  gc/panel=dlog text   text  "TextInfo" "A test message" 30 3
  gc/panel=dlog menu   form  "Format  " "SF,CF,SI,CI" reg.waveform.format /tleft /nc=4
  gc/panel=dlog list   list  "TextList" "A,B,C,D,E,F,G,Another Long One" 30 3
 elseif msg.data eqs "FILES"
  !gc/temp "FILE" file
  gc file FILE "File " "abc"
 endif

elseif msg.name eqs "DIALOG"
  set reg.fft.window msg.data.wind.value
  set reg.waveform.shape msg.data.shape.value
  set reg.waveform.freq msg.data.sfreq.value
  set reg.waveform.format msg.data.form.value

elseif msg.name eqs "PKTSIZE"
  set l:pktsize ^msg.info

elseif msg.name eqs "WIND"
  set reg.fft.window msg.data

elseif msg.name eqs "SHAPE"
  set reg.waveform.shape msg.data

elseif msg.name eqs "FFTSZ"
  set reg.fft.nfft ^msg.data

elseif msg.name eqs "FORM"
  set reg.waveform.format msg.data

elseif msg.name eqs "SFREQ"
  if /client le 0 set reg.waveform.freq msg.data
  if /client gt 0 message send "XM" ,, "SET" ,, {SFREQ=^msg.data}
  if /server gt 0 message send "RMIF" ,, "ACK" ,, {SFREQ=^msg.data}

elseif msg.name eqs "CFREQ"
  ! nothing to do yet
  if /client gt 0 message send "XM" ,, "SET" ,, {CFREQ=^msg.data}
  if /server gt 0 message send "RMIF" ,, "ACK" ,, {CFREQ=^msg.data}

elseif msg.fid eqss "PLOT" then
  res fid msg.fid
  if fid eqs "PLOT1"
    res toid "PLOT2"
  else
    res toid "PLOT1"
  endif
  if msg.name eqs "POINTER"
    pause 0.1
    message prune ,, msg
    if fid eqs "PLOT1" then set msg.name "MARK"
  endif
  if msg.name eqs "MARK" then
    set mark1.x ^msg.data.x	 ! Update plot marker
    set gc.SFREQ.action msg.data.x
    if fid eqs "PLOT2" then set gc.CFREQ.action msg.data.x
  elseif msg.name eqs "ZOOM" then
    res msg.name "ZOOMX"
    message pass ^toid msg
  elseif msg.name eqs "UNZOOM" then
    message pass ^toid msg
  elseif "PANX" subs msg.name then
    res msg.name "PANX"
    message pass ^toid msg
  endif
  if msg.name neqs "MARK" and /client gt 0 and reg.xm.status eqs "OPENED" then
    set tmp reg.^fid
    message send XM ,, "MODIFY" ,, {PROPERTY=_WAVEB,TRIM1=^tmp.x1i,TRIM2=^tmp.x2i}
    say "Trimming to ^tmp.x1i ^tmp.x2i"
  endif

elseif msg.name eqs "COMP"
  set l:tmp1 gc.COMP.item-1
  set l:tmp2 reg.PLOT1.ix21
  info "Setting compression = ^tmp1 with plotwidth = ^tmp2"
  message send XM ,, "MODIFY" ,, {PROPERTY=_WAVEB,COMP=^tmp1,PLOTWIDTH=^tmp2}

elseif msg.name eqs "LINKERR" then
  say "LinkErr = ^msg.data"

elseif msg.name eqs "PROPERTY" then
  say "Got Remote property ^msg.data"

elseif msg.name eqs "OPENED" then
  message send XM ,, "GET" ,, {SFREQ=?,CFREQ=?}

elseif msg.name eqs "CLOSED" then

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

procedure init
!say "In init"
return

procedure open
!say "In open"
return

procedure close
!say "In close"
return

procedure testproc pa1 pa2 pa3
say "Called TESTPROC ^pa1 ^pa2 ^pa3"
res
return

subroutine testsub l:pa1 l:pa2 l:pa3 o:pa4
say "Called TESTSUB ^pa1 ^pa2 ^pa3"
res
gc/wait prompt duration1 "Duration1: " 
say "Called TESTSUB back from Popup1"
gc/wait prompt duration2 "Duration2: " 
say "Called TESTSUB back from Popup2"
return
