@@ -50,6 +50,8 @@ static recurrent_fn_t* rLast = nullptr;
5050// The target time for scheduling the next timed recurrent function
5151static decltype (micros()) rTarget;
5252
53+ constexpr decltype (micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0 ) >> 1;
54+
5355// Returns a pointer to an unused sched_fn_t,
5456// or if none are available allocates a new one,
5557// or nullptr if limit is reached
@@ -106,7 +108,7 @@ bool schedule_function(const std::function<void(void)>& fn)
106108
107109IRAM_ATTR // (not only) called from ISR
108110bool 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)
110112{
111113 assert (repeat_us < decltype (recurrent_fn_t ::callNow)::neverExpires); // ~26800000us (26.8s)
112114
@@ -126,7 +128,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126128 const auto now = micros ();
127129 const auto itemRemaining = item->callNow .remaining ();
128130 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))
130132 {
131133 rTarget = now + itemRemaining;
132134 }
@@ -144,17 +146,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
144146 return true ;
145147}
146148
147- uint32_t get_scheduled_recurrent_delay_us ()
149+ decltype (micros()) get_scheduled_recurrent_delay_us()
148150{
149- if (!rFirst) return ~ static_cast < decltype ( micros ())>( 0 ) >> 1 ;
151+ if (!rFirst) return HALF_MAX_MICROS ;
150152 // handle already expired rTarget.
151153 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 ;
153155}
154156
155- uint32_t get_scheduled_delay_us ()
157+ decltype (micros()) get_scheduled_delay_us()
156158{
157- return sFirst ? 0 : ~ static_cast < decltype ( micros ())>( 0 ) >> 1 ;
159+ return sFirst ? 0 : HALF_MAX_MICROS ;
158160}
159161
160162void run_scheduled_functions ()
@@ -223,7 +225,7 @@ void run_scheduled_recurrent_functions()
223225
224226 // prevent scheduling of new functions during this run
225227 stop = rLast;
226- rTarget = micros () + (~ static_cast < decltype ( micros ())>( 0 ) >> 1 ) ;
228+ rTarget = micros () + HALF_MAX_MICROS ;
227229
228230 do
229231 {
@@ -262,7 +264,7 @@ void run_scheduled_recurrent_functions()
262264 const auto now = micros ();
263265 const auto currentRemaining = current->callNow .remaining ();
264266 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)
266268 {
267269 rTarget = now + currentRemaining;
268270 }
0 commit comments