Skip to content

[BREAKING] polledTimeout: new remaining() returns time units until (next) expiration #6843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions cores/esp8266/PolledTimeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class timeoutTemplate
using timeType = typename TimePolicyT::timeType;
static_assert(std::is_unsigned<timeType>::value == true, "timeType must be unsigned");

static constexpr timeType alwaysExpired = 0;
static constexpr timeType neverExpires = std::numeric_limits<timeType>::max();
static constexpr timeType rangeCompensate = TimePolicyT::rangeCompensate; //debug

Expand All @@ -157,10 +156,10 @@ class timeoutTemplate
IRAM_ATTR // fast
bool expired()
{
YieldPolicyT::execute(); //in case of DoNothing: gets optimized away
if(PeriodicT) //in case of false: gets optimized away
return expiredRetrigger();
return expiredOneShot();
bool hasExpired = PeriodicT ? expiredRetrigger() : expiredOneShot();
if (!hasExpired) //in case of DoNothing: gets optimized away
YieldPolicyT::execute();
return hasExpired;
}

IRAM_ATTR // fast
Expand All @@ -176,7 +175,7 @@ class timeoutTemplate

bool canWait () const
{
return _timeout != alwaysExpired;
return _timeout != 0;
}

// Resets, will trigger after this new timeout.
Expand All @@ -185,7 +184,7 @@ class timeoutTemplate
{
reset();
_timeout = TimePolicyT::toTimeTypeUnit(newUserTimeout);
_neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax());
_neverExpires = newUserTimeout > timeMax();
}

// Resets, will trigger after the timeout previously set.
Expand Down Expand Up @@ -214,15 +213,31 @@ class timeoutTemplate

void resetToNeverExpires ()
{
_timeout = alwaysExpired + 1; // because canWait() has precedence
_timeout = 1; // because canWait() has precedence
_neverExpires = true;
}

void stop()
{
resetToNeverExpires();
}

timeType getTimeout() const
{
return TimePolicyT::toUserUnit(_timeout);
}

IRAM_ATTR // fast
timeType remaining() const
{
if (_neverExpires)
return timeMax();
timeType current = TimePolicyT::time();
if (checkExpired(current))
return TimePolicyT::toUserUnit(0);
return TimePolicyT::toUserUnit(_timeout - (current - _start));
}

static constexpr timeType timeMax()
{
return TimePolicyT::timeMax;
Expand All @@ -234,7 +249,7 @@ class timeoutTemplate
bool checkExpired(const timeType internalUnit) const
{
// canWait() is not checked here
// returns "can expire" and "time expired"
// returns "can expire" and "time has expired"
return (!_neverExpires) && ((internalUnit - _start) >= _timeout);
}

Expand All @@ -249,7 +264,7 @@ class timeoutTemplate
timeType current = TimePolicyT::time();
if(checkExpired(current))
{
unsigned long n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
timeType n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
_start += n * _timeout;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Stream: public Print {

// transfers already buffered / immediately available data (no timeout)
// returns number of transfered bytes
size_t sendAvailable (Print* to) { return sendGeneric(to, -1, -1, oneShotMs::alwaysExpired); }
size_t sendAvailable (Print* to) { return sendGeneric(to, -1, -1, 0); }
size_t sendAvailable (Print& to) { return sendAvailable(&to); }

// transfers data until timeout
Expand Down