function [ok] = MFG_SetSine(h,freq,ampl,offs,chan)
%function [ok] = MFG_sine(h,freq,ampl,offs,chan)
% Instructs the function generator connected to handle h
% to produce a sine wave with frequency
% freq, amplitude ampl and offsett offs
% on channel chan.
% Author: Philipp Häfliger, 26-Feb-2025

ok=0;
stderror=2;

if ~isobject(h)
    fprintf(stderror,"Error: parameter h is not a valid object\n");
    return;
end; %if
if isempty(strfind(h.Address,"mfg"))
    fprintf(stderror,"Error: device handle is not associated with a TCP client waveform generator\n");
    return;
end; %if
if ~ismember(chan,1:2)
    fprintf(stderror,"Error: channel needs to be in {1,2}\n");
    return;
end; %if
minfreq=1e-6;
maxfreq=6e7;
if (freq>maxfreq) | (freq<minfreq)
    fprintf(stderror,"Error: frequency needs to be in [%f %f]\n",minfreq,maxfreq);
    return;
end; %if
minampl=1e-3;
maxampl=1e1;
if (ampl>maxampl) | (ampl<minampl)
    fprintf(stderror,"Error: amplitude needs to be in [%f %f]\n",minampl,maxampl);
    return;
end; %if
minoffs=-4.99;
maxoffs=4.99;
if (offs>maxoffs) | (offs<minoffs)
    fprintf(stderror,"Error: offset needs to be in [%f %f]\n",minoffs,maxoffs);
    return;
end; %if

h.writeline("SOUR"+string(chan)+":APPL:SIN "+string(freq)+","+string(ampl)+","+string(offs));

ok=1;

end