Skip to content

Commit 1f5b6a1

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

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
@@ -223,6 +223,14 @@ class timeoutTemplate
223223
return TimePolicyT::toUserUnit(_timeout);
224224
}
225225

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

233241
IRAM_ATTR // fast
234-
bool checkExpired(const timeType internalUnit) const
242+
bool checkExpired() const
235243
{
236244
// canWait() is not checked here
237245
// returns "can expire" and "time expired"
238-
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
246+
return (!_neverExpires) && ((_current - _start) >= _timeout);
239247
}
240248

241249
protected:
@@ -246,25 +254,27 @@ class timeoutTemplate
246254
if (!canWait())
247255
return true;
248256

249-
timeType current = TimePolicyT::time();
250-
if(checkExpired(current))
257+
_current = TimePolicyT::time();
258+
if(checkExpired())
251259
{
252-
unsigned long n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
260+
unsigned long n = (_current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (_current - _start >= _timeout)
253261
_start += n * _timeout;
254262
return true;
255263
}
256264
return false;
257265
}
258266

259267
IRAM_ATTR // fast
260-
bool expiredOneShot() const
268+
bool expiredOneShot()
261269
{
270+
_current = TimePolicyT::time();
262271
// returns "always expired" or "has expired"
263-
return !canWait() || checkExpired(TimePolicyT::time());
272+
return !canWait() || checkExpired();
264273
}
265274

266275
timeType _timeout;
267276
timeType _start;
277+
timeType _current;
268278
bool _neverExpires;
269279
};
270280

0 commit comments

Comments
 (0)