Skip to content

polledTimeout: add option to use CPU count instead of millis() #5870

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 47 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
fca1d5d
polledTimeout: add option to use CPU count instead of millis()
d-a-v Mar 13, 2019
6cd506b
Merge branch 'master' into polledcpu
d-a-v Mar 13, 2019
702cea8
use more "using" alias
d-a-v Mar 13, 2019
2afa570
Merge branch 'master' into polledcpu
d-a-v Mar 14, 2019
133c06f
more c++/clear code, using typename (thanks @devyte)
d-a-v Mar 14, 2019
d675eeb
rename class name to include unit, introduce timeMax() and check it w…
d-a-v Mar 15, 2019
f3f7a2d
remove useless defines
d-a-v Mar 15, 2019
a21514a
improve api readability, add micro-second unit
d-a-v Mar 15, 2019
9b8e40b
update example
d-a-v Mar 15, 2019
ba105b7
mock: emulate getCycleCount, add/fix polledTimeout CI test
d-a-v Mar 18, 2019
02909f8
Merge branch 'master' into polledcpu
d-a-v Mar 18, 2019
d1785c6
Merge branch 'master' into polledcpu
d-a-v Mar 18, 2019
ebda468
+ nano-seconds, assert -> message, comments, host test
d-a-v Mar 19, 2019
7a4c4de
allow 0 for timeout (enables immediate timeout, fix division by 0)
d-a-v Mar 19, 2019
2249315
Merge branch 'master' into polledcpu
d-a-v Mar 19, 2019
aa6b2d9
typo, set member instead of local variable
d-a-v Mar 19, 2019
e98c41c
unify error message
d-a-v Mar 19, 2019
714beb3
slight change on checkExpired() allows "never expired"
d-a-v Mar 19, 2019
925408e
remove traces of debug.h/cpp in this PR
d-a-v Mar 19, 2019
412a241
include missing <limits> header
d-a-v Mar 20, 2019
273306a
back to original expired test, introduce boolean _neverExpires, fix r…
d-a-v Mar 20, 2019
531e08e
fix expiredOneShot with _timeout==0 check
d-a-v Mar 20, 2019
a041a66
reenable getTimeout()
d-a-v Mar 20, 2019
70ac794
expose checkExpired with unit conversion
d-a-v Mar 20, 2019
e25b880
fix timing comments, move critical code to iram
d-a-v Mar 20, 2019
a20f8f6
add member ::neverExpires and use it where relevant
d-a-v Mar 20, 2019
8d4c385
improve clarity
d-a-v Mar 20, 2019
ce7e9b9
remove exposed checkExpired(), adapt LEAmDNS with equivalent
d-a-v Mar 20, 2019
0ca22ce
add API ::resetToNeverExpires(), use it in LEAmDNS
d-a-v Mar 20, 2019
3d4b369
Merge branch 'master' into polledcpu
d-a-v Mar 20, 2019
ff660ae
remove offending constness from ::flagged() LEAmDNS (due do API fix i…
d-a-v Mar 21, 2019
393d40f
simplify "Fast" base classes
d-a-v Mar 21, 2019
1f4f73c
Merge branch 'master' into polledcpu
d-a-v Mar 25, 2019
daa5e85
Merge branch 'polledcpu' of github.com:d-a-v/Arduino into polledcpu
d-a-v Mar 25, 2019
96c7fb7
minor variable rename
d-a-v Mar 25, 2019
45c5da2
Merge branch 'master' into polledcpu
d-a-v Mar 28, 2019
4930ed0
Fix examples
d-a-v Mar 28, 2019
f11c6e6
compliance with good c++ manners
d-a-v Apr 2, 2019
9d73d18
Merge branch 'master' into polledcpu
d-a-v Apr 2, 2019
f971d42
minor changes for consistency
d-a-v Apr 2, 2019
b028ea8
add missing const
d-a-v Apr 2, 2019
a02f889
expired() and bool() moved to iram
d-a-v Apr 2, 2019
40d31a5
constexpr compensation computing
d-a-v Apr 2, 2019
75f36a7
add/update comments
d-a-v Apr 3, 2019
0e72db2
Merge branch 'master' into polledcpu
d-a-v Apr 4, 2019
4470ca2
Merge branch 'polledcpu' of github.com:d-a-v/Arduino into polledcpu
d-a-v Apr 4, 2019
0969fee
move neverExpires and alwaysExpired
d-a-v Apr 4, 2019
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
58 changes: 51 additions & 7 deletions cores/esp8266/PolledTimeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include <Arduino.h>
#include <assert.h>

namespace esp8266
{
Expand All @@ -47,16 +48,57 @@ struct YieldOrSkip

} //YieldPolicy

namespace TimePolicy
{

struct TimeMillis
{
// time policy in milli-seconds based on millis()

using timeType = decltype(millis());
static timeType time() {return millis();}
static constexpr timeType toMillis = 1;

template <bool PeriodicT, typename YieldPolicyT = YieldPolicy::DoNothing>
// rollover: 48 days, setting max to 24 days to fit in signed scalar (below is = 2^31 - 1)
static constexpr timeType timeMax() { return (((((timeType)1) << ((sizeof(timeType) * 8) - 2)) - 1) << 1) + 1; }
};

#ifdef CORE_MOCK
struct TimeCycleMs: public TimeMillis {};
#else

struct TimeCycleMs
{
// time policy in milli-seconds based on ESP.getCycleCount()

using timeType = decltype(ESP.getCycleCount());
static timeType time() {return ESP.getCycleCount();}
static constexpr timeType toMillis = F_CPU / 1000;

// rollover: @80Mhz:53.6s @160Mhz:26.8s
// setting max to half of it to ensure full range is never reached
// - this particular time measurement is intended to be called very often
// (every loop, every yield)
// - this max is larger than internal watchdogs
static constexpr timeType timeMax() { return ((((uint64_t)1000) << (sizeof(timeType) * 8)) / F_CPU) / 2; }
};

#endif // cpu cycles


} //TimePolicy

template <bool PeriodicT, typename YieldPolicyT = YieldPolicy::DoNothing, typename TimePolicyT = TimePolicy::TimeMillis>
class timeoutTemplate
{
public:
using timeType = decltype(millis());
using timeType = typename TimePolicyT::timeType;

timeoutTemplate(timeType timeout)
: _timeout(timeout), _start(millis())
{}
: _timeout(timeout * TimePolicyT::toMillis), _start(TimePolicyT::time())
{
assert(timeout < TimePolicyT::timeMax());
}

bool expired()
{
Expand All @@ -79,7 +121,7 @@ class timeoutTemplate

void reset()
{
_start = millis();
_start = TimePolicyT::time();
}

timeType getTimeout() const
Expand All @@ -96,7 +138,7 @@ class timeoutTemplate

bool expiredRetrigger()
{
timeType current = millis();
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)
Expand All @@ -108,7 +150,7 @@ class timeoutTemplate

bool expiredOneShot() const
{
return checkExpired(millis());
return checkExpired(TimePolicyT::time());
}

timeType _timeout;
Expand All @@ -117,6 +159,8 @@ class timeoutTemplate

using oneShot = polledTimeout::timeoutTemplate<false>;
using periodic = polledTimeout::timeoutTemplate<true>;
using oneShotCycleMs = polledTimeout::timeoutTemplate<false, YieldPolicy::DoNothing, TimePolicy::TimeCycleMs>;
using periodicCycleMs = polledTimeout::timeoutTemplate<true, YieldPolicy::DoNothing, TimePolicy::TimeCycleMs>;

} //polledTimeout

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ void ledToggle() {
}



esp8266::polledTimeout::periodic halfPeriod(500); //use fully qualified type and avoid importing all ::esp8266 namespace to the global namespace

// also available: lighter esp8266::polledTimeout::periodicCycleMs for durations shorter than 13000 ms

// the setup function runs only once at start
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
Expand Down