gusucode.com > vnt工具箱matlab源码程序 > vnt/vntguis/+can/MessageFilter.m

    classdef (Hidden) MessageFilter < handle
% MessageFilter Class that implements settings for a CAN message filter.
%
%   The MessageFilter class implements a CAN message filter. It stores 
%   filter state and custom filter information for use in configuring 
%   and applying message filtering to a channel.
%

% Copyright 2010 The MathWorks, Inc.

properties (SetAccess = private)
    % AllowOnlyNumeric - An array of numeric IDs for a custom pass filter.
    AllowOnlyNumeric
    % AllowOnlyString - A string containing the IDs for a custom pass filter.
    AllowOnlyString
    % Maximum - The largest valid ID value for this filter.
    Maximum
    % State - The enumerated state of the filter.
    State
    % Type - Sets whether this filter is for 'Standard' or 'Extended' IDs.
    Type
end

properties (Constant)
    % MAX_ID_STD - The largest possible value for a standard ID message.
    MAX_ID_STD = 2047;
    % MAX_ID_EXT - The largest possible value for an extended ID message.
    MAX_ID_EXT = 536870911;
end

methods
    
    function obj = MessageFilter(type)
    % MessageFilter Constructor.
    %
    %   This method constructs an object for the class. It takes a type
    %   argument as input, which is a string indicating the type of
    %   message ID for which this filter is to be used.
    %

        % Set the filter type from constructor input.
        obj.Type = type;
        
        % Initialize the state of the filter to pass all messages.
        obj.State = can.FilterStates.AllowAll;
        
        % Set up the other filter properties based on the type.
        switch obj.Type
            case 'Standard'
                obj.Maximum = obj.MAX_ID_STD;
            case 'Extended'
                obj.Maximum = obj.MAX_ID_EXT;
        end
        
        obj.AllowOnlyNumeric = [];
        obj.AllowOnlyString = ['0:' num2str(obj.Maximum)];
    end
    
    function [success, numericArray] = translateCustomFilter(obj, strValue)
    % translateCustomFilter Translates string ID values into a numeric array.
    %
    %   This method converts a custom filter as a string, provided
    %   as input, into a numeric array of ID values, given as output. It
    %   also returns true/false which indicates if the translation was a
    %   success.
    %
    
        % Attach brackets used for expanding the array into numeric values.
        numericArray = ['[' strValue ']'];

        % Translate the string into an array of numbers.
        try
            % Eval the array information stored in the string.
            numericArray = eval(numericArray);
            % Eliminate any duplicate values.
            numericArray = unique(numericArray);
        catch err %#ok<NASGU>
            success = false;
            return;
        end
        
        % Validate the user input.
        try
            validateattributes(numericArray, {'numeric'}, ...
                {'vector', 'real', 'nonsparse', 'nonnegative', ...
                'nonnan', 'nonempty', 'integer', 'finite', ...
                '<=', obj.Maximum});
        catch err %#ok<NASGU>
            success = false;
            return;
        end
        
        % Return successful.
        success = true;
    end
    
    function success = updateFilterSettings(obj, newFilterState, allowOnlyCustomFilter)
    % updateFilterSettings Saves new settings to the filter.
    %
    %   This method is used to save new settings to the filter for
    %   state and any custom values or ranges. It takes two arguments. The
    %   first is the new filter state. The second is a string value which 
    %   holds the values of any custom filter entries.
    %
        
        % Validate and process filter settings based on the state.
        switch newFilterState
            case can.FilterStates.AllowOnly
                % Translate the string values into numeric values.
                [success, newValue] = translateCustomFilter(obj, allowOnlyCustomFilter);

                % Return if the translation failed.
                if ~success
                    return;
                end
                
                % Once the custom ID settings have been validated, store
                % them in the object.
                obj.AllowOnlyNumeric = newValue;
                obj.AllowOnlyString = allowOnlyCustomFilter;
                
            otherwise
                % Otherwise, just store the string directly. In this case,
                % the custom settings is not in use, but we do not want to
                % trash the custom value. The user may wish to reconfigure
                % their filter settings and return to a custom option. So,
                % we save the value to easily allow them to restore it.
                obj.AllowOnlyNumeric = [];
                obj.AllowOnlyString = allowOnlyCustomFilter;
        end
        
        % Set the filter state.
        obj.State = newFilterState;
        
        % Return successful.
        success = true;
    end
    
end

end