ESPHome 2026.1.5
Loading...
Searching...
No Matches
slow_pwm_output.cpp
Go to the documentation of this file.
1#include "slow_pwm_output.h"
3#include "esphome/core/gpio.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace slow_pwm {
8
9static const char *const TAG = "output.slow_pwm";
10
12 if (this->pin_)
13 this->pin_->setup();
14 this->turn_off();
15}
16
19 if (this->pin_) {
20 this->pin_->digital_write(new_state);
21 }
22 if (new_state != current_state_) {
23 if (this->pin_) {
24 char pin_summary[GPIO_SUMMARY_MAX_LEN];
25 this->pin_->dump_summary(pin_summary, sizeof(pin_summary));
26 ESP_LOGV(TAG, "Switching output pin %s to %s", pin_summary, ONOFF(new_state));
27 } else {
28 ESP_LOGV(TAG, "Switching to %s", ONOFF(new_state));
29 }
30
31 if (this->state_change_trigger_) {
32 this->state_change_trigger_->trigger(new_state);
33 }
34 if (new_state) {
35 if (this->turn_on_trigger_)
36 this->turn_on_trigger_->trigger();
37 } else {
38 if (this->turn_off_trigger_)
39 this->turn_off_trigger_->trigger();
40 }
41 current_state_ = new_state;
42 }
43}
44
46 uint32_t now = App.get_loop_component_start_time();
47 float scaled_state = this->state_ * this->period_;
48
49 if (now - this->period_start_time_ >= this->period_) {
50 ESP_LOGVV(TAG, "End of period. State: %f, Scaled state: %f", this->state_, scaled_state);
51 this->period_start_time_ += this->period_;
52 }
53
54 this->set_output_state_(scaled_state > now - this->period_start_time_);
55}
56
58 ESP_LOGCONFIG(TAG, "Slow PWM Output:");
59 LOG_PIN(" Pin: ", this->pin_);
60 if (this->state_change_trigger_) {
61 ESP_LOGCONFIG(TAG, " State change automation configured");
62 }
63 if (this->turn_on_trigger_) {
64 ESP_LOGCONFIG(TAG, " Turn on automation configured");
65 }
66 if (this->turn_off_trigger_) {
67 ESP_LOGCONFIG(TAG, " Turn off automation configured");
68 }
69 ESP_LOGCONFIG(TAG,
70 " Period: %d ms\n"
71 " Restart cycle on state change: %s",
72 this->period_, YESNO(this->restart_cycle_on_state_change_));
73 LOG_FLOAT_OUTPUT(this);
74}
75
77 this->state_ = state;
79 this->restart_cycle();
80}
81
82} // namespace slow_pwm
83} // 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 void digital_write(bool value)=0
virtual size_t dump_summary(char *buffer, size_t len) const
Write a summary of this pin to the provided buffer.
Definition gpio.h:129
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
constexpr size_t GPIO_SUMMARY_MAX_LEN
Maximum buffer size for dump_summary output.
Definition gpio.h:13
Application App
Global storage of Application pointer - only one Application can exist.