@@ -158,10 +158,10 @@ class timeoutTemplate
158
158
IRAM_ATTR // fast
159
159
bool expired ()
160
160
{
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 ;
165
165
}
166
166
167
167
IRAM_ATTR // fast
@@ -186,7 +186,7 @@ class timeoutTemplate
186
186
{
187
187
reset ();
188
188
_timeout = TimePolicyT::toTimeTypeUnit (newUserTimeout);
189
- _neverExpires = ( newUserTimeout < 0 ) || (newUserTimeout > timeMax () );
189
+ _neverExpires = newUserTimeout > timeMax ();
190
190
}
191
191
192
192
// Resets, will trigger after the timeout previously set.
@@ -219,11 +219,27 @@ class timeoutTemplate
219
219
_neverExpires = true ;
220
220
}
221
221
222
+ void stop ()
223
+ {
224
+ resetToNeverExpires ();
225
+ }
226
+
222
227
timeType getTimeout () const
223
228
{
224
229
return TimePolicyT::toUserUnit (_timeout);
225
230
}
226
231
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
+
227
243
static constexpr timeType timeMax ()
228
244
{
229
245
return TimePolicyT::timeMax;
@@ -235,7 +251,7 @@ class timeoutTemplate
235
251
bool checkExpired (const timeType internalUnit) const
236
252
{
237
253
// canWait() is not checked here
238
- // returns "can expire" and "time expired"
254
+ // returns "can expire" and "time has expired"
239
255
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
240
256
}
241
257
@@ -250,7 +266,7 @@ class timeoutTemplate
250
266
timeType current = TimePolicyT::time ();
251
267
if (checkExpired (current))
252
268
{
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)
254
270
_start += n * _timeout;
255
271
return true ;
256
272
}
0 commit comments