Main Content

Specify the Initialization, Output, and Termination Behavior

ThesetupImplandstepImpl方法钩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, thedigitalIOSetupfunction is called insetupImpl. To update the logic state of the digital output pin, a call towriteDigitalPinis made fromstepImplmethod. Nothing needs to be done at termination. Follow these steps to update the initialization, output, and termination code sections of theDigitalWriteSystem object you created inSelect a System Object Template.

  1. In the MATLAB®editor, openDigitalWrite.mclass file.

  2. Update thesetupImplmethod 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, 1);endend...end

    Thecoder.cevalfunction executes calls to the C wrapper functions indigitalio_arduino.h. The second and third arguments ofcoder.cevalare the Arduino®hardware pin number and pin mode values, respectively.

  3. Update theBuildInfomethod 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
  4. Update thestepImplmethod with the following code.

    methods(Access=protected)...functionstepImpl(obj,u)%#okifisempty(coder.target)% Place simulation setup code hereelse% Call C-function implementing device outputcoder.ceval('writeDigitalPin', 9, u);endend...end
  5. Update thereleaseImplmethod 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 willTest System Object on MATLAB Command Line.

See Also

||