mirror of
https://github.com/esphome/esphome.git
synced 2026-09-18 02:28:42 +00:00
[status_led] Simplify loop body, rely on disable_loop for idle optimization
This commit is contained in:
@@ -7,10 +7,10 @@ namespace status_led {
|
||||
|
||||
static const char *const TAG = "status_led";
|
||||
|
||||
static constexpr uint32_t ERROR_PERIOD_MS = 250;
|
||||
static constexpr uint32_t ERROR_ON_MS = 150;
|
||||
static constexpr uint32_t ERROR_OFF_MS = 100;
|
||||
static constexpr uint32_t WARNING_PERIOD_MS = 1500;
|
||||
static constexpr uint32_t WARNING_ON_MS = 250;
|
||||
static constexpr uint32_t WARNING_OFF_MS = 1250;
|
||||
|
||||
StatusLED *global_status_led = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
@@ -25,28 +25,14 @@ void StatusLED::dump_config() {
|
||||
}
|
||||
void StatusLED::loop() {
|
||||
const uint32_t app_state = App.get_app_state();
|
||||
const bool error = (app_state & STATUS_LED_ERROR) != 0u;
|
||||
const bool warning = (app_state & STATUS_LED_WARNING) != 0u;
|
||||
if (!error && !warning) {
|
||||
if (this->led_on_) {
|
||||
this->led_on_ = false;
|
||||
this->pin_->digital_write(false);
|
||||
}
|
||||
this->disable_loop();
|
||||
return;
|
||||
}
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if ((int32_t) (now - this->next_toggle_) < 0)
|
||||
return;
|
||||
this->led_on_ = !this->led_on_;
|
||||
uint32_t delay;
|
||||
if (error) {
|
||||
delay = this->led_on_ ? ERROR_ON_MS : ERROR_OFF_MS;
|
||||
if ((app_state & STATUS_LED_ERROR) != 0u) {
|
||||
this->pin_->digital_write(App.get_loop_component_start_time() % ERROR_PERIOD_MS < ERROR_ON_MS);
|
||||
} else if ((app_state & STATUS_LED_WARNING) != 0u) {
|
||||
this->pin_->digital_write(App.get_loop_component_start_time() % WARNING_PERIOD_MS < WARNING_ON_MS);
|
||||
} else {
|
||||
delay = this->led_on_ ? WARNING_ON_MS : WARNING_OFF_MS;
|
||||
this->pin_->digital_write(false);
|
||||
this->disable_loop();
|
||||
}
|
||||
this->next_toggle_ = now + delay;
|
||||
this->pin_->digital_write(this->led_on_);
|
||||
}
|
||||
float StatusLED::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ class StatusLED : public Component {
|
||||
|
||||
protected:
|
||||
GPIOPin *pin_;
|
||||
uint32_t next_toggle_{0};
|
||||
bool led_on_{false};
|
||||
};
|
||||
|
||||
extern StatusLED *global_status_led; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
Reference in New Issue
Block a user