1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-23 08:45:22 +03:00

delay / esp_delay: transparently manage recurrent scheduled functions (#8802)

Recurrent scheduled functions will always be running in background.

esp_delay()'s interval (intvl_ms) is internally kept to its highest value allowing to honor recurrent scheduled functions requirements.

It transparently allows to keep with the arduino and nonos-sdk trivial programming way and still use background services or drivers running regularly.
This commit is contained in:
david gauchard
2023-01-14 22:25:57 +01:00
committed by GitHub
parent e1c4a6c8e6
commit 39080e317e
8 changed files with 74 additions and 30 deletions

View File

@ -451,9 +451,8 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) {
//tasks to wait correctly.
constexpr unsigned int timeoutValue = 1000; //1 second
if(can_yield()) {
// The final argument, intvl_ms, to esp_delay influences how frequently
// the scheduled recurrent functions (Schedule.h) are probed.
esp_delay(timeoutValue, [m]() { return wifi_get_opmode() != m; }, 5);
// check opmode every 100ms or give up after timeout
esp_delay(timeoutValue, [m]() { return wifi_get_opmode() != m; }, 100);
//if at this point mode still hasn't been reached, give up
if(wifi_get_opmode() != (uint8) m) {
@ -642,11 +641,8 @@ static int hostByNameImpl(const char* aHostname, IPAddress& aResult, uint32_t ti
// We need to wait for c/b to fire *or* we exit on our own timeout
// (which also requires us to notify the c/b that it is supposed to delete the pending obj)
case ERR_INPROGRESS:
// Re-check every 10ms, we expect this to happen fast
esp_delay(timeout_ms,
[&]() {
return !pending->done;
}, 10);
// sleep until dns_found_callback is called or timeout is reached
esp_delay(timeout_ms, [&]() { return !pending->done; });
if (pending->done) {
if ((pending->addr).isSet()) {

View File

@ -84,13 +84,13 @@ static void printWiFiStatus(wl_status_t status)
static wl_status_t waitWiFiConnect(uint32_t connectTimeoutMs)
{
wl_status_t status = WL_CONNECT_FAILED;
// The final argument, intvl_ms, to esp_delay influences how frequently
// the scheduled recurrent functions (Schedule.h) are probed.
// Wait for WiFi to connect
// stop waiting upon status checked every 100ms or when timeout is reached
esp_delay(connectTimeoutMs,
[&status]() {
status = WiFi.status();
return status != WL_CONNECTED && status != WL_CONNECT_FAILED;
}, 0);
}, 100);
// Check status
if (status == WL_CONNECTED) {
@ -236,13 +236,12 @@ int8_t ESP8266WiFiMulti::startScan()
WiFi.scanNetworks(true);
// Wait for WiFi scan change or timeout
// The final argument, intvl_ms, to esp_delay influences how frequently
// the scheduled recurrent functions (Schedule.h) are probed.
// stop waiting upon status checked every 100ms or when timeout is reached
esp_delay(WIFI_SCAN_TIMEOUT_MS,
[&scanResult]() {
scanResult = WiFi.scanComplete();
return scanResult < 0;
}, 0);
}, 100);
// Check for scan timeout which may occur when scan does not report completion
if (scanResult < 0) {
DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n");

View File

@ -144,8 +144,7 @@ public:
_connect_pending = true;
_op_start_time = millis();
// will resume on timeout or when _connected or _notify_error fires
// give scheduled functions a chance to run (e.g. Ethernet uses recurrent)
esp_delay(_timeout_ms, [this]() { return this->_connect_pending; }, 1);
esp_delay(_timeout_ms, [this]() { return this->_connect_pending; });
_connect_pending = false;
if (!_pcb) {
DEBUGV(":cabrt\r\n");
@ -485,8 +484,7 @@ protected:
_send_waiting = true;
// will resume on timeout or when _write_some_from_cb or _notify_error fires
// give scheduled functions a chance to run (e.g. Ethernet uses recurrent)
esp_delay(_timeout_ms, [this]() { return this->_send_waiting; }, 1);
esp_delay(_timeout_ms, [this]() { return this->_send_waiting; });
_send_waiting = false;
} while(true);