www.gusucode.com > Arduino_Engineering_Kit_Hardware_Support 工具箱matlab源码程序 > Arduino_Engineering_Kit_Hardware_Support/simulink/+codertarget/+arduinobase/+internal/arduinoTachometer.m

    classdef arduinoTachometer < matlab.System ...
        & coder.ExternalDependency ...
        & matlab.system.mixin.Propagates ...
        & matlab.system.mixin.internal.SampleTime ...
        & matlab.system.mixin.internal.CustomIcon
    %   Tachometer 
    %
    
    % Copyright 2018 The MathWorks, Inc.
    
    %#codegen
    %#ok<*EMCA>
    
    properties (Nontunable)
        % PinNumber - Pin number
        PinNumber = 16;

        % InterruptMode - Interrupt mode
        InterruptMode = 'RISING';       

        %SampleTime Sample time
        SampleTime = 0.1;
    end
    
    properties(Constant, Hidden)        
        InterruptModeSet = matlab.system.StringSet({'RISING', 'FALLING', 'CHANGE', 'LOW', 'HIGH'});
    end
 
    properties (Dependent,Hidden)        
        InterruptMode_num
    end
    
    methods
        % Constructor
        function obj = arduinoTachometer(varargin)
            coder.allowpcode('plain');
            setProperties(obj,nargin,varargin{:});
        end
        
        function set.PinNumber(obj,val)
            classes = {'numeric'};
            attributes = {'nonempty','nonnan','finite','real','nonnegative','scalar','integer','<', 255};
            paramName = 'Pin number';
            validateattributes(val,classes,attributes,'',paramName);           
            obj.PinNumber = val;
        end
        
        function set.SampleTime(obj,sampleTime)
            coder.extrinsic('error');
            coder.extrinsic('message');
            
            validateattributes(sampleTime,{'numeric'},...
                {'nonnan', 'finite'},...
                '','''Sample time''');
            
            % Sample time must be a real scalar value or 2 element array.
            if ~isreal(sampleTime(1)) || numel(sampleTime) > 2
                error(message('arduino:build:InvalidSampleTimeNeedScalar'));
            end
            if numel(sampleTime) == 2 && sampleTime(1) > 0.0 && sampleTime(2) >= sampleTime(1)
                error(message('arduino:build:InvalidSampleTimeNeedSmallerOffset'));
            end
            if numel(sampleTime) == 2 && sampleTime(1) == -1.0 && sampleTime(2) ~= 0.0
                error(message('arduino:build:InvalidSampleTimeNeedZeroOffset'));
            end
            if numel(sampleTime) == 2 && sampleTime(1) == 0.0 && sampleTime(2) ~= 1.0
                error(message('arduino:build:InvalidSampleTimeNeedOffsetOne'));
            end
            obj.SampleTime = sampleTime;
        end                

        function val = get.InterruptMode_num(obj)
            switch obj.InterruptMode
                case 'RISING'
                    val = 0;
                case 'FALLING'
                    val = 1;
                case 'CHANGE'
                    val = 2;
                case 'LOW'
                    val = 3;
                case 'HIGH'
                    val = 4;
            end
        end
    end
    
    methods (Access=protected)
        function setupImpl(obj)            
            if coder.target('Rtw')
                % Attach the interrupt on specified Pin number 
                coder.cinclude('MW_Tachometer.h');
                coder.ceval('MW_Tachometer_Init',uint8(obj.PinNumber), uint8(obj.InterruptMode_num));
            else
                % Do nothing
            end
        end

        function releaseImpl(~)
            % Release resources, such as file handles           
        end
        
        function speedinrpm =  stepImpl(~)
            speedinrpm = uint32(0);
            
            if coder.target('Rtw')
                    speedinrpm = coder.ceval('MW_Tachometer_GetSpeed');
            else
                % Do nothing
            end
        end
    end
    
    methods (Access=protected)
        %% Define input properties
        function num = getNumInputsImpl(~)
            num = 0;
        end
        
        function num = getNumOutputsImpl(~)
                num = 1;
        end
        
        function varargout = getOutputNamesImpl(~)
            varargout{1} = 'Speed';
        end
        
        function varargout= getOutputSizeImpl(~)
            varargout{1} = [1,1];
        end
        
        function varargout= getOutputDataTypeImpl(~)
            varargout{1} = 'uint32';
        end
        
        function varargout  = isOutputComplexImpl(~)
               varargout{1} = false;
        end
        
        function varargout   = isOutputFixedSizeImpl(~)
               varargout{1} = true;
        end
        
        function st = getSampleTimeImpl(obj)
            st = obj.SampleTime;
        end                
        
        function maskDisplayCmds = getMaskDisplayImpl(obj)          
            pinNumber = sprintf('Pin: %s', num2str(obj.PinNumber));
            maskDisplayCmds = [ ...
                ['color(''white'');',newline]...
                ['plot([100,100,100,100]*1,[100,100,100,100]*1);',newline]...
                ['plot([100,100,100,100]*0,[100,100,100,100]*0);',newline]...
                ['sppkgroot = strrep(motorcarrier.internal.getSpPkgRootDir(),''\'',''/'');',newline]...
                ['image(fullfile(sppkgroot,''resources'',''tachometer.png''),''center'')',newline]...
                ['color(''blue'');',newline] ...
                ['text(99, 92, ''ARDUINO'', ''horizontalAlignment'', ''right'');', newline] ...
                ['color(''black'');',newline]...
                ['text(52,12,' [''' ' pinNumber ''',''horizontalAlignment'',''center'');' newline]]   ...
                ['color(''black'');',newline]...
                ];
        end
             
    end
    
    methods (Static, Access=protected)
        function simMode = getSimulateUsingImpl(~)
            simMode = 'Interpreted execution';
        end
        
        function isVisible = showSimulateUsingImpl
            isVisible = false;
        end
        
        function groups = getPropertyGroupsImpl(~)
            % Define section for properties in System block dialog box.
            % Tachometer Pin Number
            TachoPinNumber = matlab.system.display.internal.Property('PinNumber', 'Description', 'Pin number');
           
            % Interrupt Mode
            InterruptMode = matlab.system.display.internal.Property('InterruptMode', 'Description', 'Interrupt mode');
            
            % Sample Time
            Sampletime = matlab.system.display.internal.Property('SampleTime', 'Description', 'Sample time');
            
            PropertyListOut = {TachoPinNumber, InterruptMode, Sampletime};
            % Create mask display
            Group = matlab.system.display.Section(...
                'PropertyList',PropertyListOut);
            groups = Group;

        end
        % Note that this is ignored for the mask-on-mask
        function header = getHeaderImpl
            % getHeaderImpl Create mask header
            %   This only has an effect on the base mask.
            header = matlab.system.display.Header(mfilename('class'), ...
                'Title', 'Tachometer', ...
                'Text', sprintf('Read the rotational speed of the connected shaft.\n\nThe block outputs the speed of rotation as a uint32 value.'), ...
                'ShowSourceLink', false);
        end
    end
    
    methods (Static)
        function name = getDescriptiveName()
            name = 'Arduino Tachometer';
        end
        
        function b = isSupportedContext(context)
            b = context.isCodeGenTarget('rtw');
        end
        
        function updateBuildInfo(buildInfo, context)
            if context.isCodeGenTarget('rtw')
                
                   sppkgRoot = motorcarrier.internal.getSpPkgRootDir;  

                   % Include Paths
                   addIncludePaths(buildInfo, fullfile(sppkgRoot, 'include'));
                   addIncludeFiles(buildInfo, 'MW_Tachometer.h');   

                   % Source Files
                   systemTargetFile = get_param(buildInfo.ModelName,'SystemTargetFile');
                   
                   if isequal(systemTargetFile,'ert.tlc')
                    % Add the following when not in rapid-accel simulation
                    addSourceFiles(buildInfo, 'MW_Tachometer.cpp', fullfile(sppkgRoot,'src'),'BlockModules');                  
                  end 
            end
        end
    end
end