ESPHome 2026.6.0-dev
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
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 char pin_summary[GPIO_SUMMARY_MAX_LEN];
24 this->pin_->dump_summary(pin_summary, sizeof(pin_summary));
25 ESP_LOGV(TAG, "Switching output pin %s to %s", pin_summary, ONOFF(new_state));
26 } else {
27 ESP_LOGV(TAG, "Switching to %s", ONOFF(new_state));
28 }
29
30 if (this->state_change_trigger_) {
31 this->state_change_trigger_->trigger(new_state);
32 }
33 if (new_state) {
34 if (this->turn_on_trigger_)
35 this->turn_on_trigger_->trigger();
36 } else {
37 if (this->turn_off_trigger_)
38 this->turn_off_trigger_->trigger();
39 }
40 current_state_ = new_state;
41 }
42}
43
46 float scaled_state = this->state_ * this->period_;
47
48 if (now - this->period_start_time_ >= this->period_) {
49 ESP_LOGVV(TAG, "End of period. State: %f, Scaled state: %f", this->state_, scaled_state);
50 this->period_start_time_ += this->period_;
51 }
52
53 this->set_output_state_(scaled_state > now - this->period_start_time_);
54}
55
57 ESP_LOGCONFIG(TAG, "Slow PWM Output:");
58 LOG_PIN(" Pin: ", this->pin_);
59 if (this->state_change_trigger_) {
60 ESP_LOGCONFIG(TAG, " State change automation configured");
61 }
62 if (this->turn_on_trigger_) {
63 ESP_LOGCONFIG(TAG, " Turn on automation configured");
64 }
65 if (this->turn_off_trigger_) {
66 ESP_LOGCONFIG(TAG, " Turn off automation configured");
67 }
68 ESP_LOGCONFIG(TAG,
69 " Period: %d ms\n"
70 " Restart cycle on state change: %s",
71 this->period_, YESNO(this->restart_cycle_on_state_change_));
72 LOG_FLOAT_OUTPUT(this);
73}
74
76 this->state_ = state;
78 this->restart_cycle();
79}
80
81} // namespace esphome::slow_pwm
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:2
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.
static void uint32_t