ThesetupImpl
andstepImpl
methods hook the C functions to the System object™. 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 toreadDigitalPin
is made from thestepImpl
method. Nothing needs to be done at termination. Follow these steps to update the initialization, output, and termination code sections of the DigitalRead System object you created inSelect System Object Template.
In the MATLAB®编辑器,打开DigitalRead.m
.
Update thesetupImpl
method using the following code.
methods(Access=protected)functionsetupImpl(obj)%#okifisempty(coder.target)% Place simulation setup code hereelse% Call C-function implementing device initializationcoder.cinclude('digitalio_arduino.h'); coder.ceval('digitalIOSetup', 9, 0);endend...end
Thecoder.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 value, respectively.
Update theBuildInfo
method using the following code.
methods(Static)...functionupdateBuildInfo(buildInfo, context)ifcontext.isCodeGenTarget('rtw')%更新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 thestepImpl
method with the following code.
methods(Access=protected)...functiony = stepImpl(obj)%#oky = double(0);ifisempty(coder.target)% Place simulation output code hereelse% Call C-function implementing device outputy = coder.ceval('readDigitalPin', 9);endend...end
Unlike theDigitalWrite
System object, thestepImpl
method for theDigitalRead
System object defines an output,y
, which is the logical value of the chosen pin.
Update thereleaseImpl
method with the following code.
methods(Access=protected)...functionreleaseImpl(obj)%#okifisempty(coder.target)% Place simulation termination code hereelse% Call C-function implementing device termination% No termination code for Arduinoendend...end
In the next section, you willSet Output Port Propertiesof your system object.
Create a Digital Read Block|Select System Object Template|Set Output Port Properties