ESPHome 2025.5.0
Loading...
Searching...
No Matches
slow_pwm_output.cpp
Go to the documentation of this file.
1#include "slow_pwm_output.h"
2#include "esphome/core/log.h"
4
5namespace esphome {
6namespace slow_pwm {
7
8static const char *const TAG = "output.slow_pwm";
9
11 if (this->pin_)
12 this->pin_->setup();
13 this->turn_off();
14}
15
18 if (this->pin_) {
19 this->pin_->digital_write(new_state);
20 }
21 if (new_state != current_state_) {
22 if (this->pin_) {
23 ESP_LOGV(TAG, "Switching output pin %s to %s", this->pin_->dump_summary().c_str(), ONOFF(new_state));
24 } else {
25 ESP_LOGV(TAG, "Switching to %s", ONOFF(new_state));
26 }
27
28 if (this->state_change_trigger_) {
29 this->state_change_trigger_->trigger(new_state);
30 }
31 if (new_state) {
32 if (this->turn_on_trigger_)
33 this->turn_on_trigger_->trigger();
34 } else {
35 if (this->turn_off_trigger_)
36 this->turn_off_trigger_->trigger();
37 }
38 current_state_ = new_state;
39 }
40}
41
43 uint32_t now = App.get_loop_component_start_time();
44 float scaled_state = this->state_ * this->period_;
45
46 if (now - this->period_start_time_ >= this->period_) {
47 ESP_LOGVV(TAG, "End of period. State: %f, Scaled state: %f", this->state_, scaled_state);
48 this->period_start_time_ += this->period_;
49 }
50
51 this->set_output_state_(scaled_state > now - this->period_start_time_);
52}
53
55 ESP_LOGCONFIG(TAG, "Slow PWM Output:");
56 LOG_PIN(" Pin: ", this->pin_);
57 if (this->state_change_trigger_) {
58 ESP_LOGCONFIG(TAG, " State change automation configured");
59 }
60 if (this->turn_on_trigger_) {
61 ESP_LOGCONFIG(TAG, " Turn on automation configured");
62 }
63 if (this->turn_off_trigger_) {
64 ESP_LOGCONFIG(TAG, " Turn off automation configured");
65 }
66 ESP_LOGCONFIG(TAG, " Period: %d ms", this->period_);
67 ESP_LOGCONFIG(TAG, " Restart cycle on state change: %s", YESNO(this->restart_cycle_on_state_change_));
68 LOG_FLOAT_OUTPUT(this);
69}
70
72 this->state_ = state;
74 this->restart_cycle();
75}
76
77} // namespace slow_pwm
78} // namespace esphome
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()=0
virtual std::string dump_summary() const =0
virtual void digital_write(bool value)=0
virtual void turn_off()
Disable this binary output.
void set_output_state_(bool state)
turn on/off the configured output
void setup() override
Initialize pin.
void write_state(float state) override
std::unique_ptr< Trigger<> > turn_on_trigger_
std::unique_ptr< Trigger< bool > > state_change_trigger_
std::unique_ptr< Trigger<> > turn_off_trigger_
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.