ESPHome  2022.1.2
deep_sleep_component.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
6 #include "esphome/core/hal.h"
7 
8 #ifdef USE_ESP32
9 #include <esp_sleep.h>
10 #endif
11 
12 namespace esphome {
13 namespace deep_sleep {
14 
15 #ifdef USE_ESP32
16 
23 
28 };
29 
30 struct Ext1Wakeup {
31  uint64_t mask;
32  esp_sleep_ext1_wakeup_mode_t wakeup_mode;
33 };
34 
36  // Run duration if woken up by timer or any other reason besides those below.
37  uint32_t default_cause;
38  // Run duration if woken up by touch pads.
39  uint32_t touch_cause;
40  // Run duration if woken up by GPIO pins.
41  uint32_t gpio_cause;
42 };
43 
44 #endif
45 
46 template<typename... Ts> class EnterDeepSleepAction;
47 
48 template<typename... Ts> class PreventDeepSleepAction;
49 
56 class DeepSleepComponent : public Component {
57  public:
59  void set_sleep_duration(uint32_t time_ms);
60 #ifdef USE_ESP32
61 
64  void set_wakeup_pin(InternalGPIOPin *pin) { this->wakeup_pin_ = pin; }
65 
66  void set_wakeup_pin_mode(WakeupPinMode wakeup_pin_mode);
67 
68  void set_ext1_wakeup(Ext1Wakeup ext1_wakeup);
69 
70  void set_touch_wakeup(bool touch_wakeup);
71 
72  // Set the duration in ms for how long the code should run before entering
73  // deep sleep mode, according to the cause the ESP32 has woken.
74  void set_run_duration(WakeupCauseToRunDuration wakeup_cause_to_run_duration);
75 
76 #endif
77  void set_run_duration(uint32_t time_ms);
79 
80  void setup() override;
81  void dump_config() override;
82  void loop() override;
83  float get_loop_priority() const override;
84  float get_setup_priority() const override;
85 
87  void begin_sleep(bool manual = false);
88 
89  void prevent_deep_sleep();
90 
91  protected:
92  // Returns nullopt if no run duration is set. Otherwise, returns the run
93  // duration before entering deep sleep.
94  optional<uint32_t> get_run_duration_() const;
95 
97 #ifdef USE_ESP32
103 #endif
105  bool next_enter_deep_sleep_{false};
106  bool prevent_{false};
107 };
108 
109 extern bool global_has_deep_sleep; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
110 
111 template<typename... Ts> class EnterDeepSleepAction : public Action<Ts...> {
112  public:
113  EnterDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {}
114  TEMPLATABLE_VALUE(uint32_t, sleep_duration);
115 
116  void play(Ts... x) override {
117  if (this->sleep_duration_.has_value()) {
118  this->deep_sleep_->set_sleep_duration(this->sleep_duration_.value(x...));
119  }
120  this->deep_sleep_->begin_sleep(true);
121  }
122 
123  protected:
125 };
126 
127 template<typename... Ts> class PreventDeepSleepAction : public Action<Ts...> {
128  public:
129  PreventDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {}
130 
131  void play(Ts... x) override { this->deep_sleep_->prevent_deep_sleep(); }
132 
133  protected:
135 };
136 
137 } // namespace deep_sleep
138 } // namespace esphome
void setup()
void loop()
optional< WakeupCauseToRunDuration > wakeup_cause_to_run_duration_
WakeupPinMode
The values of this enum define what should be done if deep sleep is set up with a wakeup pin on the E...
PreventDeepSleepAction(DeepSleepComponent *deep_sleep)
This component allows setting up the node to go into deep sleep mode to conserve battery.
Ignore the fact that we will wake up when going into deep sleep.
EnterDeepSleepAction(DeepSleepComponent *deep_sleep)
As long as the wakeup pin is still in the wakeup state, keep awake.
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
Automatically invert the wakeup level.
esp_sleep_ext1_wakeup_mode_t wakeup_mode
void set_wakeup_pin(InternalGPIOPin *pin)
Set the pin to wake up to on the ESP32 once it&#39;s in deep sleep mode.