I've even simplified it even more by editing the original digitalio_arduino.cpp/h files from the guide (example with digital read/out) which BTW works without any issue.
Step by step i made following modifications:
1. Remove DIO read (leave only write) from CPP and H files
2. Change StartFcnSpec to digitalIOSetup and make changes in H file so port is always in OUTPUT mode
2. Add Servo.h library and create Servo object within CPP files
Up to this point, all edits went fine, no compile errors, all header files detected by Simulink and diode kept blinking as it should so the code actually worked in External Mode.
But of course as soon as i made the final modification and replaced pinMode() with myservo.attach() and digitalWrite() with myservo.write()
(of course i changed the data type in writeDigitalPin function from boolean to uint8_T)
the code despite compiling and building without any issue again does not work at all. Specified servo port is not being initialised at all and nothing happens when i change S-Function input.
I'm attaching simplified version where you can actually see how I edited the original Digital I/O example. Also, here's source:
LCT script
def = legacy_code('initialize');
def.SFunctionName = 'dout_sfun';
def.OutputFcnSpec = 'void NO_OP(uint8 p1, uint8 u1)';
def.StartFcnSpec = 'void NO_OP(uint8 p1)';
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def, '-DNO_OP=//')
def.SourceFiles = {fullfile(pwd,'..','src','digitalio_arduino.cpp')};
def.HeaderFiles = {'digitalio_arduino.h'};
def.IncPaths = {fullfile(pwd,'..','src'), 'C:\ProgramData\MATLAB\SupportPackages\R2021b\aIDE\libraries\Servo\src'};
def.OutputFcnSpec = 'void writeDigitalPin(uint8 p1, uint8 u1)';
def.StartFcnSpec = 'void digitalIOSetup(uint8 p1)';
legacy_code('sfcn_cmex_generate', def);
legacy_code('sfcn_tlc_generate', def);
legacy_code('rtwmakecfg_generate',def);
legacy_code('slblock_generate',def);
digitalio_arduino.cpp file
#include <Arduino.h>
#include <Servo.h>
#include "digitalio_arduino.h"
Servo myservo;
// Digital I/O initialization
extern "C" void digitalIOSetup(uint8_T pin)
{
//pinMode(pin, OUTPUT);
myservo.attach(pin);
}
// Write a logic value to pin
extern "C" void writeDigitalPin(uint8_T pin, uint8_T val)
{
//digitalWrite(pin, val);
myservo.write(val);
}
// [EOF]
digitalio_arduino.h file
#ifndef _DIGITALIO_ARDUINO_H_
#define _DIGITALIO_ARDUINO_H_
#include "rtwtypes.h"
#ifdef __cplusplus
extern "C" {
#endif
void digitalIOSetup(uint8_T pin);
void writeDigitalPin(uint8_T pin, uint8_T val);
#ifdef __cplusplus
}
#endif
#endif //_DIGITALIO_ARDUINO_H_