classdef Oscilloscope < Equipment
    %OSCILLOSCOPE Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        nchannels
        horizontalPosition
        timescale
        ch1
        ch2
        ch3
        ch4
        trigger
        acquisition
    end
    
    methods
        function sc = Oscilloscope(ipAddress,port,nchannels)
            %OSCILLOSCOPE Construct an instance of this class
            %   Detailed explanation goes here
            sc@Equipment(ipAddress,port,-1);
            sc.trigger = Trigger(sc);
            sc.nchannels = nchannels;
            for i = 1:nchannels
                sc.(['ch',num2str(i)]) = Channel(sc,i);
            end
        end
        
        function clear(sc)
            sc.messageclose;
            sc.write_unsafe('*cls');
            flushinput(sc.tcp);
        end
        
        function s = get.acquisition(sc)
            s = sc.query('ACQ:STAT?');
        end
        
        function run(sc)
            sc.write_noerror('RUN');
        end
        
        function single(sc)
            sc.write_noerror('SING');
        end
        
        function stop(sc)
            sc.write_noerror('STOP');
        end
        
        function auto(sc)
            sc.write_noerror('AUT');
        end
        
        function enable_channels(sc)
            sc.write('CHAN:AON')
        end
        
        function disable_channels(sc)
            sc.write('CHAN:AOFF')
        end
        
        function setMeasurement(sc,measurement,type,source1,source2)
            prefix = ['MEAS',num2str(measurement),':'];
            fprintf(num2str(nargin))
            if nargin < 5
                source2 = 'CH2';
                if nargin < 4
                    source1 = 'CH1';
                end
            end
            source = [source1,', ',source2];
            sc.write([prefix,'SOUR ',source]);
            sc.write([prefix,'MAIN ',type]);
        end
        
        function m = getMeasurement(sc,measurement)
            m = str2double(sc.query(['MEAS',num2str(measurement),':RES:ACT?']));
        end
        
        function setWaveformSettings(sc,window,format,type)
            if nargin < 4
                type = 'HRES';
                if nargin < 3
                    format = 'REAL';
                    if nargin < 2
                        window = 'DEF';
                    end
                end
            end
            sc.clear;
            sc.write(['CHAN:TYPE ',type]);
            sc.write(['FORM ',format]);
            sc.write('FORM:BORD MSBF');
            sc.write(['CHAN:DATA:POIN ',window]);
            sc.single;
        end
        
        function data = readWaveform(sc,datalength)
            prefixstring = fscanf(sc.tcp,'%c',2);
            prefixlength = str2double(prefixstring(2));
            fscanf(sc.tcp,'%c',prefixlength);
            data = sc.bin_read_float(datalength);
            flushinput(sc.tcp);
        end
            
        
        function data = waveform(sc,channels,window,type)
            if nargin < 4
                type = 'HRES';
                if nargin < 3
                    window = 'DEF';
                    if nargin < 2
                        channels = 1:sc.nchannels;
                    end
                end
            end
            sc.enable_channels;
            sc.setWaveformSettings(window,'REAL',type);
            for i = channels
                curcha = ['ch',num2str(i)];
                wave = sc.(curcha).getWaveform;
                data.(curcha) = wave.data;
            end
                data.sampletime = wave.sampletime;
                data.length = wave.length;
        end
        
        function message(sc,msg)
            sc.messageclose;
            sc.write(['DISP:DIAL:MESS ''',msg,''''])
        end
        
        function messageclose(sc)
            sc.opc;
            sc.write_unsafe('DISP:DIAL:CLOS');
        end
        
        
%         function data = waveform(ch,cha)
%             ch.scope.clear;
%             ch.scope.write('CHAN1:TYPE HRES');
%             ch.scope.write('FORM REAL');
%             ch.scope.write('FORM:BORD MSBF');
%             ch.scope.write('CHAN1:DATA:POIN DEF');
%             ch.scope.write('SING');
%             header = str2num(ch.scope.query('CHAN1:DATA:HEAD?'));
%             ch.scope.write_unsafe('CHAN1:DATA?');
%             prefixstring = fscanf(ch.scope.tcp,'%c',2);
%             prefixlength = str2double(prefixstring(2));
%             datalength = fscanf(ch.scope.tcp,'%c',prefixlength);
%             data = fread(ch.scope.tcp,header(3),'float');
%             flushinput(ch.scope.tcp);
%         end
    end
end





