ESPHome  2022.5.1
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  this->enabled_ = false;
15 }
17  ESP_LOGCONFIG(TAG, "Power Supply:");
18  LOG_PIN(" Pin: ", this->pin_);
19  ESP_LOGCONFIG(TAG, " Time to enable: %u ms", this->enable_time_);
20  ESP_LOGCONFIG(TAG, " Keep on time: %.1f s", this->keep_on_time_ / 1000.0f);
21 }
22 
24 
25 bool PowerSupply::is_enabled() const { return this->enabled_; }
26 
28  this->cancel_timeout("power-supply-off");
29  this->pin_->digital_write(true);
30 
31  if (this->active_requests_ == 0) {
32  // we need to enable the power supply.
33  // cancel old timeout if it exists because we now definitely have a high power mode.
34  ESP_LOGD(TAG, "Enabling power supply.");
35  delay(this->enable_time_);
36  }
37  this->enabled_ = true;
38  // increase active requests
39  this->active_requests_++;
40 }
41 
43  this->active_requests_--;
44  if (this->active_requests_ < 0) {
45  // we're just going to use 0 as our new counter.
46  this->active_requests_ = 0;
47  }
48 
49  if (this->active_requests_ == 0) {
50  // set timeout for power supply off
51  this->set_timeout("power-supply-off", this->keep_on_time_, [this]() {
52  ESP_LOGD(TAG, "Disabling power supply.");
53  this->pin_->digital_write(false);
54  this->enabled_ = false;
55  });
56  }
57 }
59  this->active_requests_ = 0;
60  this->pin_->digital_write(false);
61 }
62 
63 } // namespace power_supply
64 } // 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:71
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:67
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:16
Definition: a4988.cpp:4
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27