Skip to content

delay / esp_delay: transparently manage recurrent scheduled functions #8802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
d-a-v committed Jan 11, 2023
commit 0effb78cdee6f9b517c70d90372816757d57b32c
13 changes: 12 additions & 1 deletion cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,22 @@ extern "C" void __esp_delay(unsigned long ms) {

extern "C" void esp_delay(unsigned long ms) __attribute__((weak, alias("__esp_delay")));

bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms) {
bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, uint32_t intvl_ms) {
uint32_t expired = millis() - start_ms;
if (expired >= timeout_ms) {
return true;
}

if (intvl_ms == 0)
{
// run_recurrent_scheduled_functions() is called from esp_delay()->esp_suspend()
// intvl_ms is set according to recurrent schedule functions
// intervals based on their respective requirements
if (recurrent_max_grain_mS == 0)
update_recurrent_grain();
intvl_ms = recurrent_max_grain_mS;
}

esp_delay(std::min((timeout_ms - expired), intvl_ms));
return false;
}
Expand Down
13 changes: 2 additions & 11 deletions cores/esp8266/core_esp8266_wiring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,8 @@ static uint32_t micros_overflow_count = 0;
#define REPEAT 1

void __delay(unsigned long ms) {

// default "userland" delay() will call recurrent scheduled functions
// based on best effort according to their respective requirements

if (recurrent_max_grain_mS == 0)
update_recurrent_grain();
const auto start_ms = millis();
do
{
run_scheduled_recurrent_functions();
} while (!esp_try_delay(start_ms, ms, recurrent_max_grain_mS));
// use API letting recurrent scheduled functions run in background
esp_delay(ms, [](){ return true; });
}

void delay(unsigned long ms) __attribute__ ((weak, alias("__delay")));
Expand Down
5 changes: 3 additions & 2 deletions cores/esp8266/coredecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ inline void esp_suspend(T&& blocked) {
// Otherwise returns false after delaying for the relative
// remainder of timeout_ms, or an absolute intvl_ms, whichever is shorter.
// The delay may be asynchronously cancelled, before that timeout is reached.
bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms);
// intvl_ms==0 will adapt to recurrent scheduled functions and run them accordingly
bool esp_try_delay(const uint32_t start_ms, const uint32_t timeout_ms, uint32_t intvl_ms);

// This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds.
// Whenever it is resumed, as well as every intvl_ms millisconds, it performs
Expand All @@ -72,7 +73,7 @@ inline void esp_delay(const uint32_t timeout_ms, T&& blocked, const uint32_t int
// it keeps delaying for the remainder of the original timeout_ms period.
template <typename T>
inline void esp_delay(const uint32_t timeout_ms, T&& blocked) {
esp_delay(timeout_ms, std::forward<T>(blocked), timeout_ms);
esp_delay(timeout_ms, std::forward<T>(blocked), 0);
}

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ static int hostByNameImpl(const char* aHostname, IPAddress& aResult, uint32_t ti
esp_delay(timeout_ms,
[&]() {
return !pending->done;
}, 10);
});

if (pending->done) {
if ((pending->addr).isSet()) {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy