mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-19 12:43:28 +02:00
ac0d921413
* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
148 lines
4.3 KiB
C++
148 lines
4.3 KiB
C++
#include "esphome/core/application.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/version.h"
|
|
#include "esphome/core/hal.h"
|
|
|
|
#ifdef USE_STATUS_LED
|
|
#include "esphome/components/status_led/status_led.h"
|
|
#endif
|
|
|
|
namespace esphome {
|
|
|
|
static const char *const TAG = "app";
|
|
|
|
void Application::register_component_(Component *comp) {
|
|
if (comp == nullptr) {
|
|
ESP_LOGW(TAG, "Tried to register null component!");
|
|
return;
|
|
}
|
|
|
|
for (auto *c : this->components_) {
|
|
if (comp == c) {
|
|
ESP_LOGW(TAG, "Component %s already registered! (%p)", c->get_component_source(), c);
|
|
return;
|
|
}
|
|
}
|
|
this->components_.push_back(comp);
|
|
}
|
|
void Application::setup() {
|
|
ESP_LOGI(TAG, "Running through setup()...");
|
|
ESP_LOGV(TAG, "Sorting components by setup priority...");
|
|
std::stable_sort(this->components_.begin(), this->components_.end(), [](const Component *a, const Component *b) {
|
|
return a->get_actual_setup_priority() > b->get_actual_setup_priority();
|
|
});
|
|
|
|
for (uint32_t i = 0; i < this->components_.size(); i++) {
|
|
Component *component = this->components_[i];
|
|
|
|
component->call();
|
|
this->scheduler.process_to_add();
|
|
if (component->can_proceed())
|
|
continue;
|
|
|
|
std::stable_sort(this->components_.begin(), this->components_.begin() + i + 1,
|
|
[](Component *a, Component *b) { return a->get_loop_priority() > b->get_loop_priority(); });
|
|
|
|
do {
|
|
uint32_t new_app_state = STATUS_LED_WARNING;
|
|
this->scheduler.call();
|
|
for (uint32_t j = 0; j <= i; j++) {
|
|
this->components_[j]->call();
|
|
new_app_state |= this->components_[j]->get_component_state();
|
|
this->app_state_ |= new_app_state;
|
|
}
|
|
this->app_state_ = new_app_state;
|
|
yield();
|
|
this->feed_wdt();
|
|
} while (!component->can_proceed());
|
|
}
|
|
|
|
ESP_LOGI(TAG, "setup() finished successfully!");
|
|
this->schedule_dump_config();
|
|
this->calculate_looping_components_();
|
|
}
|
|
void Application::loop() {
|
|
uint32_t new_app_state = 0;
|
|
|
|
this->scheduler.call();
|
|
for (Component *component : this->looping_components_) {
|
|
{
|
|
WarnIfComponentBlockingGuard guard{component};
|
|
component->call();
|
|
}
|
|
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_);
|
|
|
|
uint32_t next_schedule = this->scheduler.next_schedule_in().value_or(delay_time);
|
|
// next_schedule is max 0.5*delay_time
|
|
// otherwise interval=0 schedules result in constant looping with almost no sleep
|
|
next_schedule = std::max(next_schedule, delay_time / 2);
|
|
delay_time = std::min(next_schedule, delay_time);
|
|
delay(delay_time);
|
|
}
|
|
this->last_loop_ = now;
|
|
|
|
if (this->dump_config_at_ >= 0 && this->dump_config_at_ < this->components_.size()) {
|
|
if (this->dump_config_at_ == 0) {
|
|
ESP_LOGI(TAG, "ESPHome version " ESPHOME_VERSION " compiled on %s", this->compilation_time_.c_str());
|
|
#ifdef ESPHOME_PROJECT_NAME
|
|
ESP_LOGI(TAG, "Project " ESPHOME_PROJECT_NAME " version " ESPHOME_PROJECT_VERSION);
|
|
#endif
|
|
}
|
|
|
|
this->components_[this->dump_config_at_]->call_dump_config();
|
|
this->dump_config_at_++;
|
|
}
|
|
}
|
|
|
|
void IRAM_ATTR HOT Application::feed_wdt() {
|
|
static uint32_t last_feed = 0;
|
|
uint32_t now = millis();
|
|
if (now - last_feed > 3) {
|
|
arch_feed_wdt();
|
|
last_feed = now;
|
|
#ifdef USE_STATUS_LED
|
|
if (status_led::global_status_led != nullptr) {
|
|
status_led::global_status_led->call();
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
void Application::reboot() {
|
|
ESP_LOGI(TAG, "Forcing a reboot...");
|
|
for (auto *comp : this->components_)
|
|
comp->on_shutdown();
|
|
arch_restart();
|
|
}
|
|
void Application::safe_reboot() {
|
|
ESP_LOGI(TAG, "Rebooting safely...");
|
|
for (auto *comp : this->components_)
|
|
comp->on_safe_shutdown();
|
|
for (auto *comp : this->components_)
|
|
comp->on_shutdown();
|
|
arch_restart();
|
|
}
|
|
|
|
void Application::calculate_looping_components_() {
|
|
for (auto *obj : this->components_) {
|
|
if (obj->has_overridden_loop())
|
|
this->looping_components_.push_back(obj);
|
|
}
|
|
}
|
|
|
|
Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
|
|
} // namespace esphome
|