Specify the Initialization, Output, and Termination Behavior
ThesetupImpl
andstepImpl
方法钩C / c++函数系统object™. SeeWrite the Hardware-Specific C/C++ Codefor more information on creating C/C++ device driver code. The initialization of a digital pin as output needs to be done only once at model initialization. Hence, thedigitalIOSetup
function is called insetupImpl
. To update the logic state of the digital output pin, a call towriteDigitalPin
is made fromstepImpl
method. Nothing needs to be done at termination. Follow these steps to update the initialization, output, and termination code sections of theDigitalWrite
System object you created inSelect a System Object Template.
In the MATLAB®editor, open
DigitalWrite.m
class file.Update the
setupImpl
method using the following code.methods(Access=protected)functionsetupImpl(obj)%#ok
ifisempty(coder.target)% Place simulation setup code hereelse% Call C-function implementing device initializationcoder.cinclude('digitalio_arduino.h'); coder.ceval('digitalIOSetup', 9, 1);endend...end The
coder.ceval
function executes calls to the C wrapper functions indigitalio_arduino.h
. The second and third arguments ofcoder.ceval
are the Arduino®hardware pin number and pin mode values, respectively.Update the
BuildInfo
method using the following code.methods(Static)...functionupdateBuildInfo(buildInfo, context)ifcontext.isCodeGenTarget('rtw')% Update buildInfosrcDir = fullfile(fileparts(mfilename('fullpath')),'src');%#okincludeDir = fullfile(fileparts(mfilename('fullpath')),'include'); addIncludePaths(buildInfo,includeDir);% Use the following API's to add include files, sources and linker flagsaddSourceFiles(buildInfo,'digitalio_arduino.cpp', srcDir);endend...end
Update the
stepImpl
method with the following code.methods(Access=protected)...functionstepImpl(obj,u)%#ok
ifisempty(coder.target)% Place simulation setup code hereelse% Call C-function implementing device outputcoder.ceval('writeDigitalPin', 9, u);endend...end Update the
releaseImpl
method with the following code.methods(Access=protected)...functionreleaseImpl(obj)%#ok
ifisempty(coder.target)% Place simulation termination code hereelse% Call C-function implementing device termination% No termination code for Arduinoendend...end
In the next section, you willTest System Object on MATLAB Command Line.
See Also
Create a Digital Write Block|Select a System Object Template|Test System Object on MATLAB Command Line