classdef Channel
    %CHANNEL Summary of this class goes here
    %   Detailed explanation goes here
    
    properties (Hidden, Access = private)
        scope
        channelnumber
    end
    properties (Dependent)
        state
        coupling
        bandwidth
        scale
        offset
        probe
        label
        type
    end
    
    methods
        function ch = Channel(scope,channelnumber)
            ch.channelnumber = channelnumber;
            ch.scope = scope;
        end
        
        function s = get.state(ch)
            s = ch.CHAN('state?');
        end
        
        function out = get.coupling(ch)
            out = ch.CHAN('coup?');
        end
        
        function out = get.bandwidth(ch)
            out = ch.CHAN('band?');
        end
        
        function out = get.offset(ch)
            out = ch.CHAN('offs?');
        end
        
        function out = get.scale(ch)
            out = ch.CHAN('scal?');
        end
        
        function out = get.label(ch)
            out = ch.CHAN('lab?');
        end
        
        function out = get.type(ch)
            out = ch.CHAN('type?');
        end
                
        function ch = set.state(ch,in)
            ch.CHAN('state',in);
        end
        
        function ch = set.coupling(ch,in)
            ch.CHAN('coup',in);
        end
        
        function ch = set.bandwidth(ch,in)
            ch.CHAN('band',in);
        end
        
        function ch = set.offset(ch,in)
            ch.CHAN('offs',in);
        end
        
        function ch = set.scale(ch,in)
            ch.CHAN('scal',in);
        end
        
        function ch = set.label(ch,in)
            ch.CHAN('lab',in);
        end
        
        function ch = set.type(ch,in)
            ch.CHAN('type',in);
        end
        
        function out = frequency(ch)
            out = str2double(ch.MEAS('freq'));
        end
        
        function out = peak2peak(ch)
            out = str2double(ch.MEAS('peak'));
        end
        
        function out = period(ch)
            out = str2double(ch.MEAS('per'));
        end
        
        function out = amplitude(ch)
            out = str2double(ch.MEAS('ampl'));
        end
        
        function out = mean(ch)
            out = str2double(ch.MEAS('mean'));
        end
        
        function out = rms(ch)
            out = str2double(ch.MEAS('rms'));
        end
        
        function out = phase(ch)
            out = str2double(ch.MEAS('phas'));
        end
        
        function data = waveform(ch,window)
            ch.scope.single;
            if nargin < 2
                window = 'DEF';
            end
            ch.scope.setWaveformSettings(window,'REAL')
            data = ch.getWaveform;
        end
        
        function data = getWaveform(ch)
            header = str2num(ch.CHAN('DATA:HEAD?'));
            data.length = header(3);
            data.start = header(1);
            data.stop = header(2);
            data.sampletime = str2double(ch.CHAN('DATA:XINC?'));
            ch.scope.opc;
            ch.scope.write_unsafe(['CHAN',num2str(ch.channelnumber),':DATA?']);
            data.data = ch.scope.readWaveform(data.length);
        end
        
    end
    
    methods (Hidden, Access = private)
        function c = CHAN(ch,string,in)
            if nargin == 2
                if strcmp(string(end),'?')
                    c = ch.scope.query(['CHAN',num2str(ch.channelnumber),':',string]);
                else
                    ch.scope.write(['CHAN',num2str(ch.channelnumber),':',string]);
                end
            else
                ch.scope.write(['CHAN',num2str(ch.channelnumber),':',string,' ',in]);
            end
        end
        
        function c = MEAS(ch,string)
            c = ch.scope.query(['MEAS',num2str(ch.channelnumber),':RES:ACT?',string]);
        end
        
%         function c = CHANsend(ch,string,in)
%             c = ['CHAN',num2str(ch.channelnumber),':',string,' ',in];
%         end
    end
end