21
21
#include " Schedule.h"
22
22
#include " PolledTimeout.h"
23
23
#include " interrupts.h"
24
- #include " coredecls.h"
25
24
26
25
typedef std::function<void (void )> mSchedFuncT ;
27
26
struct scheduled_fn_t
@@ -50,6 +49,8 @@ static recurrent_fn_t* rLast = nullptr;
50
49
// The target time for scheduling the next timed recurrent function
51
50
static decltype (micros()) rTarget;
52
51
52
+ constexpr decltype (micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0 ) >> 1;
53
+
53
54
// Returns a pointer to an unused sched_fn_t,
54
55
// or if none are available allocates a new one,
55
56
// or nullptr if limit is reached
@@ -106,7 +107,7 @@ bool schedule_function(const std::function<void(void)>& fn)
106
107
107
108
IRAM_ATTR // (not only) called from ISR
108
109
bool schedule_recurrent_function_us (const std::function<bool (void )>& fn,
109
- uint32_t repeat_us, const std::function<bool(void )>& alarm)
110
+ decltype(micros()) repeat_us, const std::function<bool(void )>& alarm)
110
111
{
111
112
assert (repeat_us < decltype (recurrent_fn_t ::callNow)::neverExpires); // ~26800000us (26.8s)
112
113
@@ -126,7 +127,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126
127
const auto now = micros ();
127
128
const auto itemRemaining = item->callNow .remaining ();
128
129
const int32_t remaining = rTarget - now;
129
- if (!rFirst || (remaining > 0 && static_cast <uint32_t >(remaining) > itemRemaining))
130
+ if (!rFirst || (remaining > 0 && static_cast <decltype ( micros ()) >(remaining) > itemRemaining))
130
131
{
131
132
rTarget = now + itemRemaining;
132
133
}
@@ -144,17 +145,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
144
145
return true ;
145
146
}
146
147
147
- uint32_t get_scheduled_recurrent_delay_us ()
148
+ decltype (micros()) get_scheduled_recurrent_delay_us()
148
149
{
149
- if (!rFirst) return ~ static_cast < decltype ( micros ())>( 0 ) >> 1 ;
150
+ if (!rFirst) return HALF_MAX_MICROS ;
150
151
// handle already expired rTarget.
151
152
const int32_t remaining = rTarget - micros ();
152
- return (remaining > 0 ) ? static_cast <uint32_t >(remaining) : 0 ;
153
+ return (remaining > 0 ) ? static_cast <decltype ( micros ()) >(remaining) : 0 ;
153
154
}
154
155
155
- uint32_t get_scheduled_delay_us ()
156
+ decltype (micros()) get_scheduled_delay_us()
156
157
{
157
- return sFirst ? 0 : ~ static_cast < decltype ( micros ())>( 0 ) >> 1 ;
158
+ return sFirst ? 0 : HALF_MAX_MICROS ;
158
159
}
159
160
160
161
void run_scheduled_functions ()
@@ -223,7 +224,7 @@ void run_scheduled_recurrent_functions()
223
224
224
225
// prevent scheduling of new functions during this run
225
226
stop = rLast;
226
- rTarget = micros () + (~ static_cast < decltype ( micros ())>( 0 ) >> 1 ) ;
227
+ rTarget = micros () + HALF_MAX_MICROS ;
227
228
228
229
do
229
230
{
@@ -262,7 +263,7 @@ void run_scheduled_recurrent_functions()
262
263
const auto now = micros ();
263
264
const auto currentRemaining = current->callNow .remaining ();
264
265
const int32_t remaining = rTarget - now;
265
- if (remaining > 0 && static_cast <uint32_t >(remaining) > currentRemaining)
266
+ if (remaining > 0 && static_cast <decltype ( micros ()) >(remaining) > currentRemaining)
266
267
{
267
268
rTarget = now + currentRemaining;
268
269
}
0 commit comments