@@ -47,7 +47,8 @@ struct recurrent_fn_t
47
47
48
48
static recurrent_fn_t * rFirst = nullptr ;
49
49
static recurrent_fn_t * rLast = nullptr ;
50
- static uint32_t rTarget;
50
+ // The target time for scheduling the next timed recurrent function
51
+ static decltype (micros()) rTarget;
51
52
52
53
// Returns a pointer to an unused sched_fn_t,
53
54
// or if none are available allocates a new one,
@@ -122,10 +123,12 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
122
123
esp8266::InterruptLock lockAllInterruptsInThisScope;
123
124
124
125
// prevent new item overwriting an already expired rTarget.
125
- const int32_t remaining = rTarget - micros ();
126
- if (!rFirst || (remaining > 0 && static_cast <uint32_t >(remaining) > item->callNow .remaining ()))
126
+ const auto now = micros ();
127
+ const auto itemRemaining = item->callNow .remaining ();
128
+ const int32_t remaining = rTarget - now;
129
+ if (!rFirst || (remaining > 0 && static_cast <uint32_t >(remaining) > itemRemaining))
127
130
{
128
- rTarget = micros () + item-> callNow . remaining () ;
131
+ rTarget = now + itemRemaining ;
129
132
}
130
133
131
134
if (rLast)
@@ -218,13 +221,9 @@ void run_scheduled_recurrent_functions()
218
221
recurrent_fn_t * prev = nullptr ;
219
222
bool done;
220
223
221
- {
222
- esp8266::InterruptLock lockAllInterruptsInThisScope;
223
-
224
- // prevent scheduling of new functions during this run
225
- stop = rLast;
226
- rTarget = micros () + (~static_cast <decltype (micros ())>(0 ) >> 1 );
227
- }
224
+ // prevent scheduling of new functions during this run
225
+ stop = rLast;
226
+ rTarget = micros () + (~static_cast <decltype (micros ())>(0 ) >> 1 );
228
227
229
228
do
230
229
{
@@ -260,11 +259,14 @@ void run_scheduled_recurrent_functions()
260
259
esp8266::InterruptLock lockAllInterruptsInThisScope;
261
260
262
261
// prevent current item overwriting an already expired rTarget.
263
- const int32_t remaining = rTarget - micros ();
264
- if (remaining > 0 && static_cast <uint32_t >(remaining) > current->callNow .remaining ())
262
+ const auto now = micros ();
263
+ const auto currentRemaining = current->callNow .remaining ();
264
+ const int32_t remaining = rTarget - now;
265
+ if (remaining > 0 && static_cast <uint32_t >(remaining) > currentRemaining)
265
266
{
266
- rTarget = micros () + current-> callNow . remaining () ;
267
+ rTarget = now + currentRemaining ;
267
268
}
269
+
268
270
prev = current;
269
271
current = current->mNext ;
270
272
}
0 commit comments