Skip to content

Commit 8518563

Browse files
committed
Simplified roll-over logic
1 parent 71c085d commit 8518563

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

cores/esp8266/Schedule.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126126
// prevent new item overwriting an already expired rTarget.
127127
const auto now = micros();
128128
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))
131131
{
132132
rTarget = now + itemRemaining;
133133
}
@@ -149,8 +149,9 @@ decltype(micros()) get_scheduled_recurrent_delay_us()
149149
{
150150
if (!rFirst) return HALF_MAX_MICROS;
151151
// 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;
154155
}
155156

156157
decltype(micros()) get_scheduled_delay_us()
@@ -262,8 +263,8 @@ void run_scheduled_recurrent_functions()
262263
// prevent current item overwriting an already expired rTarget.
263264
const auto now = micros();
264265
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)
267268
{
268269
rTarget = now + currentRemaining;
269270
}

0 commit comments

Comments
 (0)