ESPHome  2024.11.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; }
22  void set_phase_angle(float angle) { this->phase_angle_ = angle; }
24  void update_frequency(float frequency) override;
25 
27  void setup() override;
28  void dump_config() override;
30  float get_setup_priority() const override { return setup_priority::HARDWARE; }
31 
33  void write_state(float state) override;
34 
35  protected:
37  uint8_t channel_{};
38  uint8_t bit_depth_{};
39  float phase_angle_{0.0f};
40  float frequency_{};
41  float duty_{0.0f};
42  bool initialized_ = false;
43 };
44 
45 template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
46  public:
47  SetFrequencyAction(LEDCOutput *parent) : parent_(parent) {}
48  TEMPLATABLE_VALUE(float, frequency);
49 
50  void play(Ts... x) {
51  float freq = this->frequency_.value(x...);
52  this->parent_->update_frequency(freq);
53  }
54 
55  protected:
57 };
58 
59 } // namespace ledc
60 } // namespace esphome
61 
62 #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:47
float get_setup_priority() const override
HARDWARE setup priority.
Definition: ledc_output.h:30
uint8_t next_ledc_channel
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
void set_phase_angle(float angle)
Definition: ledc_output.h:22
void write_state(float state) override
Override FloatOutput&#39;s write_state.
void set_channel(uint8_t channel)
Definition: ledc_output.h:20
uint16_le_t frequency
Definition: bl0942.h:72
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:36
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_frequency(float frequency)
Definition: ledc_output.h:21
bool state
Definition: fan.h:34