ESPHome 2026.11.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
5
6ESPHOME_LOG_TAG(TAG, "power_supply");
7
8static constexpr uint32_t POWER_OFF_TIMEOUT_ID = 0;
9
11 this->pin_->setup();
12 if (this->pin_->is_held()) {
13 // Rail stayed on across the reset; drop it unless something asks for it
15 } else {
16 this->pin_->digital_write(false);
17 }
18 if (this->enable_on_boot_)
19 this->request_high_power();
20}
22 ESP_LOGCONFIG(TAG,
23 "Power Supply:\n"
24 " Time to enable: %" PRIu32 " ms\n"
25 " Keep on time: %" PRIu32 " s\n"
26 " Enable at startup: %s",
27 this->enable_time_, this->keep_on_time_ / 1000u, YESNO(this->enable_on_boot_));
28 LOG_PIN(" Pin: ", this->pin_);
29}
30
32 if (this->pin_->is_internal() && this->enable_on_boot_)
34 return setup_priority::IO;
35}
36
37bool PowerSupply::is_enabled() const { return this->active_requests_ != 0; }
38
40 if (this->active_requests_ == 0) {
41 this->cancel_timeout(POWER_OFF_TIMEOUT_ID);
42 ESP_LOGV(TAG, "Enabling");
43 this->pin_->digital_write(true);
44 delay(this->enable_time_);
45 }
46 this->active_requests_++;
47}
48
50 if (this->active_requests_ == 0) {
51 ESP_LOGW(TAG, "Invalid call to unrequest_high_power");
52 return;
53 }
54 this->active_requests_--;
56}
57
59 if (this->active_requests_ == 0) {
60 this->set_timeout(POWER_OFF_TIMEOUT_ID, this->keep_on_time_, [this]() {
61 ESP_LOGV(TAG, "Disabling");
62 this->pin_->digital_write(false);
63 });
64 }
65}
66
68 if (this->pin_->get_hold())
69 return;
70 this->active_requests_ = 0;
71 this->pin_->digital_write(false);
72}
73
74} // namespace esphome::power_supply
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool is_internal()
Only the platform pin class behind the InternalGPIOPin alias may return true: callers static_cast to ...
Definition gpio_pin.h:87
bool get_hold() const
Definition gpio_pin.h:89
virtual bool is_held() const
Definition gpio_pin.h:90
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).
ESPHOME_LOG_TAG(TAG, "power_supply")
constexpr float POWER
For power supply components that must be on before buses like i2c can work.
Definition component.h:37
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t