mirror of
https://github.com/esphome/esphome.git
synced 2024-12-04 12:38:17 +01:00
924df1e7de
* Add macros header with more usable Arduino version defines * Change Arduino version checking to use our version defines * Add missing ESP8266 check * Rename Arduino version macro to ARDUINO_VERSION_CODE * Upgrade clang-tidy to use Arduino 3 * Fix clang-tidy warnings * Upgrade NeoPixelBus to upstream 2.6.7 * Use Arduino-version-appropriate API to set redirect flags * Remove now unnecessary CLANG_TIDY ifdefs * Add preprocessor hackery to avoid including pgmspace.h * Bump base image to 4.1.1 and update lint * Fix nfctag * Fix make_unique ambiguous * Fix ignore name * Fix ambiguous v2 * Remove unused begin * Cast time_t to prevent issues on platforms where time_t is 32bit Co-authored-by: Otto winter <otto@otto-winter.com>
94 lines
3.3 KiB
C++
94 lines
3.3 KiB
C++
#include "deep_sleep_component.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/application.h"
|
|
|
|
namespace esphome {
|
|
namespace deep_sleep {
|
|
|
|
static const char *const TAG = "deep_sleep";
|
|
|
|
bool global_has_deep_sleep = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
|
|
void DeepSleepComponent::setup() {
|
|
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
|
|
global_has_deep_sleep = true;
|
|
|
|
if (this->run_duration_.has_value())
|
|
this->set_timeout(*this->run_duration_, [this]() { this->begin_sleep(); });
|
|
}
|
|
void DeepSleepComponent::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
|
|
if (this->sleep_duration_.has_value()) {
|
|
uint32_t duration = *this->sleep_duration_ / 1000;
|
|
ESP_LOGCONFIG(TAG, " Sleep Duration: %u ms", duration);
|
|
}
|
|
if (this->run_duration_.has_value()) {
|
|
ESP_LOGCONFIG(TAG, " Run Duration: %u ms", *this->run_duration_);
|
|
}
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
if (this->wakeup_pin_.has_value()) {
|
|
LOG_PIN(" Wakeup Pin: ", *this->wakeup_pin_);
|
|
}
|
|
#endif
|
|
}
|
|
void DeepSleepComponent::loop() {
|
|
if (this->next_enter_deep_sleep_)
|
|
this->begin_sleep();
|
|
}
|
|
float DeepSleepComponent::get_loop_priority() const {
|
|
return -100.0f; // run after everything else is ready
|
|
}
|
|
void DeepSleepComponent::set_sleep_duration(uint32_t time_ms) { this->sleep_duration_ = uint64_t(time_ms) * 1000; }
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
void DeepSleepComponent::set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode) {
|
|
this->wakeup_pin_mode_ = wakeup_pin_mode;
|
|
}
|
|
void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
|
|
#endif
|
|
void DeepSleepComponent::set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
|
|
void DeepSleepComponent::begin_sleep(bool manual) {
|
|
if (this->prevent_ && !manual) {
|
|
this->next_enter_deep_sleep_ = true;
|
|
return;
|
|
}
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_.has_value() &&
|
|
!this->sleep_duration_.has_value() && (*this->wakeup_pin_)->digital_read()) {
|
|
// Defer deep sleep until inactive
|
|
if (!this->next_enter_deep_sleep_) {
|
|
this->status_set_warning();
|
|
ESP_LOGW(TAG, "Waiting for pin_ to switch state to enter deep sleep...");
|
|
}
|
|
this->next_enter_deep_sleep_ = true;
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
ESP_LOGI(TAG, "Beginning Deep Sleep");
|
|
|
|
App.run_safe_shutdown_hooks();
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
if (this->sleep_duration_.has_value())
|
|
esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
|
|
if (this->wakeup_pin_.has_value()) {
|
|
bool level = !(*this->wakeup_pin_)->is_inverted();
|
|
if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && (*this->wakeup_pin_)->digital_read())
|
|
level = !level;
|
|
esp_sleep_enable_ext0_wakeup(gpio_num_t((*this->wakeup_pin_)->get_pin()), level);
|
|
}
|
|
if (this->ext1_wakeup_.has_value()) {
|
|
esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
|
|
}
|
|
esp_deep_sleep_start();
|
|
#endif
|
|
|
|
#ifdef ARDUINO_ARCH_ESP8266
|
|
ESP.deepSleep(*this->sleep_duration_); // NOLINT(readability-static-accessed-through-instance)
|
|
#endif
|
|
}
|
|
float DeepSleepComponent::get_setup_priority() const { return setup_priority::LATE; }
|
|
void DeepSleepComponent::prevent_deep_sleep() { this->prevent_ = true; }
|
|
|
|
} // namespace deep_sleep
|
|
} // namespace esphome
|