function [waveform,time,ok] = MDO_GetWaveform(h,channel)
%function [waveform,time,ok] = MDO_GetWaveform(h,channel)
%  gets the waveform with time ticks from the MDO oscilloscope
% Author: Philipp Häfliger, 29-Jan-2025
ok=0;
stderror=2;
stdout=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Checking input variables for validity
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isobject(h)
    fprintf(stderror,"Error: parameter h is not a valid object handle\n");
    return;
end; %if
if isempty(strfind(h.Address,"mdo"))
    fprintf(stderror,"Error: device handle is not associated with a TCP client oscilloscope\n");
    return;
end; %if
if ~ismember(channel,1:4)
    fprintf(stderror,"Error: channel needs to be in [1-4]\n");
    return;
end; %if
h.flush;
h.writeline(':ACQ'+string(channel)+':MEM?');
pause(10)
packet=uint8(h.read);

header_start=min(find(packet==uint8('#'))); % '#' marks the start of the header

% first char of header indicates length of its rest. '0' is 48, so we need to subtract 48
data_start=header_start+2+double(packet(header_start+1)-48) 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%parsing the parameters in the string returned from the scope
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
params=char(packet(1:(header_start-1)));
fprintf(stdout,"data header from scope: %s\n",params)
s=1;
i=1;
%default values
vertical_scale=1;
vertical_position=0;
horizontal_scale=1;
horizontal_position=0;
sampling_period=1;
while s<length(params)
   [name,n]=sscanf(params(s:size(params,2)),'%[A-Za-z 0-9:. +-]');
   s=s+length(name)+1;
   %params(s-1) %should be a ','
   [value,n]=sscanf(params(s:size(params,2)),'%[A-Za-z0-9:. +-]');
   s=s+length(value)+1;
   %params(s-1) %should be a ';'
   i=i+1;

   switch name
       case "Vertical Scale"
           vertical_scale=str2num(value);
           fprintf(stdout,"vertical scale: %f\n",vertical_scale)
       case "Vertical Position"
           vertical_position=str2num(value);
           fprintf(stdout,"vertical position: %f\n",vertical_position)
       case "Horizontal Scale"
           horizontal_scale=str2num(value);
           fprintf(stdout,"horizontal scale: %f\n",horizontal_scale)
       case "Horizontal Position"
           horizontal_position=str2num(value);
           fprintf(stdout,"horizontal position: %f\n",horizontal_position)
       case "Sampling Period"
           sampling_period=str2num(value);
           fprintf(stdout,"sampling period: %f\n",sampling_period)
   end; %switch
end; %while

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Converting the actual data. Its in signed 16 bit words, but we have it
% as uint8 array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m=packet(data_start:size(packet,2)) %cutting away parameter string plus data header
%mm=double(m)-double(m>127)*256
m=double(m(1:2:(size(m,2)-1)))*256 + double(m((2):2:(size(m,2)))); %unsigned 16 bit numbers
m=m-double(m>=2^15)*2^16; %interpret uint16 as 2's complement int16
ad_factor=25; % from the scope manual
m=m/ad_factor*vertical_scale - vertical_position;
waveform=m;
time=(1:size(waveform,2))*sampling_period - horizontal_position;
plot(time,waveform);
ok=1;
end