ESPHome  2024.3.1
deep_sleep_component.cpp
Go to the documentation of this file.
1 #include "deep_sleep_component.h"
2 #include <cinttypes>
4 #include "esphome/core/log.h"
5 
6 #ifdef USE_ESP8266
7 #include <Esp.h>
8 #endif
9 
10 namespace esphome {
11 namespace deep_sleep {
12 
13 static const char *const TAG = "deep_sleep";
14 
15 bool global_has_deep_sleep = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
16 
18 #ifdef USE_ESP32
19  if (this->wakeup_cause_to_run_duration_.has_value()) {
20  esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
21  switch (wakeup_cause) {
22  case ESP_SLEEP_WAKEUP_EXT0:
23  case ESP_SLEEP_WAKEUP_EXT1:
24  case ESP_SLEEP_WAKEUP_GPIO:
25  return this->wakeup_cause_to_run_duration_->gpio_cause;
26  case ESP_SLEEP_WAKEUP_TOUCHPAD:
27  return this->wakeup_cause_to_run_duration_->touch_cause;
28  default:
29  return this->wakeup_cause_to_run_duration_->default_cause;
30  }
31  }
32 #endif
33  return this->run_duration_;
34 }
35 
37  ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
38  global_has_deep_sleep = true;
39 
40  const optional<uint32_t> run_duration = get_run_duration_();
41  if (run_duration.has_value()) {
42  ESP_LOGI(TAG, "Scheduling Deep Sleep to start in %" PRIu32 " ms", *run_duration);
43  this->set_timeout(*run_duration, [this]() { this->begin_sleep(); });
44  } else {
45  ESP_LOGD(TAG, "Not scheduling Deep Sleep, as no run duration is configured.");
46  }
47 }
49  ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
50  if (this->sleep_duration_.has_value()) {
51  uint32_t duration = *this->sleep_duration_ / 1000;
52  ESP_LOGCONFIG(TAG, " Sleep Duration: %" PRIu32 " ms", duration);
53  }
54  if (this->run_duration_.has_value()) {
55  ESP_LOGCONFIG(TAG, " Run Duration: %" PRIu32 " ms", *this->run_duration_);
56  }
57 #ifdef USE_ESP32
58  if (wakeup_pin_ != nullptr) {
59  LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
60  }
61  if (this->wakeup_cause_to_run_duration_.has_value()) {
62  ESP_LOGCONFIG(TAG, " Default Wakeup Run Duration: %" PRIu32 " ms",
63  this->wakeup_cause_to_run_duration_->default_cause);
64  ESP_LOGCONFIG(TAG, " Touch Wakeup Run Duration: %" PRIu32 " ms", this->wakeup_cause_to_run_duration_->touch_cause);
65  ESP_LOGCONFIG(TAG, " GPIO Wakeup Run Duration: %" PRIu32 " ms", this->wakeup_cause_to_run_duration_->gpio_cause);
66  }
67 #endif
68 }
70  if (this->next_enter_deep_sleep_)
71  this->begin_sleep();
72 }
74  return -100.0f; // run after everything else is ready
75 }
76 void DeepSleepComponent::set_sleep_duration(uint32_t time_ms) { this->sleep_duration_ = uint64_t(time_ms) * 1000; }
77 #if defined(USE_ESP32)
79  this->wakeup_pin_mode_ = wakeup_pin_mode;
80 }
81 #endif
82 
83 #if defined(USE_ESP32)
84 #if !defined(USE_ESP32_VARIANT_ESP32C3)
85 
86 void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
87 
88 void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
89 
90 #endif
91 
93  wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
94 }
95 
96 #endif
97 
98 void DeepSleepComponent::set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
100  if (this->prevent_ && !manual) {
101  this->next_enter_deep_sleep_ = true;
102  return;
103  }
104 #ifdef USE_ESP32
105  if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
106  !this->sleep_duration_.has_value() && this->wakeup_pin_->digital_read()) {
107  // Defer deep sleep until inactive
108  if (!this->next_enter_deep_sleep_) {
109  this->status_set_warning();
110  ESP_LOGW(TAG, "Waiting for pin_ to switch state to enter deep sleep...");
111  }
112  this->next_enter_deep_sleep_ = true;
113  return;
114  }
115 #endif
116 
117  ESP_LOGI(TAG, "Beginning Deep Sleep");
118  if (this->sleep_duration_.has_value()) {
119  ESP_LOGI(TAG, "Sleeping for %" PRId64 "us", *this->sleep_duration_);
120  }
122 
123 #if defined(USE_ESP32)
124 #if !defined(USE_ESP32_VARIANT_ESP32C3)
125  if (this->sleep_duration_.has_value())
126  esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
127  if (this->wakeup_pin_ != nullptr) {
128  bool level = !this->wakeup_pin_->is_inverted();
130  level = !level;
131  }
132  esp_sleep_enable_ext0_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()), level);
133  }
134  if (this->ext1_wakeup_.has_value()) {
135  esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
136  }
137 
138  if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
139  esp_sleep_enable_touchpad_wakeup();
140  esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
141  }
142 #endif
143 #ifdef USE_ESP32_VARIANT_ESP32C3
144  if (this->sleep_duration_.has_value())
145  esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
146  if (this->wakeup_pin_ != nullptr) {
147  bool level = !this->wakeup_pin_->is_inverted();
149  level = !level;
150  }
151  esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
152  static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
153  }
154 #endif
155  esp_deep_sleep_start();
156 #endif
157 
158 #ifdef USE_ESP8266
159  ESP.deepSleep(*this->sleep_duration_); // NOLINT(readability-static-accessed-through-instance)
160 #endif
161 }
165 
166 } // namespace deep_sleep
167 } // namespace esphome
optional< uint32_t > get_run_duration_() const
optional< WakeupCauseToRunDuration > wakeup_cause_to_run_duration_
void set_sleep_duration(uint32_t time_ms)
Set the duration in ms the component should sleep once it&#39;s in deep sleep mode.
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:146
WakeupPinMode
The values of this enum define what should be done if deep sleep is set up with a wakeup pin on the E...
void set_ext1_wakeup(Ext1Wakeup ext1_wakeup)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:28
bool has_value() const
Definition: optional.h:87
void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode)
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration)
void begin_sleep(bool manual=false)
Helper to enter deep sleep mode.
virtual uint8_t get_pin() const =0
Application App
Global storage of Application pointer - only one Application can exist.
virtual bool digital_read()=0
As long as the wakeup pin is still in the wakeup state, keep awake.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Automatically invert the wakeup level.
virtual bool is_inverted() const =0