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