Skip to content

Commit 208c4a3

Browse files
committed
Use function template syntax to save using std::function objects.
1 parent 4b92d28 commit 208c4a3

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

cores/esp8266/core_esp8266_main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ extern "C" void __esp_delay(unsigned long ms) {
162162

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

165-
void esp_delay(const uint32_t timeout_ms, const IsBlockedCB& blocked, const uint32_t intvl_ms) {
166-
const auto start = millis();
165+
bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms) {
167166
decltype(millis()) expired;
168-
while ((expired = millis() - start) < timeout_ms && blocked()) {
169-
auto remaining = timeout_ms - expired;
170-
esp_delay(remaining <= intvl_ms ? remaining : intvl_ms);
167+
168+
if ((expired = millis() - start_ms) >= timeout_ms) {
169+
return true;
171170
}
171+
const auto remaining = timeout_ms - expired;
172+
esp_delay(remaining <= intvl_ms ? remaining : intvl_ms);
173+
return false;
172174
}
173175

174176
extern "C" void __yield() {

cores/esp8266/coredecls.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#define HAVE_ESP_SUSPEND 1
66

7+
#include "core_esp8266_features.h"
8+
79
#ifdef __cplusplus
810
extern "C" {
911
#endif
@@ -40,27 +42,34 @@ void settimeofday_cb (BoolCB&& cb);
4042
void settimeofday_cb (const BoolCB& cb);
4143
void settimeofday_cb (const TrivialCB& cb);
4244

43-
using IsBlockedCB = std::function<bool()>;
44-
4545
// This overload of esp_suspend() performs the blocked callback whenever it is resumed,
4646
// 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) {
4849
do {
4950
esp_suspend();
5051
} while (blocked());
5152
}
5253

54+
bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms);
55+
5356
// This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds.
5457
// Whenever it is resumed, as well as every intvl_ms millisconds, it performs
5558
// the blocked callback, and if that returns true, it keeps delaying for the remainder
5659
// 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+
}
5866

5967
// This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds.
6068
// Whenever it is resumed, it performs the blocked callback, and if that returns true,
6169
// 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);
6473
}
6574

6675
#endif // __cplusplus

tests/host/common/Arduino.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ extern "C" void esp_delay (unsigned long ms)
7171
usleep(ms * 1000);
7272
}
7373

74-
using IsBlockedCB = std::function<bool()>;
75-
76-
void esp_delay(const uint32_t timeout_ms, const IsBlockedCB& blocked, const uint32_t intvl_ms)
77-
{
78-
(void)blocked;
79-
(void)intvl_ms;
80-
usleep(timeout_ms * 1000);
74+
bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms) {
75+
decltype(millis()) expired;
76+
77+
if ((expired = millis() - start_ms) >= timeout_ms) {
78+
return true;
79+
}
80+
const auto remaining = timeout_ms - expired;
81+
esp_delay(remaining <= intvl_ms ? remaining : intvl_ms);
82+
return false;
8183
}
8284

8385
extern "C" void __panic_func(const char* file, int line, const char* func) {

0 commit comments

Comments
 (0)