www.gusucode.com > Arduino_Engineering_Kit_Hardware_Support 工具箱matlab源码程序 > Arduino_Engineering_Kit_Hardware_Support/simulink/+motorcarrier/+blocks/mkrmotorcarrier_Battery.m

    classdef mkrmotorcarrier_Battery < matlab.System ...
        & matlab.system.mixin.internal.CustomIcon ...
        & matlab.system.mixin.Propagates...
        & matlabshared.svd.BlockSampleTime ...
        & coder.ExternalDependency
    %
    % Batter Sensor - MKR Motor Carrier
    %
    
    %   Copyright 2018 The MathWorks, Inc.
    
    %#codegen
    %#ok<*EMCA>
    
    properties(Nontunable)
        % SlaveAddress_ - I2C Slave Address
        SlaveAddress_ = hex2dec('66');
    end
    
    properties(Hidden)
        blockPlatform = 'ARDUINO';
    end
    
    properties(Constant, Hidden)        
        % --------------Commands for Battery ADC at D11---------------------
        GET_RAW_ADC_BATTERY             = 13   ;
        GET_CONVERTED_ADC_BATTERY       = 14   ;
        GET_FILTERED_ADC_BATTERY        = 15   ;
    end
    
    
    methods
        % Constructor
        function obj = mkrmotorcarrier_Battery(varargin)
            %This would allow the code generation to proceed with the
            %p-files in the installed location of the support package.
            coder.allowpcode('plain');
            
            % Support name-value pair arguments when constructing the object.
            setProperties(obj,nargin,varargin{:});
        end
    end
    
    methods (Access = protected)

        % Get data from D11
        function data = D11_getData(obj, command, target) 
            % First write the command and target over I2C
            final_data_to_write = [uint8(command), uint8(target)];
            % Number of bytes to read
            num_bytes_to_read = 5;
            % Allocate out
            out_raw = coder.nullcopy(uint8(zeros(1, num_bytes_to_read)));
            if coder.target('Rtw')% done only for code gen
                coder.cinclude('MW_arduinoI2C.h');
                coder.ceval('MW_i2cWriteLoop', uint8(obj.SlaveAddress_), 0, 0, ...
                         coder.rref(final_data_to_write), 2);
                % Read the 5 bytes (irq_status + int32 data) of data over I2C 
                coder.ceval('MW_i2cReadLoop', uint8(obj.SlaveAddress_),...
                    0, 0, uint8(num_bytes_to_read), coder.wref(out_raw));
            elseif ( coder.target('RtwForRapid') || coder.target('RtwForSfun') )
            end
            
            % Ignore the first irq_status byte received for the time being
            out = out_raw(2:end);
            % Concatenate received bytes to get uint32 data
            data = typecast(out, 'uint32');
        end
        
        function out = Battery_getRaw(obj)
            out = D11_getData(obj, uint8(obj.GET_RAW_ADC_BATTERY), 0);
        end
        
        function out = Battery_getConverted(obj)
            out = D11_getData(obj, uint8(obj.GET_CONVERTED_ADC_BATTERY), 0);
        end
    end
    
    methods(Access = protected)
        function setupImpl(~)
            % Perform one-time calculations, such as computing constants
            if coder.target('Rtw')
                
            end
        end
 
        function data = stepImpl(obj)
            % Implement algorithm. Calculate y as a function of input u and
            %  discrete states.
            data = uint16(0);
            if coder.target('Rtw')                
                data = uint16(Battery_getRaw(obj));                
            end
        end
        
        function num = getNumInputsImpl(~)
            % Define total number of inputs for system with optional inputs
            num = 0;
        end
        
        function num = getNumOutputsImpl(~)
            % Define total number of outputs for system with optional
            % outputs
            num = 1;
        end
        
        function varargout = getOutputNamesImpl(~)
            % Return output port names for System block
            varargout{1} ='voltage';
        end
        %
        function varargout = getOutputSizeImpl(~)
            % Return size for each output port
            varargout{1} =[1 1];
        end
        %
        function varargout = getOutputDataTypeImpl(~)
            % Return data type for each output port
            varargout{1} ='uint16';
        end
        %
        function varargout = isOutputComplexImpl(~)
            % Return true for each output port with complex data
            varargout{1} =false;
        end
        %
        function varargout = isOutputFixedSizeImpl(~)
            % Return true for each output port with fixed size
            varargout{1} =true;
        end
        
        function st = getSampleTimeImpl(obj)
            st = obj.SampleTime;
        end
        
        function maskDisplayCmds = getMaskDisplayImpl(obj)            
            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]...
                ['color(''blue'');',newline] ...
                ['text(100, 92, '' ' obj.blockPlatform ' '', ''horizontalAlignment'', ''right'');',newline]  ...
                ['color(''black'');',newline]...   
                ['sppkgroot = strrep(motorcarrier.internal.getSpPkgRootDir(),''\'',''/'');',newline]...
                ['image(fullfile(sppkgroot,''resources'',''battery.png''),''center'')',newline]];
        end
        
    end %methods
 
    methods(Access = protected, Static)
        function header = getHeaderImpl
            %getHeaderImpl Create mask header
            %   This only has an effect on the base mask.
            header = matlab.system.display.Header(mfilename('class'), ...
                'Title', 'Battery Read', ...
                'Text', sprintf('Read the battery voltage of the MKR Motor Carrier.\n\nThe block outputs the voltage as a uint16 digital value (0-4095, minimum to maximum).'), ...
                'ShowSourceLink', false);
        end
        
        function groups = getPropertyGroupsImpl            
            % Sample Time
            Sampletime = matlab.system.display.internal.Property('SampleTime', 'Description', 'Sample time');
            SlaveAddressProp = matlab.system.display.internal.Property('SlaveAddress_', 'IsGraphical', false);                       

            PropertyListOut = {Sampletime, SlaveAddressProp};
            % Create mask display
            Group = matlab.system.display.Section(...
                'PropertyList',PropertyListOut);
            groups = Group;
        end
        
        function flag = showSimulateUsingImpl
            % Return false if simulation mode hidden in System block dialog
            flag = false;
        end
        
        function simMode = getSimulateUsingImpl(~)
            simMode = 'Interpreted execution';
        end
    end
    
    methods (Static)
        function name = getDescriptiveName()
            name = 'MKR Motor Carrier Battery';
        end
        
        function b = isSupportedContext(context)
            b = context.isCodeGenTarget('rtw');
        end
        
         function updateBuildInfo(buildInfo, context)
             if context.isCodeGenTarget('rtw')
                codertarget.arduinobase.internal.arduinoI2CWrite.updateBuildInfo(buildInfo, context);
             end
        end
    end %methods
end