ESPHome  2022.6.2
esp8266_pwm.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP8266
4 
6 #include "esphome/core/hal.h"
9 
10 namespace esphome {
11 namespace esp8266_pwm {
12 
13 class ESP8266PWM : public output::FloatOutput, public Component {
14  public:
15  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
16  void set_frequency(float frequency) { this->frequency_ = frequency; }
18  void update_frequency(float frequency) override {
19  this->set_frequency(frequency);
20  this->write_state(this->last_output_);
21  }
22 
24  void setup() override;
25  void dump_config() override;
27  float get_setup_priority() const override { return setup_priority::HARDWARE; }
28 
29  protected:
30  void write_state(float state) override;
31 
33  float frequency_{1000.0};
35  float last_output_{0.0};
36 };
37 
38 template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
39  public:
40  SetFrequencyAction(ESP8266PWM *parent) : parent_(parent) {}
41  TEMPLATABLE_VALUE(float, frequency);
42 
43  void play(Ts... x) {
44  float freq = this->frequency_.value(x...);
45  this->parent_->update_frequency(freq);
46  }
47 
49 };
50 
51 } // namespace esp8266_pwm
52 } // namespace esphome
53 
54 #endif
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
float get_setup_priority() const override
HARDWARE setup_priority.
Definition: esp8266_pwm.h:27
void write_state(float state) override
Definition: esp8266_pwm.cpp:27
float last_output_
Cache last output level for dynamic frequency updating.
Definition: esp8266_pwm.h:35
void set_pin(InternalGPIOPin *pin)
Definition: esp8266_pwm.h:15
void setup() override
Initialize pin.
Definition: esp8266_pwm.cpp:16
void update_frequency(float frequency) override
Dynamically update frequency.
Definition: esp8266_pwm.h:18
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
Definition: a4988.cpp:4
void set_frequency(float frequency)
Definition: esp8266_pwm.h:16
bool state
Definition: fan.h:34