startmacro/np=34*3+1/nw=34*14+4/quiet/ds/nmq=1k &
  a:device[PIC1] n:side[3] n:nfft[1k] n:ndec[256] n:ntun[-1] &
  n:samplerate[4*10e6] n:freq[-116*78000] a:sclk[I]

local nyquist n nn ii bank flg plotmax plotmin format filesize ioff
local px py pw ph ix iy iw ih osamplerate switches
local ww wwp1 wpc pdec cp i2n n1 n2 n3 osamples item n2i
local sample time1 time2 timecode tstat ipkt npkt aflags

env set pause 0.01
res wpc 9	! widgets per channel sourcepic+mfft
switch "AFLAGS" u:aflags "GET"

picd/flags=^aflags query ^device "CPC" cp
picd/flags=^aflags query ^device "CTYPE" ctype
picd/flags=^aflags query ^device "GCHIP" gtype

if cp eq 1 res n2i (1,2,9,10,17,18,25,26)
if cp eq 2 res n2i (1,3,2,4,9,11,10,12,17,19,18,20,25,27,26,28)
if cp eq 4 res n2i (1,3,5,7,2,4,6,8,9,11,13,15,10,12,14,16,17,19,21,23,18,20,22,24,25,27,29,31,26,28,30,32)

picd/flags=^aflags query ^device "MCHNS" ii
if ii eq 4 res n2i (1,2,3,4) 
if ii eq 0 then
  warning "No tuners available in this configuration"
  stop
endif

if ntun lt 0
  picd/flags=^aflags query ^device "CHNS" ntun
  if side ne 3 calc ntun ntun/2
  calc l:ntun ntun 24 min
endif

calc pdec ntun 256/ndec * round 1 max power2

! start test waveform playback
if /LIVE gt 0 then
  picd/flags=^aflags reset ^device iix
  res switches "flags=muxclk=^sclk"
  res plotmax " "
  res plotmin " "
elseif /TCX gt 0
  picd/flags=^aflags reset ^device 
  res switches "tcpp/flags="
  res plotmax "75"
  res plotmin "-60"
  res pdec 1
else
  res aflags "^aflags|ioc=iir"
  picd/flags=^aflags reset ^device
  if /hs gt 0
    res switches "flags=muxclk=c"
    picd get ^device "CCLK" samplerate
  elseif /polar gt 0
    res switches "flags=muxclk=^sclk|polar"
  else
    res switches "flags=muxclk=^sclk"
  endif
  res plotmax "75"
  res plotmin "-60"
endif



if freq le 0
  calc freq samplerate 16*78000/40e6 *
endif

if /itdec gt 0
  concat switches "|ITDEC" switches
endif

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

concat switches "|^aflags" switches

res nyquist samplerate/2

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

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
if ndec lt 2k 
  res ipkt 8k
else
  res ipkt 2k
endif

calc ioff /chnoff 0 max
calc filesize 2.5*osamplerate nfft*2 max 64k max round

xpipe/setup/controls/timeout=600 on

control/wb=1 dval "Freq: "
 
loop ntun nn
  callp nn2ii
  calc ix nn-1 4 mod 1.5 - 0.103*osamplerate *
  if /meas gt 0 calc ix 0
  pic create testout^nn ^format ^filesize osamplerate
  if /mux gt 0
    sourcepic/id=10+nn/port=tuner^ii/^switches/tl=ipkt/packet/timeout=5/tcpp &
	testout^nn _cbp^nn ^device ndec freq+ix
  else
    sourcepic/id=10+nn/port=tuner^ii/^switches/skip=pdec/tl=1 &
	testout^nn(fs=nfft) _cb^nn ^device ndec freq+ix
  endif
endloop

if /mux gt 0
  picfunc/afn=ntun/renum PACK file _cbx(ps=1m) _cbp
  calc npkt 16K ipkt / round 1 + 5 max
  picfunc/afn=ntun UNPACK file _cbx _cb
endif

if /nox le 0
loop ntun nn
  callp nn2ii
  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 0
  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
endloop
endif

if ctype eq 5 or ctype eq 6 then
  res wbfmt "SI"
else
  res wbfmt "SB"
endif

loop /wbp n
  pic create testoutm^n wbfmt 128k samplerate
!  pic create testoutm^n wbfmt 128m samplerate
  sourcepic/id=10+ntun+n/port=module^n/^switches/tl=1/host=y &
		testoutm^n(fs=nfft) _cbm^n ^device 1 !1k 
  mfft/s=d _cbm^n(fs=0) _cbmf^n nfft HANN 0
  xrtplot/s=d/xl=px-pw/xt=^n*ih+300+py/xw=iw/xh=ih/dbrange=100&
	/id=100+ntun+n/xn="Module-^n" &
	_cbmf^n ,^plotmin,^plotmax,d2
endloop

if /TCT gt 0
  picd/replay=2/flags=intclk/port=module2 play ^device tctest
endif

xpipe off

picd reset ^device 

! remove test output files
loop ntun nn
  erase testout^nn 
endloop

endmacro

startcontrols

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

label plotx
xcall wgetid pid 1001
!say "Got new freq ^pfreq from ^pid"
calc nn pid-100

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

label menux
res wwp1 ww+1
if item eq 1 then
  res w_^wwp1 2
elseif item eq 2 then
  res w_^wwp1 4
elseif item eq 3 then
  if w_^wwp1 ne 0
    xcall message "Channel must be stopped first"
  else
    xcall/name="Tuner #: " lval ii ii 1 24 1
    message send "DEVICE" nn+10 ii  
    message send /TITLE/ nn+100 0 "Plot-^nn  Tuner-^ii" 
    callp nn2ii ^ii
    goto xchan
  endif
elseif item eq 4 then
  message send "TC" nn+10 ,, 1 "D" -1
  message getwn "=TC" ,,tstat,,, sample time1 time2
  timex time1|time2 tx:timecode
  say "TC = ^timecode at Sample ^sample stat = ^tstat"
endif
return

label xxx
if item eq 1
  xcall menu item 0 "More;StopAll,StartAll"
  if item eq 1
    loop ntun n
      calc wwp1 (n-1) wpc * 1 + 1 +
      res w_^wwp1 4
    endloop
  else if item eq 2
    loop ntun n
      calc wwp1 (n-1) wpc * 1 + 1 +
      res w_^wwp1 2
    endloop
  endif
else
  stop
endif
return

procedure nn2ii d:newii[0]
if side eq 3
  res n3 nn
else
  calc n1 nn-1 cp mod
  calc n2 nn-1 cp / fix
  calc n3 n2*2 (side-1) + cp * n1+1 +
endif
if gtype gt 2 then
  res ii nn+ioff
elseif newii gt 0
  res n2i(n3) newii
elseif ioff gt 0
  calc ii n2i(n3) ioff +
else
  res ii n2i(n3)
endif
calc ww (nn-1) wpc * 1 +
return


endcontrols
