ESPHome  2024.3.1
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
uint16_t x
Definition: tt21100.cpp:17
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:99
void set_channel(uint8_t channel)
Definition: ledc_output.h:20
uint16_le_t frequency
Definition: bl0942.h:21
void setup() override
Setup LEDC.
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
void dump_config() override
LEDCOutput(InternalGPIOPin *pin)
Definition: ledc_output.h:18
InternalGPIOPin * pin_
Definition: ledc_output.h:35
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_frequency(float frequency)
Definition: ledc_output.h:21
bool state
Definition: fan.h:34