ESPHome  2022.9.2
power_supply.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
5 
6 namespace esphome {
7 namespace power_supply {
8 
9 class PowerSupply : public Component {
10  public:
11  void set_pin(GPIOPin *pin) { pin_ = pin; }
12  void set_enable_time(uint32_t enable_time) { enable_time_ = enable_time; }
13  void set_keep_on_time(uint32_t keep_on_time) { keep_on_time_ = keep_on_time; }
14 
16  bool is_enabled() const;
17 
19  void request_high_power();
20 
22  void unrequest_high_power();
23 
24  // ========== INTERNAL METHODS ==========
25  // (In most use cases you won't need these)
27  void setup() override;
28  void dump_config() override;
30  float get_setup_priority() const override;
31 
32  void on_shutdown() override;
33 
34  protected:
36  bool enabled_{false};
37  uint32_t enable_time_;
38  uint32_t keep_on_time_;
39  int16_t active_requests_{0}; // use signed integer to make catching negative requests easier.
40 };
41 
43  public:
44  void set_parent(PowerSupply *parent) { parent_ = parent; }
45  void request() {
46  if (!this->requested_ && this->parent_ != nullptr) {
47  this->parent_->request_high_power();
48  this->requested_ = true;
49  }
50  }
51  void unrequest() {
52  if (this->requested_ && this->parent_ != nullptr) {
53  this->parent_->unrequest_high_power();
54  this->requested_ = false;
55  }
56  }
57 
58  protected:
59  PowerSupply *parent_{nullptr};
60  bool requested_{false};
61 };
62 
63 } // namespace power_supply
64 } // 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:13
void set_parent(PowerSupply *parent)
Definition: power_supply.h:44
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?
Definition: a4988.cpp:4
void set_enable_time(uint32_t enable_time)
Definition: power_supply.h:12