ESPHome  2024.12.2
slow_pwm_output.h
Go to the documentation of this file.
1 #pragma once
4 #include "esphome/core/hal.h"
6 
7 namespace esphome {
8 namespace slow_pwm {
9 
11  public:
12  void set_pin(GPIOPin *pin) { pin_ = pin; };
13  void set_period(unsigned int period) { period_ = period; };
14  void set_restart_cycle_on_state_change(bool restart_cycle_on_state_change) {
15  restart_cycle_on_state_change_ = restart_cycle_on_state_change;
16  }
17  void restart_cycle() { this->period_start_time_ = millis(); }
18 
20  void setup() override;
21  void dump_config() override;
23  float get_setup_priority() const override { return setup_priority::HARDWARE; }
24 
26  // Lazy create
27  if (!this->turn_on_trigger_)
28  this->turn_on_trigger_ = make_unique<Trigger<>>();
29  return this->turn_on_trigger_.get();
30  }
32  if (!this->turn_off_trigger_)
33  this->turn_off_trigger_ = make_unique<Trigger<>>();
34  return this->turn_off_trigger_.get();
35  }
36 
38  if (!this->state_change_trigger_)
39  this->state_change_trigger_ = make_unique<Trigger<bool>>();
40  return this->state_change_trigger_.get();
41  }
42 
43  protected:
44  void loop() override;
45  void write_state(float state) override;
47  void set_output_state_(bool state);
48 
49  GPIOPin *pin_{nullptr};
50  std::unique_ptr<Trigger<>> turn_on_trigger_{nullptr};
51  std::unique_ptr<Trigger<>> turn_off_trigger_{nullptr};
52  std::unique_ptr<Trigger<bool>> state_change_trigger_{nullptr};
53  float state_{0};
54  bool current_state_{false};
55  unsigned int period_start_time_{0};
56  unsigned int period_;
58 };
59 
60 } // namespace slow_pwm
61 } // namespace esphome
void write_state(float state) override
std::unique_ptr< Trigger<> > turn_off_trigger_
float get_setup_priority() const override
HARDWARE setup_priority.
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
std::unique_ptr< Trigger<> > turn_on_trigger_
void set_output_state_(bool state)
turn on/off the configured output
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void set_period(unsigned int period)
void set_restart_cycle_on_state_change(bool restart_cycle_on_state_change)
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
Trigger< bool > * get_state_change_trigger()
void setup() override
Initialize pin.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::unique_ptr< Trigger< bool > > state_change_trigger_
bool state
Definition: fan.h:34