mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 07:28:10 +01:00
Warn if a component does long-running work in loop thread (#565)
* Warn if a component does long-running work in loop thread * Update application.cpp
This commit is contained in:
parent
8778ddd5c5
commit
afc4e45fb0
3 changed files with 38 additions and 28 deletions
|
@ -61,6 +61,7 @@ RESERVED_IDS = [
|
|||
'App', 'pinMode', 'delay', 'delayMicroseconds', 'digitalRead', 'digitalWrite', 'INPUT',
|
||||
'OUTPUT',
|
||||
'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'int8_t', 'int16_t', 'int32_t', 'int64_t',
|
||||
'close', 'pause', 'sleep', 'open',
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -66,6 +66,42 @@ void Application::dump_config() {
|
|||
component->dump_config();
|
||||
}
|
||||
}
|
||||
void Application::loop() {
|
||||
uint32_t new_app_state = 0;
|
||||
const uint32_t start = millis();
|
||||
for (Component *component : this->components_) {
|
||||
if (!component->is_failed()) {
|
||||
component->call_loop();
|
||||
}
|
||||
new_app_state |= component->get_component_state();
|
||||
this->app_state_ |= new_app_state;
|
||||
this->feed_wdt();
|
||||
}
|
||||
this->app_state_ = new_app_state;
|
||||
const uint32_t end = millis();
|
||||
if (end - start > 200) {
|
||||
ESP_LOGV(TAG, "A component took a long time in a loop() cycle (%.1f s).", (end - start) / 1e3f);
|
||||
ESP_LOGV(TAG, "Components should block for at most 20-30ms in loop().");
|
||||
ESP_LOGV(TAG, "This will become a warning soon.");
|
||||
}
|
||||
|
||||
const uint32_t now = millis();
|
||||
|
||||
if (HighFrequencyLoopRequester::is_high_frequency()) {
|
||||
yield();
|
||||
} else {
|
||||
uint32_t delay_time = this->loop_interval_;
|
||||
if (now - this->last_loop_ < this->loop_interval_)
|
||||
delay_time = this->loop_interval_ - (now - this->last_loop_);
|
||||
delay(delay_time);
|
||||
}
|
||||
this->last_loop_ = now;
|
||||
|
||||
if (this->dump_config_scheduled_) {
|
||||
this->dump_config();
|
||||
this->dump_config_scheduled_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ICACHE_RAM_ATTR HOT Application::feed_wdt() {
|
||||
static uint32_t LAST_FEED = 0;
|
||||
|
|
|
@ -87,34 +87,7 @@ class Application {
|
|||
void setup();
|
||||
|
||||
/// Make a loop iteration. Call this in your loop() function.
|
||||
void loop() {
|
||||
uint32_t new_app_state = 0;
|
||||
for (Component *component : this->components_) {
|
||||
if (!component->is_failed()) {
|
||||
component->call_loop();
|
||||
}
|
||||
new_app_state |= component->get_component_state();
|
||||
this->app_state_ |= new_app_state;
|
||||
this->feed_wdt();
|
||||
}
|
||||
this->app_state_ = new_app_state;
|
||||
|
||||
const uint32_t now = millis();
|
||||
if (HighFrequencyLoopRequester::is_high_frequency()) {
|
||||
yield();
|
||||
} else {
|
||||
uint32_t delay_time = this->loop_interval_;
|
||||
if (now - this->last_loop_ < this->loop_interval_)
|
||||
delay_time = this->loop_interval_ - (now - this->last_loop_);
|
||||
delay(delay_time);
|
||||
}
|
||||
this->last_loop_ = now;
|
||||
|
||||
if (this->dump_config_scheduled_) {
|
||||
this->dump_config();
|
||||
this->dump_config_scheduled_ = false;
|
||||
}
|
||||
}
|
||||
void loop();
|
||||
|
||||
/// Get the name of this Application set by set_name().
|
||||
const std::string &get_name() const { return this->name_; }
|
||||
|
|
Loading…
Reference in a new issue