2121#include  " Schedule.h" 
2222#include  " PolledTimeout.h" 
2323#include  " interrupts.h" 
24- #include  " coredecls.h" 
2524
2625typedef  std::function<void (void )> mSchedFuncT ;
2726struct  scheduled_fn_t 
@@ -50,6 +49,8 @@ static recurrent_fn_t* rLast = nullptr;
5049//  The target time for scheduling the next timed recurrent function 
5150static  decltype (micros()) rTarget;
5251
52+ constexpr  decltype (micros()) HALF_MAX_MICROS = ~static_cast<decltype(micros())>(0 ) >> 1;
53+ 
5354//  Returns a pointer to an unused sched_fn_t,
5455//  or if none are available allocates a new one,
5556//  or nullptr if limit is reached
@@ -106,7 +107,7 @@ bool schedule_function(const std::function<void(void)>& fn)
106107
107108IRAM_ATTR //  (not only) called from ISR
108109bool  schedule_recurrent_function_us (const  std::function<bool (void )>& fn,
109-     uint32_t  repeat_us, const  std::function<bool(void )>& alarm)
110+     decltype(micros())  repeat_us, const  std::function<bool(void )>& alarm)
110111{
111112    assert (repeat_us < decltype (recurrent_fn_t ::callNow)::neverExpires); // ~26800000us (26.8s)
112113
@@ -126,7 +127,7 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
126127    const  auto  now = micros ();
127128    const  auto  itemRemaining = item->callNow .remaining ();
128129    const  int32_t  remaining = rTarget - now;
129-     if  (!rFirst || (remaining > 0  && static_cast <uint32_t >(remaining) > itemRemaining))
130+     if  (!rFirst || (remaining > 0  && static_cast <decltype ( micros ()) >(remaining) > itemRemaining))
130131    {
131132        rTarget = now + itemRemaining;
132133    }
@@ -144,17 +145,17 @@ bool schedule_recurrent_function_us(const std::function<bool(void)>& fn,
144145    return  true ;
145146}
146147
147- uint32_t  get_scheduled_recurrent_delay_us ()
148+ decltype (micros())  get_scheduled_recurrent_delay_us()
148149{
149-     if  (!rFirst) return  ~ static_cast < decltype ( micros ())>( 0 ) >>  1 ;
150+     if  (!rFirst) return  HALF_MAX_MICROS ;
150151    //  handle already expired rTarget.
151152    const  int32_t  remaining = rTarget - micros ();
152-     return  (remaining > 0 ) ? static_cast <uint32_t >(remaining) : 0 ;
153+     return  (remaining > 0 ) ? static_cast <decltype ( micros ()) >(remaining) : 0 ;
153154}
154155
155- uint32_t  get_scheduled_delay_us ()
156+ decltype (micros())  get_scheduled_delay_us()
156157{
157-     return  sFirst  ? 0  : ~ static_cast < decltype ( micros ())>( 0 ) >>  1 ;
158+     return  sFirst  ? 0  : HALF_MAX_MICROS ;
158159}
159160
160161void  run_scheduled_functions ()
@@ -223,7 +224,7 @@ void run_scheduled_recurrent_functions()
223224
224225    //  prevent scheduling of new functions during this run
225226    stop = rLast;
226-     rTarget = micros () + (~ static_cast < decltype ( micros ())>( 0 ) >>  1 ) ;
227+     rTarget = micros () + HALF_MAX_MICROS ;
227228
228229    do 
229230    {
@@ -262,7 +263,7 @@ void run_scheduled_recurrent_functions()
262263            const  auto  now = micros ();
263264            const  auto  currentRemaining = current->callNow .remaining ();
264265            const  int32_t  remaining = rTarget - now;
265-             if  (remaining > 0  && static_cast <uint32_t >(remaining) > currentRemaining)
266+             if  (remaining > 0  && static_cast <decltype ( micros ()) >(remaining) > currentRemaining)
266267            {
267268                rTarget = now + currentRemaining;
268269            }
0 commit comments