|
4 | 4 |
|
5 | 5 | #define HAVE_ESP_SUSPEND 1
|
6 | 6 |
|
| 7 | +#include "core_esp8266_features.h" |
| 8 | + |
7 | 9 | #ifdef __cplusplus
|
8 | 10 | extern "C" {
|
9 | 11 | #endif
|
@@ -40,27 +42,34 @@ void settimeofday_cb (BoolCB&& cb);
|
40 | 42 | void settimeofday_cb (const BoolCB& cb);
|
41 | 43 | void settimeofday_cb (const TrivialCB& cb);
|
42 | 44 |
|
43 |
| -using IsBlockedCB = std::function<bool()>; |
44 |
| - |
45 | 45 | // This overload of esp_suspend() performs the blocked callback whenever it is resumed,
|
46 | 46 | // and if that returns true, it immediately suspends again.
|
47 |
| -inline void esp_suspend(const IsBlockedCB& blocked) { |
| 47 | +template <typename T> |
| 48 | +inline void esp_suspend(T&& blocked) { |
48 | 49 | do {
|
49 | 50 | esp_suspend();
|
50 | 51 | } while (blocked());
|
51 | 52 | }
|
52 | 53 |
|
| 54 | +bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms); |
| 55 | + |
53 | 56 | // This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds.
|
54 | 57 | // Whenever it is resumed, as well as every intvl_ms millisconds, it performs
|
55 | 58 | // the blocked callback, and if that returns true, it keeps delaying for the remainder
|
56 | 59 | // of the original timeout_ms period.
|
57 |
| -void esp_delay(const uint32_t timeout_ms, const IsBlockedCB& blocked, const uint32_t intvl_ms); |
| 60 | +template <typename T> |
| 61 | +inline void esp_delay(const uint32_t timeout_ms, T&& blocked, const uint32_t intvl_ms) { |
| 62 | + const auto start_ms = millis(); |
| 63 | + while (!try_esp_delay(start_ms, timeout_ms, intvl_ms) && blocked()) { |
| 64 | + } |
| 65 | +} |
58 | 66 |
|
59 | 67 | // This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds.
|
60 | 68 | // Whenever it is resumed, it performs the blocked callback, and if that returns true,
|
61 | 69 | // it keeps delaying for the remainder of the original timeout_ms period.
|
62 |
| -inline void esp_delay(const uint32_t timeout_ms, const IsBlockedCB& blocked) { |
63 |
| - esp_delay(timeout_ms, blocked, timeout_ms); |
| 70 | +template <typename T> |
| 71 | +inline void esp_delay(const uint32_t timeout_ms, T&& blocked) { |
| 72 | + esp_delay(timeout_ms, std::forward<T>(blocked), timeout_ms); |
64 | 73 | }
|
65 | 74 |
|
66 | 75 | #endif // __cplusplus
|
|
0 commit comments