ESPHome  2022.11.3
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 %u 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: %u ms", duration);
53  }
54  if (this->run_duration_.has_value()) {
55  ESP_LOGCONFIG(TAG, " Run Duration: %u 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: %u ms", this->wakeup_cause_to_run_duration_->default_cause);
63  ESP_LOGCONFIG(TAG, " Touch Wakeup Run Duration: %u ms", this->wakeup_cause_to_run_duration_->touch_cause);
64  ESP_LOGCONFIG(TAG, " GPIO Wakeup Run Duration: %u ms", this->wakeup_cause_to_run_duration_->gpio_cause);
65  }
66 #endif
67 }
69  if (this->next_enter_deep_sleep_)
70  this->begin_sleep();
71 }
73  return -100.0f; // run after everything else is ready
74 }
75 void DeepSleepComponent::set_sleep_duration(uint32_t time_ms) { this->sleep_duration_ = uint64_t(time_ms) * 1000; }
76 #if defined(USE_ESP32)
78  this->wakeup_pin_mode_ = wakeup_pin_mode;
79 }
80 #endif
81 
82 #if defined(USE_ESP32)
83 #if !defined(USE_ESP32_VARIANT_ESP32C3)
84 
85 void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
86 
87 void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
88 
89 #endif
90 
92  wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
93 }
94 
95 #endif
96 
97 void DeepSleepComponent::set_run_duration(uint32_t time_ms) { this->run_duration_ = time_ms; }
99  if (this->prevent_ && !manual) {
100  this->next_enter_deep_sleep_ = true;
101  return;
102  }
103 #ifdef USE_ESP32
104  if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
105  !this->sleep_duration_.has_value() && this->wakeup_pin_->digital_read()) {
106  // Defer deep sleep until inactive
107  if (!this->next_enter_deep_sleep_) {
108  this->status_set_warning();
109  ESP_LOGW(TAG, "Waiting for pin_ to switch state to enter deep sleep...");
110  }
111  this->next_enter_deep_sleep_ = true;
112  return;
113  }
114 #endif
115 
116  ESP_LOGI(TAG, "Beginning Deep Sleep");
117  if (this->sleep_duration_.has_value())
118  ESP_LOGI(TAG, "Sleeping for %" PRId64 "us", *this->sleep_duration_);
119 
121 
122 #if defined(USE_ESP32)
123 #if !defined(USE_ESP32_VARIANT_ESP32C3)
124  if (this->sleep_duration_.has_value())
125  esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
126  if (this->wakeup_pin_ != nullptr) {
127  bool level = !this->wakeup_pin_->is_inverted();
129  level = !level;
130  }
131  esp_sleep_enable_ext0_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()), level);
132  }
133  if (this->ext1_wakeup_.has_value()) {
134  esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
135  }
136 
137  if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
138  esp_sleep_enable_touchpad_wakeup();
139  esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
140  }
141 #endif
142 #ifdef USE_ESP32_VARIANT_ESP32C3
143  if (this->sleep_duration_.has_value())
144  esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
145  if (this->wakeup_pin_ != nullptr) {
146  bool level = !this->wakeup_pin_->is_inverted();
148  level = !level;
149  }
150  esp_deep_sleep_enable_gpio_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()),
151  static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
152  }
153 #endif
154  esp_deep_sleep_start();
155 #endif
156 
157 #ifdef USE_ESP8266
158  ESP.deepSleep(*this->sleep_duration_); // NOLINT(readability-static-accessed-through-instance)
159 #endif
160 }
164 
165 } // namespace deep_sleep
166 } // 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.
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:68
const float LATE
For components that should be initialized at the very end of the setup process.
Definition: component.cpp:27
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.
void status_set_warning()
Definition: component.cpp:141
virtual bool digital_read()=0
As long as the wakeup pin is still in the wakeup state, keep awake.
Definition: a4988.cpp:4
Automatically invert the wakeup level.
virtual bool is_inverted() const =0