ESPHome 2026.5.0
Loading...
Searching...
No Matches
status_led.cpp
Go to the documentation of this file.
1#include "status_led.h"
2#include "esphome/core/log.h"
4
5namespace esphome::status_led {
6
7static const char *const TAG = "status_led";
8
9static constexpr uint32_t ERROR_PERIOD_MS = 250;
10static constexpr uint32_t ERROR_ON_MS = 150;
11static constexpr uint32_t WARNING_PERIOD_MS = 1500;
12static constexpr uint32_t WARNING_ON_MS = 250;
13
14StatusLED *global_status_led = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
15
16StatusLED::StatusLED(GPIOPin *pin) : pin_(pin) { global_status_led = this; }
18 this->pin_->setup();
19 this->pin_->digital_write(false);
20}
22 ESP_LOGCONFIG(TAG, "Status LED:");
23 LOG_PIN(" Pin: ", this->pin_);
24}
26 const uint32_t app_state = App.get_app_state();
27 // Use millis() rather than App.get_loop_component_start_time() because this loop is also
28 // dispatched from Application::feed_wdt() during long blocking operations, where the cached
29 // per-component timestamp doesn't advance and would freeze the blink pattern.
30 const uint32_t now = millis();
31 if ((app_state & STATUS_LED_ERROR) != 0u) {
32 this->pin_->digital_write(now % ERROR_PERIOD_MS < ERROR_ON_MS);
33 } else if ((app_state & STATUS_LED_WARNING) != 0u) {
34 this->pin_->digital_write(now % WARNING_PERIOD_MS < WARNING_ON_MS);
35 } else {
36 this->pin_->digital_write(false);
37 this->disable_loop();
38 }
39}
41
42} // namespace esphome::status_led
uint8_t get_app_state() const
Return the public app state status bits (STATUS_LED_* only).
void disable_loop()
Disable this component's loop.
virtual void setup()=0
virtual void digital_write(bool value)=0
float get_setup_priority() const override
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41
const char *const TAG
Definition spi.cpp:7
StatusLED * global_status_led
constexpr uint8_t STATUS_LED_WARNING
Definition component.h:88
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t STATUS_LED_ERROR
Definition component.h:89
static void uint32_t