@@ -158,10 +158,10 @@ class timeoutTemplate
158158 IRAM_ATTR // fast
159159 bool expired ()
160160 {
161- YieldPolicyT::execute (); // in case of DoNothing: gets optimized away
162- if (PeriodicT) // in case of false : gets optimized away
163- return expiredRetrigger ();
164- return expiredOneShot () ;
161+ bool hasExpired = PeriodicT ? expiredRetrigger () : expiredOneShot ();
162+ if (!hasExpired) // in case of DoNothing : gets optimized away
163+ YieldPolicyT::execute ();
164+ return hasExpired ;
165165 }
166166
167167 IRAM_ATTR // fast
@@ -186,7 +186,7 @@ class timeoutTemplate
186186 {
187187 reset ();
188188 _timeout = TimePolicyT::toTimeTypeUnit (newUserTimeout);
189- _neverExpires = ( newUserTimeout < 0 ) || (newUserTimeout > timeMax () );
189+ _neverExpires = newUserTimeout > timeMax ();
190190 }
191191
192192 // Resets, will trigger after the timeout previously set.
@@ -219,11 +219,27 @@ class timeoutTemplate
219219 _neverExpires = true ;
220220 }
221221
222+ void stop ()
223+ {
224+ resetToNeverExpires ();
225+ }
226+
222227 timeType getTimeout () const
223228 {
224229 return TimePolicyT::toUserUnit (_timeout);
225230 }
226231
232+ IRAM_ATTR // fast
233+ timeType remaining () const
234+ {
235+ if (_neverExpires)
236+ return timeMax ();
237+ timeType current = TimePolicyT::time ();
238+ if (checkExpired (current))
239+ return TimePolicyT::toUserUnit (0 );
240+ return TimePolicyT::toUserUnit (_timeout - (current - _start));
241+ }
242+
227243 static constexpr timeType timeMax ()
228244 {
229245 return TimePolicyT::timeMax;
@@ -235,7 +251,7 @@ class timeoutTemplate
235251 bool checkExpired (const timeType internalUnit) const
236252 {
237253 // canWait() is not checked here
238- // returns "can expire" and "time expired"
254+ // returns "can expire" and "time has expired"
239255 return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
240256 }
241257
@@ -250,7 +266,7 @@ class timeoutTemplate
250266 timeType current = TimePolicyT::time ();
251267 if (checkExpired (current))
252268 {
253- unsigned long n = (current - _start) / _timeout; // how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
269+ timeType n = (current - _start) / _timeout; // how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
254270 _start += n * _timeout;
255271 return true ;
256272 }
0 commit comments