ESPHome  2024.12.2
power_supply.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
5 
6 #include <cinttypes>
7 
8 namespace esphome {
9 namespace power_supply {
10 
11 class PowerSupply : public Component {
12  public:
13  void set_pin(GPIOPin *pin) { pin_ = pin; }
14  void set_enable_time(uint32_t enable_time) { enable_time_ = enable_time; }
15  void set_keep_on_time(uint32_t keep_on_time) { keep_on_time_ = keep_on_time; }
16  void set_enable_on_boot(bool enable_on_boot) { enable_on_boot_ = enable_on_boot; }
17 
19  bool is_enabled() const;
20 
22  void request_high_power();
23 
25  void unrequest_high_power();
26 
27  // ========== INTERNAL METHODS ==========
28  // (In most use cases you won't need these)
30  void setup() override;
31  void dump_config() override;
33  float get_setup_priority() const override;
34 
35  void on_shutdown() override;
36 
37  protected:
39  bool enable_on_boot_{false};
40  uint32_t enable_time_;
41  uint32_t keep_on_time_;
42  int16_t active_requests_{0}; // use signed integer to make catching negative requests easier.
43 };
44 
46  public:
47  void set_parent(PowerSupply *parent) { parent_ = parent; }
48  void request() {
49  if (!this->requested_ && this->parent_ != nullptr) {
50  this->parent_->request_high_power();
51  this->requested_ = true;
52  }
53  }
54  void unrequest() {
55  if (this->requested_ && this->parent_ != nullptr) {
56  this->parent_->unrequest_high_power();
57  this->requested_ = false;
58  }
59  }
60 
61  protected:
62  PowerSupply *parent_{nullptr};
63  bool requested_{false};
64 };
65 
66 } // namespace power_supply
67 } // namespace esphome
float get_setup_priority() const override
Hardware setup priority (+1).
void setup() override
Register callbacks.
Definition: power_supply.cpp:9
void set_keep_on_time(uint32_t keep_on_time)
Definition: power_supply.h:15
void set_parent(PowerSupply *parent)
Definition: power_supply.h:47
void request_high_power()
Request high power mode. Use unrequest_high_power() to remove this request.
void unrequest_high_power()
Un-request high power mode.
bool is_enabled() const
Is this power supply currently on?
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_enable_time(uint32_t enable_time)
Definition: power_supply.h:14
void set_enable_on_boot(bool enable_on_boot)
Definition: power_supply.h:16