startmacro/nmq=8k/ps=128k/rmifid=2 N:nsamples L:port A:remote
!
! macro for emulating SD350 spectrum analyzer
!
! @author Jeff Schoen
! @version $Id: sd360.txt,v 1.21 2008/05/07 17:11:09 ntn Exp $
!
local SFREQ,CFREQ,FWIDTH,FS,form,nn
local /M/

if /c gt 0
  res form "CF"
else
  res form "SF"
endif
res cfreq 0.0
res fwidth 0.1

timer set

if /server gt 0
  xpipe/setup/controls on
  WAVEFORM/tl=1k/rt/ctag=3:SFREQ _wave ^form 1e9 SIN 1e4 1e4,,, 1/1e5
  MFFT/rs/fftp=3 _wave _wavep 1k hann 0.0
  STATUS/quiet _wavep ,,,,, FS
  magnitude/tl=fs _wavep _waveb(ft=b) 20 log
  rmif/id=2/pktmsg/msgid=1 port sfreq|cfreq|fwidth|_waveb
  xpipe off

elseif /client gt 0
  xpipe/setup/controls on
  rmif/id=2/keymsg/msgid=1 port ,, remote _waveb
  STATUS/quiet _waveb ,,,,, FS
  XRTPLOT/xs=3/mtag=X:SFREQ/xcnt=2/mark=3 _waveb ,,,,, 70
  XRTRASTER/xs=2/mtag=X:CFREQ/lpb=2/stretch _waveb ,,, fs 20
  XRTDISPLAY/xs=4 SFREQ/"Sine Frequency" FWIDTH/"Filter Width"
  xpipe off

else
  if /nmplot gt 0 then
    nm/on/bg/id=999
  endif
  if /nmwin gt 0 then
    nm/on/bg/id=999
    nm look java
  endif
  xpipe/setup on
    if /nmwin gt 0 then
      nm panel/jsetup="nxm.sys.libxm.sd360xm"
    endif
    WAVEFORM/tl=1k/ctag=3:SFREQ _wave ^form nsamples SIN 1e4 1e4,,, 1/1e5
    MFFT/fftp=3 _wave _wavep 1k hann 0.0
    STATUS/quiet _wavep ,,,,, FS
    if /nox le 0
    XRTPLOT/xs=3/mtag=X:SFREQ/xcnt=2/mark=3 _wavep(cl=8*fs) 1e-5 1e4 l2 ,, 70
    if /nmplot gt 0 then
      nm/xs=2 plot/theme=WoB xmpipe:_wavep cnt=click Z1=-100 Z2=80 CM=20LOG /lpb=16
    else
      XRTRASTER/xs=2/mtag=X:CFREQ/lpb=2/stretch _wavep 1e-5 1e4 l2 fs 20
    endif
    XRTDISPLAY/xs=4 SFREQ/"Sine Frequency" FWIDTH/"Filter Width"
    endif
  xpipe off
  if /nmplot gt 0 then
    nm/off
  endif
endif

timer elapse
endmacro

startcontrols

label initialentry
xcontrol/lab=domenu menu item ,, "MACRO;Run,Pause,Ping,Keys,Get,Set,Reset,Test,Stop"
xcontrol/lab=setsfreq monitor sfreq
xcontrol/lab=setcfreq monitor cfreq
xcontrol/lab=getmsg msmon 1 10
return

label domenu
if item eq 3
  ! ping test ?
elseif item eq 4
  message send "KEYS" 2 1
elseif item eq 5
  rmsg send ^remote "GET" ,, "{SFREQ=?,CFREQ=?}"
elseif item eq 6
  rmsg send ^remote "SET" ,, "{SFREQ=20E3,CFREQ=10E3}"
elseif item eq 7
  rmsg send ^remote "SET" ,, "{SFREQ=10E3,CFREQ=10E3}"
elseif item eq 8
  ! other test
elseif item eq 9
  stop
endif
return

label setsfreq
if /client gt 0
 rmsg send ^remote "SET" ,, "{SFREQ=^SFREQ}"
endif
return

label setcfreq
if /client gt 0
 rmsg send ^remote "SET" ,, "{SFREQ=^CFREQ+.001,CFREQ=^CFREQ}"
endif
return

label getmsg
message get /M/
if M.name eqs "RPKT"
  if M.RPKT_func eqs "SET"
    rmsg "T2R" ,,,, M.RPKT_data d_
    loop d_nk nn   ! loop through table entries
      res P_^{d_k^nn} ^{d_v^nn}
    endloop
    rmsg "SEND" M.RPKT_addr "ACK" M.info ,, d_
  else if M.RPKT_func eqs "GET"
    rmsg "T2R" ,,,, M.RPKT_data d_
    loop d_nk nn   ! loop through table entries
      res  d_v^nn P_^{d_k^nn}
    endloop
    rmsg "SEND" M.RPKT_addr "RET" M.info ,, d_
  else if M.RPKT_func eqs "ACK"
    rmsg "T2R" ,,,, M.RPKT_data d_
    say "Got ACK = ^M.RPKT_data"
    res d_*
  else if M.RPKT_func eqs "RET"
    rmsg "T2R" ,,,, M.RPKT_data d_
    say "Got RET = ^M.RPKT_data"
    res d_*
  endif
  rem/quiet d_*
else if M.name eqs "GETKEY"
  say "GetKey a ^M.GETKEY_name ^M.GETKEY_value
elseif M.name eqs "RETKEY"
  say "RetKey a ^M.RETKEY_name ^M.RETKEY_value
elseif M.name eqs "SETKEY"
  say "SetKey a ^M.SETKEY_name ^M.SETKEY_value
elseif M.name eqs "ACKKEY"
  say "AckKey a ^M.ACKKEY_name ^M.ACKKEY_value
elseif M.name eqs "RERR"
  say "Link Err: ^M.RERR_text for ^M.RERR_func ^M.RERR_data from ^M.RERR_addr"
else
  say "Got a ^M.name message"
endif
return

endcontrols
