ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
power_supply.cpp
Go to the documentation of this file.
1#include "power_supply.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace power_supply {
6
7static const char *const TAG = "power_supply";
8
10 this->pin_->setup();
11 this->pin_->digital_write(false);
12 if (this->enable_on_boot_)
13 this->request_high_power();
14}
16 ESP_LOGCONFIG(TAG,
17 "Power Supply:\n"
18 " Time to enable: %" PRIu32 " ms\n"
19 " Keep on time: %" PRIu32 " s\n"
20 " Enable at startup: %s",
21 this->enable_time_, this->keep_on_time_ / 1000u, YESNO(this->enable_on_boot_));
22 LOG_PIN(" Pin: ", this->pin_);
23}
24
26
27bool PowerSupply::is_enabled() const { return this->active_requests_ != 0; }
28
30 if (this->active_requests_ == 0) {
31 this->cancel_timeout("power-supply-off");
32 ESP_LOGV(TAG, "Enabling");
33 this->pin_->digital_write(true);
34 delay(this->enable_time_);
35 }
36 this->active_requests_++;
37}
38
40 if (this->active_requests_ == 0) {
41 ESP_LOGW(TAG, "Invalid call to unrequest_high_power");
42 return;
43 }
44 this->active_requests_--;
45 if (this->active_requests_ == 0) {
46 this->set_timeout("power-supply-off", this->keep_on_time_, [this]() {
47 ESP_LOGV(TAG, "Disabling");
48 this->pin_->digital_write(false);
49 });
50 }
51}
53 this->active_requests_ = 0;
54 this->pin_->digital_write(false);
55}
56
57} // namespace power_supply
58} // namespace esphome
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual void setup()=0
virtual void digital_write(bool value)=0
void request_high_power()
Request high power mode. Use unrequest_high_power() to remove this request.
bool is_enabled() const
Is this power supply currently on?
void unrequest_high_power()
Un-request high power mode.
void setup() override
Register callbacks.
float get_setup_priority() const override
Hardware setup priority (+1).
const float IO
For components that represent GPIO pins like PCF8573.
Definition component.cpp:48
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29