From 7f0e23b728ea14e209a2ff8986472a4983c697f6 Mon Sep 17 00:00:00 2001 From: Embedded Innovation Date: Tue, 8 Nov 2016 20:06:53 +0100 Subject: [PATCH] Finish LED pattern before low-power sleep Wait until no LEDs are blinking anymore before going to sleep. Decrement LED blink counter before acting on the value, otherwise LED state will always lag behind. --- core/MyLeds.cpp | 60 ++++++++++++++++++++++-------------------- core/MyLeds.h | 5 ++++ core/MySensorsCore.cpp | 7 +++++ 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/core/MyLeds.cpp b/core/MyLeds.cpp index bbbcb66..0da6628 100644 --- a/core/MyLeds.cpp +++ b/core/MyLeds.cpp @@ -36,17 +36,17 @@ inline void ledsInit() countErr = 0; // Setup led pins -#if defined(MY_DEFAULT_RX_LED_PIN) - pinMode(MY_DEFAULT_RX_LED_PIN, OUTPUT); -#endif -#if defined(MY_DEFAULT_TX_LED_PIN) - pinMode(MY_DEFAULT_TX_LED_PIN, OUTPUT); -#endif -#if defined(MY_DEFAULT_ERR_LED_PIN) - pinMode(MY_DEFAULT_ERR_LED_PIN, OUTPUT); -#endif + #if defined(MY_DEFAULT_RX_LED_PIN) + pinMode(MY_DEFAULT_RX_LED_PIN, OUTPUT); + #endif + #if defined(MY_DEFAULT_TX_LED_PIN) + pinMode(MY_DEFAULT_TX_LED_PIN, OUTPUT); + #endif + #if defined(MY_DEFAULT_ERR_LED_PIN) + pinMode(MY_DEFAULT_ERR_LED_PIN, OUTPUT); + #endif prevTime = hwMillis() - LED_PROCESS_INTERVAL_MS; // Substract some, to make sure leds gets updated on first run. - ledsProcess(); + ledsProcess(); } void ledsProcess() { @@ -56,33 +56,33 @@ void ledsProcess() { } prevTime = hwMillis(); - uint8_t state; + uint8_t state; - // For an On/Off ratio of 4, the pattern repeated will be [on, on, on, off] - // until the counter becomes 0. -#if defined(MY_DEFAULT_RX_LED_PIN) - state = (countRx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF; - hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, state); - if (countRx) { + // For an On/Off ratio of 4, the pattern repeated will be [on, on, on, off] + // until the counter becomes 0. + #if defined(MY_DEFAULT_RX_LED_PIN) + if (countRx) { --countRx; } -#endif + state = (countRx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF; + hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, state); + #endif -#if defined(MY_DEFAULT_TX_LED_PIN) - state = (countTx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF; - hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, state); - if (countTx) { + #if defined(MY_DEFAULT_TX_LED_PIN) + if (countTx) { --countTx; } -#endif + state = (countTx & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF; + hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, state); + #endif -#if defined(MY_DEFAULT_ERR_LED_PIN) - state = (countErr & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF; - hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, state); - if (countErr) { + #if defined(MY_DEFAULT_ERR_LED_PIN) + if (countErr) { --countErr; } -#endif + state = (countErr & (LED_ON_OFF_RATIO-1)) ? LED_ON : LED_OFF; + hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, state); + #endif } void ledsBlinkRx(uint8_t cnt) { @@ -105,3 +105,7 @@ void ledsBlinkErr(uint8_t cnt) { } ledsProcess(); } + +bool ledsBlinking() { + return countRx || countTx || countErr; +} diff --git a/core/MyLeds.h b/core/MyLeds.h index e6f982d..e0fb121 100644 --- a/core/MyLeds.h +++ b/core/MyLeds.h @@ -43,6 +43,11 @@ void ledsBlinkTx(uint8_t cnt); void ledsBlinkErr(uint8_t cnt); void ledsProcess(); // do the actual blinking + /** + * Test if any LED is currently blinking. + * @return true when one or more LEDs are blinking, false otherwise. + */ + bool ledsBlinking(); #else // Remove led functions if feature is disabled diff --git a/core/MySensorsCore.cpp b/core/MySensorsCore.cpp index 9f4170c..0c87a84 100644 --- a/core/MySensorsCore.cpp +++ b/core/MySensorsCore.cpp @@ -520,6 +520,13 @@ int8_t _sleep(const uint32_t sleepingMS, const bool smartSleep, const uint8_t in transportPowerDown(); #endif + #if defined (MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN) + // Wait until leds finish their blinking pattern + while (ledsBlinking()) { + doYield(); + } + #endif + setIndication(INDICATION_SLEEP); int8_t result = MY_SLEEP_NOT_POSSIBLE; // default