ESPHome 2025.5.0
Loading...
Searching...
No Matches
deep_sleep_esp32.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace deep_sleep {
7
8static const char *const TAG = "deep_sleep";
9
11 if (this->wakeup_cause_to_run_duration_.has_value()) {
12 esp_sleep_wakeup_cause_t wakeup_cause = esp_sleep_get_wakeup_cause();
13 switch (wakeup_cause) {
14 case ESP_SLEEP_WAKEUP_EXT0:
15 case ESP_SLEEP_WAKEUP_EXT1:
16 case ESP_SLEEP_WAKEUP_GPIO:
17 return this->wakeup_cause_to_run_duration_->gpio_cause;
18 case ESP_SLEEP_WAKEUP_TOUCHPAD:
19 return this->wakeup_cause_to_run_duration_->touch_cause;
20 default:
21 return this->wakeup_cause_to_run_duration_->default_cause;
22 }
23 }
24 return this->run_duration_;
25}
26
28 this->wakeup_pin_mode_ = wakeup_pin_mode;
29}
30
31#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6)
32void DeepSleepComponent::set_ext1_wakeup(Ext1Wakeup ext1_wakeup) { this->ext1_wakeup_ = ext1_wakeup; }
33
34#if !defined(USE_ESP32_VARIANT_ESP32H2)
35void DeepSleepComponent::set_touch_wakeup(bool touch_wakeup) { this->touch_wakeup_ = touch_wakeup; }
36#endif
37
38#endif
39
41 wakeup_cause_to_run_duration_ = wakeup_cause_to_run_duration;
42}
43
45 if (wakeup_pin_ != nullptr) {
46 LOG_PIN(" Wakeup Pin: ", this->wakeup_pin_);
47 }
48 if (this->wakeup_cause_to_run_duration_.has_value()) {
49 ESP_LOGCONFIG(TAG, " Default Wakeup Run Duration: %" PRIu32 " ms",
50 this->wakeup_cause_to_run_duration_->default_cause);
51 ESP_LOGCONFIG(TAG, " Touch Wakeup Run Duration: %" PRIu32 " ms", this->wakeup_cause_to_run_duration_->touch_cause);
52 ESP_LOGCONFIG(TAG, " GPIO Wakeup Run Duration: %" PRIu32 " ms", this->wakeup_cause_to_run_duration_->gpio_cause);
53 }
54}
55
57 if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_KEEP_AWAKE && this->wakeup_pin_ != nullptr &&
58 this->wakeup_pin_->digital_read()) {
59 // Defer deep sleep until inactive
60 if (!this->next_enter_deep_sleep_) {
61 this->status_set_warning();
62 ESP_LOGW(TAG, "Waiting wakeup pin state change to enter deep sleep...");
63 }
64 this->next_enter_deep_sleep_ = true;
65 return false;
66 }
67 return true;
68}
69
71#if !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32C6) && !defined(USE_ESP32_VARIANT_ESP32H2)
72 if (this->sleep_duration_.has_value())
73 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
74 if (this->wakeup_pin_ != nullptr) {
75 bool level = !this->wakeup_pin_->is_inverted();
77 level = !level;
78 }
79 esp_sleep_enable_ext0_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()), level);
80 }
81 if (this->ext1_wakeup_.has_value()) {
82 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
83 }
84
85 if (this->touch_wakeup_.has_value() && *(this->touch_wakeup_)) {
86 esp_sleep_enable_touchpad_wakeup();
87 esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
88 }
89#endif
90
91#if defined(USE_ESP32_VARIANT_ESP32H2)
92 if (this->sleep_duration_.has_value())
93 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
94 if (this->ext1_wakeup_.has_value()) {
95 esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
96 }
97#endif
98
99#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6)
100 if (this->sleep_duration_.has_value())
101 esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
102 if (this->wakeup_pin_ != nullptr) {
103 bool level = !this->wakeup_pin_->is_inverted();
105 level = !level;
106 }
107 esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
108 static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
109 }
110#endif
111 esp_deep_sleep_start();
112}
113
114} // namespace deep_sleep
115} // namespace esphome
116#endif
void status_set_warning(const char *message="unspecified")
virtual bool digital_read()=0
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration)
optional< uint32_t > get_run_duration_() const
void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode)
void set_ext1_wakeup(Ext1Wakeup ext1_wakeup)
optional< WakeupCauseToRunDuration > wakeup_cause_to_run_duration_
bool has_value() const
Definition optional.h:87
WakeupPinMode
The values of this enum define what should be done if deep sleep is set up with a wakeup pin on the E...
@ WAKEUP_PIN_MODE_KEEP_AWAKE
As long as the wakeup pin is still in the wakeup state, keep awake.
@ WAKEUP_PIN_MODE_INVERT_WAKEUP
Automatically invert the wakeup level.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7