|
1 | 1 | #include "Schedule.h"
|
2 | 2 | #include "PolledTimeout.h"
|
| 3 | +#include "Ticker.h" |
3 | 4 | #ifdef ESP8266
|
4 | 5 | #include "interrupts.h"
|
5 | 6 | #include "coredecls.h"
|
|
10 | 11 |
|
11 | 12 | typedef std::function<bool(void)> mFuncT;
|
12 | 13 |
|
| 14 | +constexpr uint32_t TICKER_MIN_US = 5000; |
| 15 | + |
13 | 16 | struct scheduled_fn_t
|
14 | 17 | {
|
15 |
| - mFuncT mFunc; |
16 |
| - esp8266::polledTimeout::periodicFastUs callNow; |
17 |
| - schedule_e policy; |
18 |
| - |
19 |
| - scheduled_fn_t() : callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { } |
| 18 | + mFuncT mFunc = nullptr; |
| 19 | + esp8266::polledTimeout::periodicFastUs callNow; |
| 20 | + schedule_e policy = SCHEDULE_FUNCTION_FROM_LOOP; |
| 21 | + scheduled_fn_t() : callNow(esp8266::polledTimeout::periodicFastUs::alwaysExpired) { } |
20 | 22 | };
|
21 | 23 |
|
22 | 24 | namespace {
|
23 |
| - static circular_queue_mp<scheduled_fn_t> schedule_queue(SCHEDULED_FN_MAX_COUNT); |
| 25 | + static circular_queue_mp<scheduled_fn_t> schedule_queue(SCHEDULED_FN_MAX_COUNT); |
24 | 26 | #ifndef ESP8266
|
25 |
| - std::mutex schedulerMutex; |
| 27 | + std::mutex schedulerMutex; |
26 | 28 | #endif
|
| 29 | + |
| 30 | + void ticker_scheduled(Ticker* ticker, const std::function<bool(void)>& fn, uint32_t repeat_us, schedule_e policy) |
| 31 | + { |
| 32 | + auto repeat_ms = (repeat_us + 500) / 1000; |
| 33 | + 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)) |
| 41 | + { |
| 42 | + ticker_scheduled(ticker, fn, repeat_us, policy); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
27 | 46 | };
|
28 | 47 |
|
29 | 48 | bool IRAM_ATTR schedule_function_us(std::function<bool(void)>&& fn, uint32_t repeat_us, schedule_e policy)
|
30 | 49 | {
|
31 |
| - scheduled_fn_t item; |
32 |
| - item.policy = policy; |
33 |
| - item.mFunc = std::move(fn); |
34 |
| - if (repeat_us) item.callNow.reset(repeat_us); |
35 |
| - return schedule_queue.push(std::move(item)); |
| 50 | + if (repeat_us >= TICKER_MIN_US) |
| 51 | + { |
| 52 | + auto ticker = new Ticker; |
| 53 | + if (!ticker) return false; |
| 54 | + if (!schedule_function([ticker, fn = std::move(fn), repeat_us, policy]() |
| 55 | + { |
| 56 | + ticker_scheduled(ticker, fn, repeat_us, policy); |
| 57 | + return false; |
| 58 | + }, SCHEDULE_FUNCTION_WITHOUT_YIELDELAYCALLS)) |
| 59 | + { |
| 60 | + delete ticker; |
| 61 | + return false; |
| 62 | + } |
| 63 | + return true; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + scheduled_fn_t item; |
| 68 | + item.mFunc = std::move(fn); |
| 69 | + if (repeat_us) item.callNow.reset(repeat_us); |
| 70 | + item.policy = policy; |
| 71 | + return schedule_queue.push(std::move(item)); |
| 72 | + } |
36 | 73 | }
|
37 | 74 |
|
38 | 75 | bool IRAM_ATTR schedule_function_us(const std::function<bool(void)>& fn, uint32_t repeat_us, schedule_e policy)
|
39 | 76 | {
|
40 |
| - return schedule_function_us(std::function<bool(void)>(fn), repeat_us, policy); |
| 77 | + return schedule_function_us(std::function<bool(void)>(fn), repeat_us, policy); |
41 | 78 | }
|
42 | 79 |
|
43 | 80 | bool IRAM_ATTR schedule_function(std::function<void(void)>&& fn, schedule_e policy)
|
44 | 81 | {
|
45 |
| - return schedule_function_us([fn = std::move(fn)]() { fn(); return false; }, 0, policy); |
| 82 | + return schedule_function_us([fn = std::move(fn)]() { fn(); return false; }, 0, policy); |
46 | 83 | }
|
47 | 84 |
|
48 | 85 | bool IRAM_ATTR schedule_function(const std::function<void(void)>& fn, schedule_e policy)
|
49 | 86 | {
|
50 |
| - return schedule_function(std::function<void(void)>(fn), policy); |
| 87 | + return schedule_function(std::function<void(void)>(fn), policy); |
51 | 88 | }
|
52 | 89 |
|
53 | 90 | void run_scheduled_functions(schedule_e policy)
|
54 | 91 | {
|
55 |
| - // Note to the reader: |
56 |
| - // There is no exposed API to remove a scheduled function: |
57 |
| - // Scheduled functions are removed only from this function, and |
58 |
| - // its purpose is that it is never called from an interrupt |
59 |
| - // (always on cont stack). |
| 92 | + // Note to the reader: |
| 93 | + // There is no exposed API to remove a scheduled function: |
| 94 | + // Scheduled functions are removed only from this function, and |
| 95 | + // its purpose is that it is never called from an interrupt |
| 96 | + // (always on cont stack). |
60 | 97 |
|
61 |
| - static bool fence = false; |
62 |
| - { |
| 98 | + static bool fence = false; |
| 99 | + { |
63 | 100 | #ifdef ESP8266
|
64 |
| - InterruptLock lockAllInterruptsInThisScope; |
| 101 | + InterruptLock lockAllInterruptsInThisScope; |
65 | 102 | #else
|
66 |
| - std::lock_guard<std::mutex> lock(schedulerMutex); |
| 103 | + std::lock_guard<std::mutex> lock(schedulerMutex); |
67 | 104 | #endif
|
68 |
| - if (fence) { |
69 |
| - // prevent recursive calls from yield() |
70 |
| - return; |
71 |
| - } |
72 |
| - fence = true; |
73 |
| - } |
| 105 | + if (fence) { |
| 106 | + // prevent recursive calls from yield() |
| 107 | + return; |
| 108 | + } |
| 109 | + fence = true; |
| 110 | + } |
74 | 111 |
|
75 |
| - esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms |
| 112 | + esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms |
76 | 113 |
|
77 |
| - // run scheduled function: |
78 |
| - // - when its schedule policy allows it anytime |
79 |
| - // - or if we are called at loop() time |
80 |
| - // and |
81 |
| - // - its time policy allows it |
82 |
| - schedule_queue.for_each_requeue([policy, &yieldNow](scheduled_fn_t& func) |
83 |
| - { |
84 |
| - if (yieldNow) { |
| 114 | + // run scheduled function: |
| 115 | + // - when its schedule policy allows it anytime |
| 116 | + // - or if we are called at loop() time |
| 117 | + // and |
| 118 | + // - its time policy allows it |
| 119 | + schedule_queue.for_each_requeue([policy, &yieldNow](scheduled_fn_t& func) |
| 120 | + { |
| 121 | + if (yieldNow) { |
85 | 122 | #ifdef ESP8266
|
86 |
| - cont_yield(g_pcont); |
| 123 | + cont_yield(g_pcont); |
87 | 124 | #else
|
88 |
| - yield(); |
| 125 | + yield(); |
89 | 126 | #endif
|
90 |
| - } |
91 |
| - return |
92 |
| - (func.policy != SCHEDULE_FUNCTION_WITHOUT_YIELDELAYCALLS && policy != SCHEDULE_FUNCTION_FROM_LOOP) |
93 |
| - || !func.callNow |
94 |
| - || func.mFunc(); |
95 |
| - }); |
96 |
| - fence = false; |
| 127 | + } |
| 128 | + return |
| 129 | + (func.policy != SCHEDULE_FUNCTION_WITHOUT_YIELDELAYCALLS && policy != SCHEDULE_FUNCTION_FROM_LOOP) |
| 130 | + || !func.callNow |
| 131 | + || func.mFunc(); |
| 132 | + }); |
| 133 | + fence = false; |
97 | 134 | }
|
0 commit comments