ESPHome  2022.6.2
ledc_output.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
7 
8 #ifdef USE_ESP32
9 
10 namespace esphome {
11 namespace ledc {
12 
13 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
14 extern uint8_t next_ledc_channel;
15 
16 class LEDCOutput : public output::FloatOutput, public Component {
17  public:
18  explicit LEDCOutput(InternalGPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
19 
20  void set_channel(uint8_t channel) { this->channel_ = channel; }
21  void set_frequency(float frequency) { this->frequency_ = frequency; }
23  void update_frequency(float frequency) override;
24 
26  void setup() override;
27  void dump_config() override;
29  float get_setup_priority() const override { return setup_priority::HARDWARE; }
30 
32  void write_state(float state) override;
33 
34  protected:
36  uint8_t channel_{};
37  uint8_t bit_depth_{};
38  float frequency_{};
39  float duty_{0.0f};
40  bool initialized_ = false;
41 };
42 
43 template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
44  public:
45  SetFrequencyAction(LEDCOutput *parent) : parent_(parent) {}
46  TEMPLATABLE_VALUE(float, frequency);
47 
48  void play(Ts... x) {
49  float freq = this->frequency_.value(x...);
50  this->parent_->update_frequency(freq);
51  }
52 
53  protected:
55 };
56 
57 } // namespace ledc
58 } // namespace esphome
59 
60 #endif
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
SetFrequencyAction(LEDCOutput *parent)
Definition: ledc_output.h:45
float get_setup_priority() const override
HARDWARE setup priority.
Definition: ledc_output.h:29
uint8_t next_ledc_channel
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
void write_state(float state) override
Override FloatOutput&#39;s write_state.
Definition: ledc_output.cpp:53
void set_channel(uint8_t channel)
Definition: ledc_output.h:20
void setup() override
Setup LEDC.
Definition: ledc_output.cpp:78
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void dump_config() override
LEDCOutput(InternalGPIOPin *pin)
Definition: ledc_output.h:18
InternalGPIOPin * pin_
Definition: ledc_output.h:35
Definition: a4988.cpp:4
void set_frequency(float frequency)
Definition: ledc_output.h:21
bool state
Definition: fan.h:34