% read_waveforms    - Read out 4 acquisition channels

function data = read_waveforms(sys, dev)

base=[sys, ':', dev, ':'];

pvs={'ACQ:ADC0'; 'ACQ:ADC1'; 'ACQ:ADC2'; 'ACQ:ADC3'; 'ACQ:TSC'};
pvs = strcat(base, pvs);
vals = lcaGet(pvs);

for k=1:4
  data.chan(:,k) = vals(k,:)';
end
data.t = vals(5,:)';
data.sel = lcaGet([base, 'ACQ:SEL'], 1, 'byte');
data.sys = sys;
data.dev = dev;

% Collect calibration information
pvs = {'FSAMP'; 'REF:DDS:FREQ'};

k = 1;
for ch=0:2
  pvs = [pvs; sprintf('CH%d:SCALE', ch)];
  pvs = [pvs; sprintf('CH%d:PH_OFFSET', ch)];
  pvc{k}   = sprintf('CH%d:PWRSW', ch);
  pvc{k+1} = sprintf('CH%d:UNITS', ch);
  k = k + 2;
end
pvs = strcat(base, pvs);
vals = lcaGet(pvs);

data.fs  = vals(1)*1e6;
data.fif = vals(2)*1e3;
data.theta = data.fif / data.fs * 2 * pi;
[M N] = rat(data.fif/data.fs);
data.nfilt = N;

data.scale     = vals(3:2:8);
data.ph_offset = vals(4:2:8);

strings = lcaGet(strcat(base, pvc(:)));
data.pwrsw=strings(1:2:end);
data.units=strings(2:2:end);

% Plot
if (nargout == 0)
  plot_waveforms(data);
end
