@@ -47,7 +47,8 @@ struct recurrent_fn_t
4747
4848static recurrent_fn_t * rFirst = nullptr ;
4949static 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;
5152
5253// Returns a pointer to an unused sched_fn_t,
5354// or if none are available allocates a new one,
@@ -122,10 +123,12 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
122123 esp8266::InterruptLock lockAllInterruptsInThisScope;
123124
124125 // 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))
127130 {
128- rTarget = micros () + item-> callNow . remaining () ;
131+ rTarget = now + itemRemaining ;
129132 }
130133
131134 if (rLast)
@@ -218,13 +221,9 @@ void run_scheduled_recurrent_functions()
218221 recurrent_fn_t * prev = nullptr ;
219222 bool done;
220223
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 );
228227
229228 do
230229 {
@@ -260,11 +259,14 @@ void run_scheduled_recurrent_functions()
260259 esp8266::InterruptLock lockAllInterruptsInThisScope;
261260
262261 // 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)
265266 {
266- rTarget = micros () + current-> callNow . remaining () ;
267+ rTarget = now + currentRemaining ;
267268 }
269+
268270 prev = current;
269271 current = current->mNext ;
270272 }
0 commit comments