8000 PolledTimeout Class for wrapping millis() loops (WIP) by devyte · Pull Request #5198 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

PolledTimeout Class for wrapping millis() loops (WIP) #5198

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

Merged
merged 16 commits into from
Nov 26, 2018
Merged
Changes from 1 commit
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
Next Next commit
PolledTimeout Class for wrapping millis() loops
  • Loading branch information
devyte committed Oct 2, 2018
commit f43c43c321c587d5117ef68fac41a7f6a44759d2
68 changes: 68 additions & 0 deletions cores/esp8266/PolledTimeout.h
902B
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef __POLLEDTIMING_H__
#define __POLLEDTIMING_H__

namespace esp8266
{

template<bool PeriodicT>
class polledTimeout
{
public:
using timeType = unsigned int;

polledTimeout(timeType timeout)
: _timeout(timeout), _start(millis())
{}

bool expired()
{
if(PeriodicT)
return expiredRetrigger();
return expiredOneShot();
}

operator bool()
{
return expired();
}

bool reset()
{
_start = millis();
}

protected:
bool checkExpired(timeType t) const
{
return (t - _start) >= _timeout;
}

bool expiredRetrigger()
{
timeType current = millis();
if(checkExpired(current))
{
unsigned int n = (current - _start) / _timeout; //how many _timeouts periods have elapsed, will usually be 1 (current - _start >= _timeout)
_start += n * _timeout;
return true;
}
return false;
}

bool expiredOneShot() const
{
return checkExpired(millis());
}

timeType _timeout;
timeType _start;
};


using polledTimeoutOneShot = polledTimeout<false>;
using polledTimeoutPeriodic = polledTimeout<true>;


}//esp8266

#endif
0