ESPHome  2024.10.2
power_supply.cpp
Go to the documentation of this file.
1 #include "power_supply.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace power_supply {
6 
7 static const char *const TAG = "power_supply";
8 
10  ESP_LOGCONFIG(TAG, "Setting up Power Supply...");
11 
12  this->pin_->setup();
13  this->pin_->digital_write(false);
14  if (this->enable_on_boot_)
15  this->request_high_power();
16 }
18  ESP_LOGCONFIG(TAG, "Power Supply:");
19  LOG_PIN(" Pin: ", this->pin_);
20  ESP_LOGCONFIG(TAG, " Time to enable: %" PRIu32 " ms", this->enable_time_);
21  ESP_LOGCONFIG(TAG, " Keep on time: %.1f s", this->keep_on_time_ / 1000.0f);
22  if (this->enable_on_boot_)
23  ESP_LOGCONFIG(TAG, " Enabled at startup: True");
24 }
25 
27 
28 bool PowerSupply::is_enabled() const { return this->active_requests_ != 0; }
29 
31  if (this->active_requests_ == 0) {
32  this->cancel_timeout("power-supply-off");
33  ESP_LOGD(TAG, "Enabling power supply.");
34  this->pin_->digital_write(true);
35  delay(this->enable_time_);
36  }
37  this->active_requests_++;
38 }
39 
41  if (this->active_requests_ == 0) {
42  ESP_LOGW(TAG, "Invalid call to unrequest_high_power");
43  return;
44  }
45  this->active_requests_--;
46  if (this->active_requests_ == 0) {
47  this->set_timeout("power-supply-off", this->keep_on_time_, [this]() {
48  ESP_LOGD(TAG, "Disabling power supply.");
49  this->pin_->digital_write(false);
50  });
51  }
52 }
54  this->active_requests_ = 0;
55  this->pin_->digital_write(false);
56 }
57 
58 } // namespace power_supply
59 } // namespace esphome
virtual void digital_write(bool value)=0
float get_setup_priority() const override
Hardware setup priority (+1).
void setup() override
Register callbacks.
Definition: power_supply.cpp:9
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
Definition: component.cpp:73
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:69
virtual void setup()=0
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?
const float IO
For components that represent GPIO pins like PCF8573.
Definition: component.cpp:17
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26