arduino-libraries / ArduinoModbus

244 stars 116 forks source link

Use `rtos::ThisThread::sleep_for` as delay in `_sleep_response_timeout()` for Mbed OS boards #107

Open salasidis opened 1 year ago

salasidis commented 1 year ago

The code currently present is as follows

#elif defined(_MBED) - or ir could reference actual boards PORTENTA_H7 etc
    delay(ctx->response_timeout.tv_sec * 1000);
    delayMicroseconds(ctx->response_timeout.tv_usec);
#else

I would like if possible to add an #ifdef _MBED (as used by Portenta and others)

In there the delay can be changed to:

rtos::ThisThread::sleep_for(mbed::chrono::milliseconds_u32(sec * 1000));
rtos::ThisThread::sleep_for(mbed::chrono::milliseconds_u32(usec / 1000));
delayMicroseconds(usec % 1000);

This way delays are task switch implemented - not wasting CPU time, The sub ms delay will be done in the usal Arduino way, as MBed does not allow sub ms sleep intervals (ticker res 1mS).