startmacro/np=24*3+1/nw=24*10+4/quiet/timeout=100/ds/nmq=1k &
  a:device[PIC1] n:side[3] n:nfft[2k] n:ndec[256] n:ntun[-1] &
  n:samplerate[4*10e6] n:freq[16*78000]

local nyquist ramqual nn ii bank flg plotmax plotmin format filesize ioff
local px py pw ph ix iy iw ih osamplerate switches
local pdec cp i2n n1 n2 n3 osamples item 
local sample time1 time2 timecode gain

env set pause 0.01

picd query ^device "CPC" cp
if /cpc gt 0 res cp /cpc
if /tct gt 0 res side 1

if ntun lt 0
  picd/flags=cpc=^cp get ^device "CHNS" ntun
  if side ne 3 calc ntun ntun/2
endif

res nyquist samplerate/2

res pw 180
res ph 150
res px 250
res py 50
calc iw pw-10
calc ih ph-30

! create test input file
if ramaux rexists then
  res ramqual "(CTG=1)(AUX=RAMAUX)(DET=1)"
else
  res ramqual "(CTG=1)"
endif

if /c gt 0
  res format "CI"
  calc nfft nfft/2
  calc osamplerate samplerate/ndec/2
else
  res format "SI"
  calc osamplerate samplerate/ndec
endif

! create test output file
calc filesize 0.5*osamplerate nfft*2 max 16k / round 1 max 16k *
res osamplerate
pic create testout ^format ntun*^filesize osamplerate
status testout

calc pdec ntun 256/ndec * round 1 max power2

! start test waveform playback
if /live gt 0 then
  picd reset ^device iix
  res switches "flags=cpc=^cp|muxclk=i|inp=2"
  res plotmax " "
  res plotmin " "
elseif /tct gt 0
  picd reset ^device t1
  res switches "flags=iom=test|cpc=^cp"
  res plotmax "60"
  res plotmin "-60"
  res pdec 1
else
  picd reset ^device iir
  res switches "flags=muxclk=i|cpc=^cp"
  res plotmax "60"
  res plotmin "-60"
endif

if /ovsr gt 0
  picd ovsr ^device ,, /ovsr -1
endif

calc ioff /chnoff 1 max
if side eq 2 calc ioff ioff 2 max
if side ne 3 concat switches "/AUTOSS" switches

xpipe/setup/controls on

control/wb=1 dval "Freq: "
 
sourcepic/id=10/wb=2/port=tuner^ioff/^switches/packet/tl=nfft/multi=ntun/renum &
		testout _cbm(ps=1m) ^device ndec freq

picfunc/afn=ntun UNPACK file _cbm _cb

loop ntun nn
  calc ix nn-1 4 mod pw * px +
  calc iy nn-1 4 / fix ph * py +
  mfft/s=d _cb^nn(fs=0) _cbf^nn nfft HANN -4
  xrtplot/s=d/xl=ix/xt=iy/xw=iw/xh=ih/dbrange=100&
	/id=100+nn/mtag=x:w_1001/xn="Plot-^nn  Tuner-^ii" &
	_cbf^nn ,^plotmin,^plotmax,d2
  if /tofile gt 0 noop _cb^nn tmp_cb^nn
endloop

if /tct gt 0
  picd/replay=2/flags=muxclk=i/port=module2 play ^device tctest
endif

xpipe off

picd reset ^device 

! remove test output file
erase testout

endmacro

startcontrols

label initialentry
xcontrol/lab=xxx menu item 0 "Macro;Exit"
xcontrol/name="Plot: "/lab=xchan lval nn 0 0 ntun 1
xcontrol/lab=plotx dmon w_1001

goto xchan

label plotx
xcall wgetid pid 1001
calc nn pid-100 

label xchan
callp nn2ii
xcall setc nn nn
xcontrol/over=4/lab=freqx/name="TFreq^ii: " dval freq freq 0 nyquist nyquist*1e-5 
xcontrol/over=5/lab=gainx/name="TGain^ii: "/len=6 lval gain 0 -100 100 1 
xcontrol/over=6/lab=menux menu item 0 "Plot ^nn;Start,Stop,Chan,TCode"
xcontrol/over=7/name="TDec^ii: "/len=6/lab=decx lval ndec ndec 2 8192 2 
return

label freqx
message send "FREQ" 10 ii 1 "D" freq
return

label gainx
message send "GAIN" 10 ii 1 "D" gain
return

label decx
! change decimation on all channels
message send "DEC" 10 -1 1 "D" ndec
return

label menux
if item eq 1 then
  res w_2001 2
elseif item eq 2 then
  res w_2001 4
elseif item eq 3 then
elseif item eq 4 then
  message send "TC" nn+10 ,, 1 "D" -1
  message getwn "=TC" ,,,,, sample time1 time2
  timex time1|time2 tx:timecode
  say "TC = ^timecode at Sample ^sample"
endif
return

label xxx
stop
return

procedure nn2ii
res ii nn
return

endcontrols
