classdef (Hidden) LogicAnalyzerWave < dsp.AbstractLogicAnalyzerDisplay
    %LogicAnalyzerWave Wave object for dsp.LogicAnalyzer
    
    %   This object is used to describe the properties of a wave
    %   displayed in the Logic Analyzer window.
    
    %   Copyright 2012-2014 The MathWorks, Inc.
    
    properties
        %Color Color used for display
        %   Specify Color to be one of 'Default' | 'Black' | 'Blue' |
        %   'Green' | 'Cyan' | 'Red' | 'Magenta' | 'Yellow' |
        %   'White'
        %   When an empty value is specified, the display is the
        %   default color chosen in the Logic Analyzer.
        %   The default value for this property is 'Default'.
        Color = 'Default';
        
        %InputChannel Input channel that corresponds to this wave
        %   The property specifies the input channel whose data is used
        %   for this wave. The default value for this property is 1
        %   (connect the first input to this wave).
        InputChannel = 1;
        
        %Radix Radix for wave
        %   Specify Radix to be one of 'Default' | 'Hexadecimal' | 'Octal' |
        %   'Binary' | 'Signed decimal' | 'Unsigned decimal'. This
        %   property does not apply to double, single and logical inputs.
        %   When an empty value is specified, the radix for the wave is
        %   the default radix specified in the Logic Analyzer.
        %   The default value for this property is 'Default'.
        Radix = 'Default';
        
        % FontSize Font size for values in the wave
        %   Specify the Font size to be a scalar non-negative integer.
        %   When the value 0 is specified, the font size for the wave
        %   is the default font size specified in the Logic Analyzer.
        %   The default value for this property is 0.
        FontSize = 0;
        
        % Format Display format for wave
        %   Specify Format to be one of 'Default' | 'Digital' | 'Analog'. When
        %   'Default' value is specified, the display format for the wave is the
        %   default display format specified in the Logic Analyzer. The default
        %   value of this property is 'Default'.
        Format = 'Default';
    end
    
    %{
    properties (SetAccess = protected)
        %DataType Data Type of wave
        %  This information is derived from the input data provided to the
        %  Logic Analyzer.
        DataType;
    end
    %}
    
    methods
        function obj = LogicAnalyzerWave(varargin)
            % constructor
            obj = obj@dsp.AbstractLogicAnalyzerDisplay;
            % pv pairs should be multiple of 2 and all properties should be
            % char
            if nargin~= 0 && (mod(nargin,2) ~= 0 || ...
                    ~all(cellfun(@(inp)ischar(inp) || isstring(inp), varargin(1:2:end))))
                error(message('dspshared:LogicAnalyzer:pvpairs'));
            end
            for ii = 1:nargin/2
                obj.(char(varargin{(2*ii)-1})) = varargin{2*ii};
            end
        end
    end
    
    methods
        
        function obj = set.Color(obj, value)
            % Color can be one of the set specifiedy
            if ischar(value) || isstring(value)
                colorSet = getLogicAnalyzerDisplayColorSet;
                colorSet = {'Default', colorSet{:}}; %#ok<CCAT>
                value    = validatestring(value, colorSet, '', 'Color');
            end
            obj.Color = value;
        end % set.Color
        
        function obj = set.InputChannel(obj, value)
            % InputChannel must be a non-negative scalar integer
            propName = 'InputChannel';
            obj.validateScalarInteger(value, propName);
            obj.(propName) = value;
        end
        
        function obj = set.Radix(obj, value)
            % Radix must be one of 'Default' | 'Hexadecimal' | 'Octal' | 'Binary' |
            % 'Signed decimal' | 'Unsigned decimal'
            propName = 'Radix';
            radixSet = getLogicAnalyzerDisplayRadixSet;
            radixSet = {'Default', radixSet{:}}; %#ok<CCAT>
            obj.(propName) = validatestring(value, radixSet, '', propName);
        end
        
        function obj = set.FontSize(obj, value)
            % Height must be a non-negative scalar integer
            propName = 'FontSize';
            obj.validateScalarInteger(value, propName);
            obj.(propName) = value;
        end
        
        function obj = set.Format(obj, value)
            % Specify Format to be one of 'Default' | 'Automatic' |'Digital'| 'Analog'
            propName = 'Format';
            formatSet = getLogicAnalyzerDisplayFormatSet;
            formatSet = {'Default', formatSet{:}}; %#ok<CCAT>
            obj.(propName) = validatestring(value, formatSet, '', propName);
        end
    end
    
    methods(Static, Hidden)
        function validateScalarInteger(value, propName)
            % value of propname must be a scalar integer
            % non-negative and positi
            validateattributes(value, ...
                {'numeric'}, {'real','finite','scalar','integer','nonnegative'},'', ...
                propName);
        end
        
        function obj = fromGraphicalSettings(gs, inputIds)
            inputs = {'InputChannel', find(strcmp(inputIds, gs.signalId)), ...
                'Tag', sprintf('W%d', gs.instanceNumber)};
            if isfield(gs, 'radix')
                switch gs.radix
                    case 'unsigneddecimal'
                        radix = 'Unsigned decimal';
                    case 'signeddecimal'
                        radix = 'Signed decimal';
                    otherwise
                        radix = [upper(gs.radix(1)) gs.radix(2:end)];
                end
                inputs = [inputs {'Radix', radix}];
            end
            if isfield(gs, 'format')
                inputs = [inputs {'Format', [upper(gs.format(1)) gs.format(2:end)]}];
            end
            if isfield(gs, 'height')
                inputs = [inputs {'Height', gs.height}];
            end
            if isfield(gs, 'fontSize')
                inputs = [inputs {'FontSize', gs.fontSize}];
            end
            if isfield(gs, 'name')
                inputs = [inputs {'Name', gs.name}];
            end
            if isfield(gs, 'color')
                color = lower(gs.color);
                if strncmpi(color(1), '#', 1)
                    color = uiservices.hexColorToRGB(color);
                end
                inputs = [inputs {'Color', color}];
            end
            
            obj = dsp.LogicAnalyzerWave(inputs{:});
        end
    end
    
    methods(Hidden)
        
        function gs = toGraphicalSettings(this, inputIds, varargin)
            gs = toGraphicalSettings@dsp.AbstractLogicAnalyzerDisplay(this, varargin{:});
            gs.type = 'wave';
            if nargin < 3 || any(strcmp(varargin, 'Color'))
                color = this.Color;
                if strcmpi(color, 'default')
                    gs.color = 'default';
                elseif ischar(color)
                    gs.color = uiservices.stringColorToHexColor(color);
                else
                    gs.color = uiservices.rgbToHexColor(color);
                end
            end
            if nargin < 3 || any(strcmp(varargin, 'Format'))
                gs.format = lower(this.Format);
                if strcmp(gs.format, 'automatic')
                    gs.format = 'auto';
                end
            end
            if nargin < 3 || any(strcmp(varargin, 'Radix'))
                gs.radix = strrep(lower(this.Radix), ' ', '');
            end
            if nargin < 3 || any(strcmp(varargin, 'FontSize'))
                gs.fontSize = this.FontSize;
            end
            if nargin < 3 || any(strcmp(varargin, 'InputChannel'))
                gs.signalId = inputIds{this.InputChannel};
            end
        end
                
        function setValues = set(obj, propName) %#ok<INUSL>
            % allows tab completion
            switch(propName)
                case 'Color'
                    setValues = getLogicAnalyzerDisplayColorSet;
                    setValues = {'Default', setValues{:}}; %#ok<CCAT>
                case 'Radix'
                    setValues = getLogicAnalyzerDisplayRadixSet;
                    setValues = {'Default', setValues{:}};
                case 'Format'
                    setValues = getLogicAnalyzerDisplayFormatSet;
                    setValues = {'Default', setValues{:}};
            end
        end % set
    end
end
