mirror of
https://github.com/esphome/esphome.git
synced 2024-12-19 16:07:47 +01:00
Optimize application loop speed (#860)
* Optimize application loop speed * Also check call_loop * Remove duplicate code * Fixes
This commit is contained in:
parent
e86f2e993f
commit
e9e92afc9e
@ -416,8 +416,6 @@ LightColorValues LightCall::validate_() {
|
||||
if (this->brightness_.has_value())
|
||||
v.set_brightness(*this->brightness_);
|
||||
|
||||
if (this->brightness_.has_value())
|
||||
v.set_brightness(*this->brightness_);
|
||||
if (this->red_.has_value())
|
||||
v.set_red(*this->red_);
|
||||
if (this->green_.has_value())
|
||||
|
@ -57,13 +57,14 @@ void Application::setup() {
|
||||
|
||||
ESP_LOGI(TAG, "setup() finished successfully!");
|
||||
this->schedule_dump_config();
|
||||
this->calculate_looping_components_();
|
||||
}
|
||||
void Application::loop() {
|
||||
uint32_t new_app_state = 0;
|
||||
const uint32_t start = millis();
|
||||
|
||||
this->scheduler.call();
|
||||
for (Component *component : this->components_) {
|
||||
for (Component *component : this->looping_components_) {
|
||||
component->call();
|
||||
new_app_state |= component->get_component_state();
|
||||
this->app_state_ |= new_app_state;
|
||||
@ -146,6 +147,13 @@ void Application::safe_reboot() {
|
||||
}
|
||||
}
|
||||
|
||||
void Application::calculate_looping_components_() {
|
||||
for (auto *obj : this->components_) {
|
||||
if (obj->has_overridden_loop())
|
||||
this->looping_components_.push_back(obj);
|
||||
}
|
||||
}
|
||||
|
||||
Application App;
|
||||
|
||||
} // namespace esphome
|
||||
|
@ -209,7 +209,10 @@ class Application {
|
||||
|
||||
void register_component_(Component *comp);
|
||||
|
||||
void calculate_looping_components_();
|
||||
|
||||
std::vector<Component *> components_{};
|
||||
std::vector<Component *> looping_components_{};
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
std::vector<binary_sensor::BinarySensor *> binary_sensors_{};
|
||||
|
@ -138,6 +138,16 @@ float Component::get_actual_setup_priority() const {
|
||||
return this->setup_priority_override_;
|
||||
}
|
||||
void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; }
|
||||
bool Component::has_overridden_loop() const {
|
||||
#ifdef CLANG_TIDY
|
||||
bool loop_overridden = true;
|
||||
bool call_loop_overridden = true;
|
||||
#else
|
||||
bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop);
|
||||
bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop);
|
||||
#endif
|
||||
return loop_overridden || call_loop_overridden;
|
||||
}
|
||||
|
||||
PollingComponent::PollingComponent(uint32_t update_interval) : Component(), update_interval_(update_interval) {}
|
||||
|
||||
|
@ -126,6 +126,8 @@ class Component {
|
||||
|
||||
void status_momentary_error(const std::string &name, uint32_t length = 5000);
|
||||
|
||||
bool has_overridden_loop() const;
|
||||
|
||||
protected:
|
||||
virtual void call_loop();
|
||||
virtual void call_setup();
|
||||
|
Loading…
Reference in New Issue
Block a user