@@ -126,8 +126,8 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126
126
// prevent new item overwriting an already expired rTarget.
127
127
const auto now = micros ();
128
128
const auto itemRemaining = item->callNow .remaining ();
129
- const int32_t remaining = rTarget - now;
130
- if (!rFirst || (remaining > 0 && static_cast < decltype ( micros ())>( remaining) > itemRemaining))
129
+ const auto remaining = rTarget - now;
130
+ if (!rFirst || (remaining <= HALF_MAX_MICROS && remaining > itemRemaining))
131
131
{
132
132
rTarget = now + itemRemaining;
133
133
}
@@ -149,8 +149,9 @@ decltype(micros()) get_scheduled_recurrent_delay_us()
149
149
{
150
150
if (!rFirst) return HALF_MAX_MICROS;
151
151
// handle already expired rTarget.
152
- const int32_t remaining = rTarget - micros ();
153
- return (remaining > 0 ) ? static_cast <decltype (micros ())>(remaining) : 0 ;
152
+ const auto now = micros ();
153
+ const auto remaining = rTarget - now;
154
+ return (remaining <= HALF_MAX_MICROS) ? remaining : 0 ;
154
155
}
155
156
156
157
decltype (micros()) get_scheduled_delay_us()
@@ -262,8 +263,8 @@ void run_scheduled_recurrent_functions()
262
263
// prevent current item overwriting an already expired rTarget.
263
264
const auto now = micros ();
264
265
const auto currentRemaining = current->callNow .remaining ();
265
- const int32_t remaining = rTarget - now;
266
- if (remaining > 0 && static_cast < decltype ( micros ())>( remaining) > currentRemaining)
266
+ const auto remaining = rTarget - now;
267
+ if (remaining <= HALF_MAX_MICROS && remaining > currentRemaining)
267
268
{
268
269
rTarget = now + currentRemaining;
269
270
}
0 commit comments