Skip to content

Commit 2bcdf18

Browse files
committed
Add function to retrieve the time remaining until the (next) timeout occurs.
1 parent 7248808 commit 2bcdf18

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

cores/esp8266/PolledTimeout.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ class timeoutTemplate
224224
return TimePolicyT::toUserUnit(_timeout);
225225
}
226226

227+
IRAM_ATTR // fast
228+
timeType expiresIn()
229+
{
230+
if (_neverExpires) return timeMax();
231+
if (expired()) return TimePolicyT::toUserUnit(0);
232+
return TimePolicyT::toUserUnit(_timeout - (_current - _start));
233+
}
234+
227235
static constexpr timeType timeMax()
228236
{
229237
return TimePolicyT::timeMax;
@@ -232,11 +240,11 @@ class timeoutTemplate
232240
private:
233241

234242
IRAM_ATTR // fast
235-
bool checkExpired(const timeType internalUnit) const
243+
bool checkExpired() const
236244
{
237245
// canWait() is not checked here
238246
// returns "can expire" and "time expired"
239-
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
247+
return (!_neverExpires) && ((_current - _start) >= _timeout);
240248
}
241249

242250
protected:
@@ -247,25 +255,27 @@ class timeoutTemplate
247255
if (!canWait())
248256
return true;
249257

250-
timeType current = TimePolicyT::time();
251-
if(checkExpired(current))
258+
_current = TimePolicyT::time();
259+
if(checkExpired())
252260
{
253-
unsigned long n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
261+
unsigned long n = (_current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (_current - _start >= _timeout)
254262
_start += n * _timeout;
255263
return true;
256264
}
257265
return false;
258266
}
259267

260268
IRAM_ATTR // fast
261-
bool expiredOneShot() const
269+
bool expiredOneShot()
262270
{
271+
_current = TimePolicyT::time();
263272
// returns "always expired" or "has expired"
264-
return !canWait() || checkExpired(TimePolicyT::time());
273+
return !canWait() || checkExpired();
265274
}
266275

267276
timeType _timeout;
268277
timeType _start;
278+
timeType _current;
269279
bool _neverExpires;
270280
};
271281

0 commit comments

Comments
 (0)