Main Content

Specify the Initialization, Output, and Termination

ThesetupImplandstepImplmethods 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, thedigitalIOSetupfunction is called insetupImpl. To update the logic state of the digital output pin, a call toreadDigitalPinis made from thestepImplmethod. 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.

  1. In the MATLAB®编辑器,打开DigitalRead.m.

  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, 0);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 value, respectively.

  3. Update theBuildInfomethod 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
  4. Update thestepImplmethod 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 theDigitalWriteSystem object, thestepImplmethod for theDigitalReadSystem object defines an output,y, which is the logical value of the chosen pin.

  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 willSet Output Port Propertiesof your system object.

See Also

||