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
    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 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 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)
            ch.scope.clear;
            ch.CHAN('TYPE HRES');
            ch.scope.write('FORM REAL');
            ch.scope.write('FORM:BORD MSBF');
            ch.CHAN('DATA:POIN DEF');
            ch.scope.write('SING');
            header = str2num(ch.CHAN('DATA:HEAD?'));
            ch.scope.opc;
            ch.scope.write_unsafe(['CHAN',num2str(ch.channelnumber),':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
    
    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