image thumbnail

I2C block for ArduinoIO simulink package

by

 

02 May 2013 (Updated )

This is an augmenting block that enables the ArduinoIO package with the I2C bus read capability.

msfun_arduino_i2c(block)
function msfun_arduino_i2c(block)
% Help for Writing Level-2 M-File S-Functions:
%   web([docroot '/toolbox/simulink/sfg/f7-67622.html']

    %   Copyright 2011 The MathWorks, Inc.s

% instance variables 
myArduino = [];
slaveNum = '5A';  % this is the I2C address of the slave in hex format
reg = '07';% this is the register address which you want to read data from
setup(block);

%% ---------------------------------------------------------

    function setup(block)
        % Register the number of ports.
        block.NumInputPorts  = 0;
        block.NumOutputPorts = 1;
        
        block.SetPreCompOutPortInfoToDynamic;
        block.OutputPort(1).Dimensions  = 1;
        block.OutputPort(1).SamplingMode = 'sample';
        
        % Set up the states
        block.NumContStates = 0;
        block.NumDworks = 0;
        
        % Register the parameters.
        block.NumDialogPrms     = 4; % arduinoVar, slaveAddr, reg, Ts
        block.DialogPrmsTunable = {'Nontunable', 'Nontunable', 'Nontunable', 'Nontunable'};
        
        % Set the sample time
        block.SampleTimes = [block.DialogPrm(4).Data 0];
        
        block.SetAccelRunOnTLC(false); % run block in interpreted mode even w/ Acceleration
        block.SimStateCompliance = 'DefaultSimState';
                
        % the ArduinoIO Setup block uses the Start method to initialize the arduino
        % connection; by using InitConditions, we ensure that we don't access
        % the variable before it is created
        
        block.RegBlockMethod('CheckParameters', @CheckPrms); % called during update diagram
        % block.RegBlockMethod('Start', @Start); % called first
        block.RegBlockMethod('PostPropagationSetup', @PostPropSetup); 
        block.RegBlockMethod('InitializeConditions', @InitConditions); % called second
        block.RegBlockMethod('Outputs', @Output); % called first in sim loop
        % block.RegBlockMethod('Update', @Update); % called second in sim loop
        block.RegBlockMethod('Terminate', @Terminate);
    end

%%
    function CheckPrms(block)        
        try
            validateattributes(block.DialogPrm(1).Data, {'char'}, {'nonempty'}); % Name of arduino instance
            validateattributes(block.DialogPrm(2).Data, {'char'}, {'nonempty'}); % device addr
            validateattributes(block.DialogPrm(3).Data, {'char'}, {'nonempty'});
            validateattributes(block.DialogPrm(4).Data, {'numeric'}, {'real', 'scalar', 'nonzero'}); % sample time
        catch %#ok<CTCH>
            error('Simulink:ArduinoIO:invalidParameter', 'Invalid value for a mask parameter');
        end        
    end                
        
%%
    function PostPropSetup(block)
        st = block.SampleTimes;
        if st(1) == 0
            error('The ArduinoIO library blocks can only handle discrete sample times');
        end        
    end
%%
    function InitConditions(block) 
        % fprintf('%s: InitConditions\n', getfullname(block.BlockHandle));
        customData = getSetupBlockUserData(bdroot(block.BlockHandle), block.DialogPrm(1).Data);
        myArduino = customData('arduinoHandle');
        try
            slaveAddr_hex = block.DialogPrm(2).Data; % the device addr will be received by the .pde file
            slaveNum = hex2dec(slaveAddr_hex);
            reg_hex       = block.DialogPrm(3).Data;
            reg      = hex2dec(reg_hex);
        catch e
            e
        end
        % myArduino.pinMode(myPin, 'input');
    end

%%
    function Output(block)
        % fprintf('%s: Output\n', getfullname(block.BlockHandle));
        i2cValue = myArduino.i2cRead(slaveNum, reg);
        if isempty(i2cValue)
            i2cValue = 0;
        end        
        block.OutputPort(1).Data = i2cValue;
    end

%%
    function Terminate(block) %#ok<INUSD>
         % The ArduinoIO Setup block handles cleanup of myArduino
    end

end

Contact us