From 62bb52b4d230c1e60ed2b51fb6058c2602558676 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 7 Jun 2020 16:39:35 +0200 Subject: [PATCH 01/10] wip: brings polledTimeout to the same level as millis() --- cores/esp8266/Esp.h | 4 +++ cores/esp8266/PolledTimeout.h | 26 ++++++++++++++----- .../BlinkPolledTimeout/BlinkPolledTimeout.ino | 22 +++++++--------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index 4ad59e2237..fc0d1fb179 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -24,6 +24,10 @@ #include #include "spi_vendors.h" +// brings PolledTimeout at the same level as `millis()` +#include +using namespace esp8266::polledTimeout; + /** * AVR macros for WDT managment */ diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index 715564a8e7..f2ea1db89a 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -4,7 +4,7 @@ /* PolledTimeout.h - Encapsulation of a polled Timeout - + Copyright (c) 2018 Daniel Salazar. All rights reserved. This file is part of the esp8266 core for Arduino environment. @@ -161,13 +161,13 @@ class timeoutTemplate return expiredRetrigger(); return expiredOneShot(); } - + IRAM_ATTR // fast operator bool() { - return expired(); + return expired(); } - + bool canExpire () const { return !_neverExpires; @@ -192,6 +192,18 @@ class timeoutTemplate _start = TimePolicyT::time(); } + void resetAndSetExpired (const timeType newUserTimeout) + { + reset(newUserTimeout); + _start -= _timeout; + } + + void resetAndSetExpired () + { + reset(); + _start -= _timeout; + } + void resetToNeverExpires () { _timeout = alwaysExpired + 1; // because canWait() has precedence @@ -202,7 +214,7 @@ class timeoutTemplate { return TimePolicyT::toUserUnit(_timeout); } - + static constexpr timeType timeMax() { return TimePolicyT::timeMax; @@ -235,14 +247,14 @@ class timeoutTemplate } return false; } - + IRAM_ATTR // fast bool expiredOneShot() const { // returns "always expired" or "has expired" return !canWait() || checkExpired(TimePolicyT::time()); } - + timeType _timeout; timeType _start; bool _neverExpires; diff --git a/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino b/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino index 11e2aff482..a1ae86334c 100644 --- a/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino +++ b/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino @@ -23,8 +23,6 @@ */ -#include - void ledOn() { digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level } @@ -38,29 +36,27 @@ void ledToggle() { } -esp8266::polledTimeout::periodicFastUs halfPeriod(500000); //use fully qualified type and avoid importing all ::esp8266 namespace to the global namespace +periodicFastUs halfPeriod(500000); // the setup function runs only once at start void setup() { Serial.begin(115200); Serial.println(); - Serial.printf("periodic/oneShotMs::timeMax() = %u ms\n", (uint32_t)esp8266::polledTimeout::periodicMs::timeMax()); - Serial.printf("periodic/oneShotFastMs::timeMax() = %u ms\n", (uint32_t)esp8266::polledTimeout::periodicFastMs::timeMax()); - Serial.printf("periodic/oneShotFastUs::timeMax() = %u us\n", (uint32_t)esp8266::polledTimeout::periodicFastUs::timeMax()); - Serial.printf("periodic/oneShotFastNs::timeMax() = %u ns\n", (uint32_t)esp8266::polledTimeout::periodicFastNs::timeMax()); + Serial.printf("periodic/oneShotMs::timeMax() = %u ms\n", (uint32_t)periodicMs::timeMax()); + Serial.printf("periodic/oneShotFastMs::timeMax() = %u ms\n", (uint32_t)periodicFastMs::timeMax()); + Serial.printf("periodic/oneShotFastUs::timeMax() = %u us\n", (uint32_t)periodicFastUs::timeMax()); + Serial.printf("periodic/oneShotFastNs::timeMax() = %u ns\n", (uint32_t)periodicFastNs::timeMax()); #if 0 // 1 for debugging polledTimeout - Serial.printf("periodic/oneShotMs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicMs::rangeCompensate); - Serial.printf("periodic/oneShotFastMs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicFastMs::rangeCompensate); - Serial.printf("periodic/oneShotFastUs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicFastUs::rangeCompensate); - Serial.printf("periodic/oneShotFastNs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicFastNs::rangeCompensate); + Serial.printf("periodic/oneShotMs::rangeCompensate = %u\n", (uint32_t)periodicMs::rangeCompensate); + Serial.printf("periodic/oneShotFastMs::rangeCompensate = %u\n", (uint32_t)periodicFastMs::rangeCompensate); + Serial.printf("periodic/oneShotFastUs::rangeCompensate = %u\n", (uint32_t)periodicFastUs::rangeCompensate); + Serial.printf("periodic/oneShotFastNs::rangeCompensate = %u\n", (uint32_t)periodicFastNs::rangeCompensate); #endif pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output - using esp8266::polledTimeout::oneShotMs; //import the type to the local namespace - //STEP1; turn the led ON ledOn(); From 27f574386feccb6456e699e1a84c496dc7c07cb4 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 7 Jun 2020 16:49:51 +0200 Subject: [PATCH 02/10] move features to features.h --- cores/esp8266/Arduino.h | 5 ----- cores/esp8266/Esp.h | 7 +++++-- cores/esp8266/PolledTimeout.h | 14 +++++++------- cores/esp8266/core_esp8266_features.h | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index edb6bdd5c4..3ae44dc7cb 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -184,11 +184,6 @@ void analogWrite(uint8_t pin, int val); void analogWriteFreq(uint32_t freq); void analogWriteRange(uint32_t range); -unsigned long millis(void); -unsigned long micros(void); -uint64_t micros64(void); -void delay(unsigned long); -void delayMicroseconds(unsigned int us); unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index fc0d1fb179..d7ed22ba61 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -128,10 +128,13 @@ class EspClass { #if defined(F_CPU) || defined(CORE_MOCK) constexpr uint8_t getCpuFreqMHz() const { - return clockCyclesPerMicrosecond(); + return esp_get_cpu_freq_mhz(); } #else - uint8_t getCpuFreqMHz(); + uint8_t getCpuFreqMHz() const + { + return esp_get_cpu_freq_mhz(); + } #endif uint32_t getFlashChipId(); diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index f2ea1db89a..1df47871eb 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -25,7 +25,7 @@ #include -#include +#include namespace esp8266 { @@ -70,12 +70,12 @@ struct TimeSourceMillis struct TimeSourceCycles { - // time policy based on ESP.getCycleCount() + // time policy based on esp_get_cycle_count() // this particular time measurement is intended to be called very often // (every loop, every yield) - using timeType = decltype(ESP.getCycleCount()); - static timeType time() {return ESP.getCycleCount();} + using timeType = decltype(esp_get_cycle_count()); + static timeType time() {return esp_get_cycle_count();} static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz }; @@ -271,14 +271,14 @@ using periodic = polledTimeout::timeoutTemplate /*__attribute__((deprecate using oneShotMs = polledTimeout::timeoutTemplate; using periodicMs = polledTimeout::timeoutTemplate; -// Time policy based on ESP.getCycleCount(), and intended to be called very often: +// Time policy based on esp_get_cycle_count(), and intended to be called very often: // "Fast" versions sacrifices time range for improved precision and reduced execution time (by 86%) -// (cpu cycles for ::expired(): 372 (millis()) vs 52 (ESP.getCycleCount())) +// (cpu cycles for ::expired(): 372 (millis()) vs 52 (esp_get_cycle_count())) // timeMax() values: // Ms: max is 26843 ms (26.8 s) // Us: max is 26843545 us (26.8 s) // Ns: max is 1073741823 ns ( 1.07 s) -// (time policy based on ESP.getCycleCount() is intended to be called very often) +// (time policy based on esp_get_cycle_count() is intended to be called very often) using oneShotFastMs = polledTimeout::timeoutTemplate; using periodicFastMs = polledTimeout::timeoutTemplate; diff --git a/cores/esp8266/core_esp8266_features.h b/cores/esp8266/core_esp8266_features.h index d3b70f3dcc..e5d572d318 100644 --- a/cores/esp8266/core_esp8266_features.h +++ b/cores/esp8266/core_esp8266_features.h @@ -111,6 +111,29 @@ extern "C" { #endif void precache(void *f, uint32_t bytes); +unsigned long millis(void); +unsigned long micros(void); +uint64_t micros64(void); +void delay(unsigned long); +void delayMicroseconds(unsigned int us); + +#if defined(F_CPU) || defined(CORE_MOCK) +#ifdef __cplusplus +constexpr +#else +inline +#endif +int esp_get_cpu_freq_mhz() +{ + return F_CPU / 1000000L; +} +#else +inline int esp_get_cpu_freq_mhz() +{ + return system_get_cpu_freq(); +} +#endif + #ifdef __cplusplus } From 9cb4a55edd78311ab856a8ffb32e293bceba66b6 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 7 Jun 2020 16:52:43 +0200 Subject: [PATCH 03/10] ditto --- cores/esp8266/PolledTimeout.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index 1df47871eb..56e9cc9370 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -24,7 +24,7 @@ */ #include - +#include #include namespace esp8266 @@ -76,7 +76,7 @@ struct TimeSourceCycles using timeType = decltype(esp_get_cycle_count()); static timeType time() {return esp_get_cycle_count();} - static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz + static constexpr timeType ticksPerSecond = esp_get_cpu_freq_mhz() * 1000000UL; // 80'000'000 or 160'000'000 Hz static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz }; From 3c3cbea4e507a4d3a4b7b5ae55f9d10d474d15e2 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 7 Jun 2020 16:55:43 +0200 Subject: [PATCH 04/10] fix std:: dependencies --- cores/esp8266/PolledTimeout.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index 56e9cc9370..373df84ae6 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -23,8 +23,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include +#include // IRAM_ATTR +#include // std::numeric_limits +#include // std::is_unsigned #include namespace esp8266 From 096d2f293d873135d0e994f7e0f6147c37002f33 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 7 Jun 2020 17:04:55 +0200 Subject: [PATCH 05/10] fix emulation on host --- tests/host/common/MockEsp.cpp | 7 +++++- tests/host/common/mock.h | 47 +++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/tests/host/common/MockEsp.cpp b/tests/host/common/MockEsp.cpp index 0854bdcd7d..44c0cb01cc 100644 --- a/tests/host/common/MockEsp.cpp +++ b/tests/host/common/MockEsp.cpp @@ -203,7 +203,7 @@ uint32_t EspClass::getFlashChipSize(void) String EspClass::getFullVersion () { - return "host-emulation"; + return "emulation-on-host"; } uint32_t EspClass::getFreeContStack() @@ -216,6 +216,11 @@ void EspClass::resetFreeContStack() } uint32_t EspClass::getCycleCount() +{ + return esp_get_cycle_count(); +} + +uint32_t esp_get_cycle_count() { timeval t; gettimeofday(&t, NULL); diff --git a/tests/host/common/mock.h b/tests/host/common/mock.h index 27a2da4c42..1cf5821efd 100644 --- a/tests/host/common/mock.h +++ b/tests/host/common/mock.h @@ -31,6 +31,29 @@ #define CORE_MOCK 1 +// + +#define ARDUINO 267 +#define ESP8266 1 +#define A0 0 +#define LED_BUILTIN 0 +#define F_CPU 80000000 +#define LWIP_OPEN_SRC +#define TCP_MSS 536 +#define LWIP_FEATURES 1 + +// + +#define D0 0 +#define D1 1 +#define D2 3 +#define D3 3 +#define D4 4 +#define D5 5 +#define D6 6 +#define D7 7 +#define D8 8 + // include host's STL before any other include file // because core definition like max() is in the way @@ -61,28 +84,10 @@ typedef uint32_t uint32; // -#define ARDUINO 267 -#define ESP8266 1 -#define A0 0 -#define LED_BUILTIN 0 -#define F_CPU 80000000 -#define LWIP_OPEN_SRC -#define TCP_MSS 536 -#define LWIP_FEATURES 1 - -// - -#define D0 0 -#define D1 1 -#define D2 3 -#define D3 3 -#define D4 4 -#define D5 5 -#define D6 6 -#define D7 7 -#define D8 8 +#include +#include -// +uint32_t esp_get_cycle_count(); #include From 3053441cf52f214b1b2ffadb47d23a8cebedfe74 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 31 Jul 2020 00:35:39 +0200 Subject: [PATCH 06/10] comment out namespace import --- cores/esp8266/Esp.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index d7ed22ba61..be48f56fe6 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -24,9 +24,9 @@ #include #include "spi_vendors.h" -// brings PolledTimeout at the same level as `millis()` -#include -using namespace esp8266::polledTimeout; +// proposal: brings PolledTimeout at the same level as `millis()` +//#include +//using namespace esp8266::polledTimeout; /** * AVR macros for WDT managment From 109a11998e70b2ee13d37c55aec778369a265d53 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 31 Jul 2020 00:44:59 +0200 Subject: [PATCH 07/10] use a global define to experimentally enable global polledTimeout --- cores/esp8266/Esp.h | 7 ++++-- .../BlinkPolledTimeout/BlinkPolledTimeout.ino | 23 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index be48f56fe6..b3a4e63b38 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -24,9 +24,12 @@ #include #include "spi_vendors.h" +#if EXPERIMENTAL_POLLEDTIMEOUT_GLOBAL // proposal: brings PolledTimeout at the same level as `millis()` -//#include -//using namespace esp8266::polledTimeout; +// use global define EXPERIMENTAL_POLLEDTIMEOUT_GLOBAL=1 to enable +#include +using namespace esp8266::polledTimeout; +#endif /** * AVR macros for WDT managment diff --git a/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino b/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino index a1ae86334c..4d2c33d36a 100644 --- a/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino +++ b/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino @@ -22,6 +22,7 @@ Note that this sketch uses LED_BUILTIN to find the pin with the internal LED */ +#include void ledOn() { digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level @@ -36,23 +37,23 @@ void ledToggle() { } -periodicFastUs halfPeriod(500000); +esp8266::polledTimeout::periodicFastUs halfPeriod(500000); // the setup function runs only once at start void setup() { Serial.begin(115200); Serial.println(); - Serial.printf("periodic/oneShotMs::timeMax() = %u ms\n", (uint32_t)periodicMs::timeMax()); - Serial.printf("periodic/oneShotFastMs::timeMax() = %u ms\n", (uint32_t)periodicFastMs::timeMax()); - Serial.printf("periodic/oneShotFastUs::timeMax() = %u us\n", (uint32_t)periodicFastUs::timeMax()); - Serial.printf("periodic/oneShotFastNs::timeMax() = %u ns\n", (uint32_t)periodicFastNs::timeMax()); + Serial.printf("periodic/oneShotMs::timeMax() = %u ms\n", (uint32_t)esp8266::polledTimeout::periodicMs::timeMax()); + Serial.printf("periodic/oneShotFastMs::timeMax() = %u ms\n", (uint32_t)esp8266::polledTimeout::periodicFastMs::timeMax()); + Serial.printf("periodic/oneShotFastUs::timeMax() = %u us\n", (uint32_t)esp8266::polledTimeout::periodicFastUs::timeMax()); + Serial.printf("periodic/oneShotFastNs::timeMax() = %u ns\n", (uint32_t)esp8266::polledTimeout::periodicFastNs::timeMax()); #if 0 // 1 for debugging polledTimeout - Serial.printf("periodic/oneShotMs::rangeCompensate = %u\n", (uint32_t)periodicMs::rangeCompensate); - Serial.printf("periodic/oneShotFastMs::rangeCompensate = %u\n", (uint32_t)periodicFastMs::rangeCompensate); - Serial.printf("periodic/oneShotFastUs::rangeCompensate = %u\n", (uint32_t)periodicFastUs::rangeCompensate); - Serial.printf("periodic/oneShotFastNs::rangeCompensate = %u\n", (uint32_t)periodicFastNs::rangeCompensate); + Serial.printf("periodic/oneShotMs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicMs::rangeCompensate); + Serial.printf("periodic/oneShotFastMs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicFastMs::rangeCompensate); + Serial.printf("periodic/oneShotFastUs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicFastUs::rangeCompensate); + Serial.printf("periodic/oneShotFastNs::rangeCompensate = %u\n", (uint32_t)esp8266::polledTimeout::periodicFastNs::rangeCompensate); #endif pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output @@ -61,7 +62,7 @@ void setup() { ledOn(); //STEP2: wait for ON timeout - oneShotMs timeoutOn(2000); + esp8266::polledTimeout::oneShotMs timeoutOn(2000); while (!timeoutOn) { yield(); } @@ -70,7 +71,7 @@ void setup() { ledOff(); //STEP4: wait for OFF timeout to assure the led is kept off for this time before exiting setup - oneShotMs timeoutOff(2000); + esp8266::polledTimeout::oneShotMs timeoutOff(2000); while (!timeoutOff) { yield(); } From a24e8948f5744e8397080b4d8453a51a74bad63b Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 15 Aug 2020 17:19:58 +0200 Subject: [PATCH 08/10] remove experimental global polledtimeout classes --- cores/esp8266/Esp.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index b3a4e63b38..b3e187fc52 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -24,13 +24,6 @@ #include #include "spi_vendors.h" -#if EXPERIMENTAL_POLLEDTIMEOUT_GLOBAL -// proposal: brings PolledTimeout at the same level as `millis()` -// use global define EXPERIMENTAL_POLLEDTIMEOUT_GLOBAL=1 to enable -#include -using namespace esp8266::polledTimeout; -#endif - /** * AVR macros for WDT managment */ From 16e9c66bd1e8714d0fe2b891080ac1d2984c2948 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 15 Aug 2020 17:25:54 +0200 Subject: [PATCH 09/10] api explanation --- cores/esp8266/PolledTimeout.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index 373df84ae6..7eec547e48 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -179,6 +179,7 @@ class timeoutTemplate return _timeout != alwaysExpired; } + // Resets, will trigger after this new timeout. IRAM_ATTR // called from ISR void reset(const timeType newUserTimeout) { @@ -187,18 +188,24 @@ class timeoutTemplate _neverExpires = (newUserTimeout < 0) || (newUserTimeout > timeMax()); } + // Resets, will trigger after the timeout previously set. IRAM_ATTR // called from ISR void reset() { _start = TimePolicyT::time(); } + // Resets to just expired so that on next poll the check will immediately trigger for the user, + // also change timeout (after next immediate trigger). + IRAM_ATTR // called from ISR void resetAndSetExpired (const timeType newUserTimeout) { reset(newUserTimeout); _start -= _timeout; } + // Resets to just expired so that on next poll the check will immediately trigger for the user. + IRAM_ATTR // called from ISR void resetAndSetExpired () { reset(); From d11e4f907dba8a13f5610d212b667fd3314098e4 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 15 Aug 2020 17:30:31 +0200 Subject: [PATCH 10/10] restore original BlinkPolledTimeout.ino --- .../examples/BlinkPolledTimeout/BlinkPolledTimeout.ino | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino b/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino index 4d2c33d36a..11e2aff482 100644 --- a/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino +++ b/libraries/esp8266/examples/BlinkPolledTimeout/BlinkPolledTimeout.ino @@ -22,6 +22,7 @@ Note that this sketch uses LED_BUILTIN to find the pin with the internal LED */ + #include void ledOn() { @@ -37,7 +38,7 @@ void ledToggle() { } -esp8266::polledTimeout::periodicFastUs halfPeriod(500000); +esp8266::polledTimeout::periodicFastUs halfPeriod(500000); //use fully qualified type and avoid importing all ::esp8266 namespace to the global namespace // the setup function runs only once at start void setup() { @@ -58,11 +59,13 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output + using esp8266::polledTimeout::oneShotMs; //import the type to the local namespace + //STEP1; turn the led ON ledOn(); //STEP2: wait for ON timeout - esp8266::polledTimeout::oneShotMs timeoutOn(2000); + oneShotMs timeoutOn(2000); while (!timeoutOn) { yield(); } @@ -71,7 +74,7 @@ void setup() { ledOff(); //STEP4: wait for OFF timeout to assure the led is kept off for this time before exiting setup - esp8266::polledTimeout::oneShotMs timeoutOff(2000); + oneShotMs timeoutOff(2000); while (!timeoutOff) { yield(); }