Skip to content

Commit 42d5350

Browse files
committed
Port Schedule to ESP32.
Same yield behavior as on ESP8266 - scheduler has policies for loop and yield initiated runs.
1 parent 65d9467 commit 42d5350

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

cores/esp32/Schedule.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ namespace {
3131
{
3232
auto repeat_ms = (repeat_us + 500) / 1000;
3333
ticker->once_ms(repeat_ms, [ticker, fn, repeat_us, policy]()
34-
{
35-
if (!schedule_function([ticker, fn, repeat_us, policy]()
36-
{
37-
if (fn()) ticker_scheduled(ticker, fn, repeat_us, policy);
38-
else delete ticker;
39-
return false;
40-
}, policy))
4134
{
42-
ticker_scheduled(ticker, fn, repeat_us, policy);
43-
}
44-
});
35+
if (!schedule_function([ticker, fn, repeat_us, policy]()
36+
{
37+
if (fn()) ticker_scheduled(ticker, fn, repeat_us, policy);
38+
else delete ticker;
39+
return false;
40+
}, policy))
41+
{
42+
ticker_scheduled(ticker, fn, repeat_us, policy);
43+
}
44+
});
4545
}
4646
};
4747

@@ -119,8 +119,10 @@ void run_scheduled_functions(schedule_e policy)
119119
schedule_queue.for_each_requeue([policy, &yieldNow](scheduled_fn_t& func)
120120
{
121121
if (yieldNow) {
122-
#ifdef ESP8266
122+
#if defined(ESP8266)
123123
cont_yield(g_pcont);
124+
#elif defined(ESP32)
125+
vPortYield();
124126
#else
125127
yield();
126128
#endif

cores/esp32/Schedule.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef ESP_SCHEDULE_H
22
#define ESP_SCHEDULE_H
33

4-
#include <functional>
5-
64
// This API is stabilizing
75
// Function signatures may change, internal queue will remain FIFO.
86
//
@@ -24,13 +22,21 @@
2422
// * Returns false if the number of scheduled functions exceeds
2523
// SCHEDULED_FN_MAX_COUNT.
2624

25+
#ifdef __cplusplus
26+
#include <functional>
27+
extern "C" {
28+
#endif
29+
2730
#define SCHEDULED_FN_MAX_COUNT 32
2831

29-
enum schedule_e
32+
typedef enum schedule_e_t
3033
{
3134
SCHEDULE_FUNCTION_FROM_LOOP,
3235
SCHEDULE_FUNCTION_WITHOUT_YIELDELAYCALLS
33-
};
36+
} schedule_e;
37+
38+
#ifdef __cplusplus
39+
}
3440

3541
// * Run the lambda only once next time
3642
bool schedule_function(std::function<void(void)>&& fn,
@@ -50,9 +56,18 @@ bool schedule_function_us(const std::function<bool(void)>& fn,
5056
uint32_t repeat_us,
5157
schedule_e policy = SCHEDULE_FUNCTION_FROM_LOOP);
5258

59+
extern "C" {
60+
#endif /* __cplusplus */
61+
5362
// Run all scheduled functions.
5463
// Use this function if your are not using `loop`, or `loop` does not return
5564
// on a regular basis.
56-
void run_scheduled_functions(schedule_e policy = SCHEDULE_FUNCTION_FROM_LOOP);
5765

58-
#endif //ESP_SCHEDULE_H
66+
#ifndef __cplusplus
67+
void run_scheduled_functions(schedule_e policy);
68+
#else
69+
void run_scheduled_functions(schedule_e policy = SCHEDULE_FUNCTION_FROM_LOOP);
70+
}
71+
#endif
72+
73+
#endif //ESP_SCHEDULE_H

0 commit comments

Comments
 (0)