fix LED flashing

This commit is contained in:
proddy
2026-08-05 15:43:40 +02:00
parent 3a81c70018
commit 1956ff8386
+58 -42
View File
@@ -16,16 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "led.h" #include "led.h"
#include "emsesp.h" #include "emsesp.h"
namespace emsesp { namespace emsesp {
uuid::log::Logger LED::logger_{F_(led), uuid::log::Facility::KERN}; uuid::log::Logger LED::logger_{F_(led), uuid::log::Facility::KERN};
// initialise the LED, fetching the settings from the WebSettingsService // initialise the LED, fetching the settings from the WebSettingsService
// set the LED to on or off when in normal operating mode // set the LED to on or off when in normal operating mode
void LED::init() { void LED::init() {
// copy the application settings // copy the application settings
EMSESP::webSettingsService.read([&](WebSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
led_gpio_ = settings.led_gpio; led_gpio_ = settings.led_gpio;
@@ -44,12 +44,12 @@ void LED::init() {
} }
reset_led(); // start with LED in default state, depending on if it's hidden or not reset_led(); // start with LED in default state, depending on if it's hidden or not
} }
// handle LED routine // handle LED routine
// called from the System::loop() // called from the System::loop()
// returns true if the LED flash is active, i.e its a lock down state // returns true if the LED flash is active, i.e its a lock down state
bool LED::loop(uint8_t healthcheck, bool button_busy) { bool LED::loop(uint8_t healthcheck, bool button_busy) {
// if LED flashing is active it means its about to perform a factory reset, so don't do anything else and keep it flashing // if LED flashing is active it means its about to perform a factory reset, so don't do anything else and keep it flashing
if (led_fast_flash_timer_) { if (led_fast_flash_timer_) {
led_fast_flash(); led_fast_flash();
@@ -59,11 +59,16 @@ bool LED::loop(uint8_t healthcheck, bool button_busy) {
// the user-requested LED blink always has preference // the user-requested LED blink always has preference
if (!is_user_led_blink_) { if (!is_user_led_blink_) {
// check for button press. // check for button press.
// if button is pressed, show LED (yellow on RGB LED, on/off on standard LED) // while the button is held show the LED (yellow on RGB LED, on/off on standard LED),
// it will turn off on the next loop cycle // and on release go back to the default state and re-check the health on the next cycle
if (last_button_busy_ != button_busy) { if (last_button_busy_ != button_busy) {
last_button_busy_ = button_busy; last_button_busy_ = button_busy;
set_led(button_busy ? Color::OFF : Color::YELLOW); // Yellow if (button_busy) {
set_led(Color::YELLOW); // Yellow
} else {
reset_led();
previous_healthcheck_ = System::HEALTHCHECK_RESET;
}
return false; return false;
} }
@@ -104,25 +109,36 @@ bool LED::loop(uint8_t healthcheck, bool button_busy) {
color_steps_[2] = Color::BLUE; // blue, no network and no bus color_steps_[2] = Color::BLUE; // blue, no network and no bus
} }
} }
// there's nothing to flash, so leave the LED in the state reset_led() gave it.
// Running the sequence here would keep writing Color::OFF and a healthy system would never show green.
if (healthcheck == 0 || button_busy) {
return false;
}
} }
// show the LED status based on the healthcheck and button busy status // show the LED status based on the healthcheck and button busy status
sequence_led(); sequence_led();
return false; return false;
} }
// turn the LED back it's default state depending on if it's hidden or not // turn the LED back it's default state depending on if it's hidden or not
void LED::reset_led() { void LED::reset_led() {
is_user_led_blink_ = false; is_user_led_blink_ = false;
set_led(hide_led_ ? Color::OFF : Color::GREEN); // Green set_led(hide_led_ ? Color::OFF : Color::GREEN); // Green
color_steps_[0] = Color::OFF; color_steps_[0] = Color::OFF;
color_steps_[1] = Color::OFF; color_steps_[1] = Color::OFF;
color_steps_[2] = Color::OFF; color_steps_[2] = Color::OFF;
}
// LED flash every few ms and then perform a factory reset // rewind the sequence so it starts from the top if the health degrades again
void LED::led_fast_flash() { led_long_timer_ = 1; // 1 will kick it off immediately
led_short_timer_ = 0;
led_flash_step_ = 0;
}
// LED flash every few ms and then perform a factory reset
void LED::led_fast_flash() {
uint32_t current_time = uuid::get_uptime(); uint32_t current_time = uuid::get_uptime();
if (current_time - last_toggle_time_ >= LED_FLASH_INTERVAL_MS) { if (current_time - last_toggle_time_ >= LED_FLASH_INTERVAL_MS) {
@@ -135,15 +151,15 @@ void LED::led_fast_flash() {
if (current_time - led_flash_start_time_ >= led_flash_duration_) { if (current_time - led_flash_start_time_ >= led_flash_duration_) {
set_led(Color::OFF); set_led(Color::OFF);
led_fast_flash_timer_ = false; led_fast_flash_timer_ = false;
#ifndef EMSESP_DEBUG #ifndef EMSESP_DEBUG
System::command_format(nullptr, 0); // Execute format operation, unless in debug mode System::command_format(nullptr, 0); // Execute format operation, unless in debug mode
#endif #endif
}
} }
}
// set LED on/off or RGB color // set LED on/off or RGB color
// ignores whether the LED is hidden or not (if hide_led_ is set) // ignores whether the LED is hidden or not (if hide_led_ is set)
void LED::set_led(Color color) { void LED::set_led(Color color) {
if (!led_gpio_) { if (!led_gpio_) {
return; return;
} }
@@ -174,12 +190,12 @@ void LED::set_led(Color color) {
} else { } else {
digitalWrite(led_gpio_, (red == 0 && green == 0 && blue == 0) || color == Color::OFF ? !LED_ON : LED_ON); digitalWrite(led_gpio_, (red == 0 && green == 0 && blue == 0) || color == Color::OFF ? !LED_ON : LED_ON);
} }
} }
// set LED custom routine // set LED custom routine
// For example: /api/system/led?data=red:blink1 // For example: /api/system/led?data=red:blink1
// For older non-RGB models, the colour would default to just being on. // For older non-RGB models, the colour would default to just being on.
bool LED::set_custom_led_routine(std::string color, std::string pattern) { bool LED::set_custom_led_routine(std::string color, std::string pattern) {
static constexpr struct { static constexpr struct {
const char * name; const char * name;
Color value; Color value;
@@ -244,12 +260,12 @@ bool LED::set_custom_led_routine(std::string color, std::string pattern) {
led_long_timer_ = uuid::get_uptime() + HEALTHCHECK_LED_FLASH_FAST_DURATION + 200UL; led_long_timer_ = uuid::get_uptime() + HEALTHCHECK_LED_FLASH_FAST_DURATION + 200UL;
return true; return true;
} }
// uses LED to show system health and user-requested LED blinks // uses LED to show system health and user-requested LED blinks
// it works in a batch of 3 configured flashes, then a long pause // it works in a batch of 3 configured flashes, then a long pause
// the timing is different for user-requested LED blink and for system healthcheck // the timing is different for user-requested LED blink and for system healthcheck
void LED::sequence_led() { void LED::sequence_led() {
// first long pause before we start flashing // first long pause before we start flashing
auto current_time = uuid::get_uptime(); auto current_time = uuid::get_uptime();
if (led_long_timer_ if (led_long_timer_
@@ -298,10 +314,10 @@ void LED::sequence_led() {
set_led(Color::OFF); // turn off on even number count, to make it flash set_led(Color::OFF); // turn off on even number count, to make it flash
} }
} }
} }
// Start the LED flash timer - duration in seconds // Start the LED flash timer - duration in seconds
void LED::start_led_fast_flash(uint8_t duration) { void LED::start_led_fast_flash(uint8_t duration) {
// Don't start if already running // Don't start if already running
if (led_fast_flash_timer_) { if (led_fast_flash_timer_) {
return; return;
@@ -311,6 +327,6 @@ void LED::start_led_fast_flash(uint8_t duration) {
led_flash_start_time_ = uuid::get_uptime(); // current time led_flash_start_time_ = uuid::get_uptime(); // current time
led_flash_duration_ = (uint32_t)duration * 1000; // duration in milliseconds led_flash_duration_ = (uint32_t)duration * 1000; // duration in milliseconds
led_fast_flash_timer_ = true; // it's active led_fast_flash_timer_ = true; // it's active
} }
} // namespace emsesp } // namespace emsesp