ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ledc_output.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
6#include <cstdint>
7
8#ifdef USE_ESP32
9
10namespace esphome::ledc {
11
12// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
13extern uint8_t next_ledc_channel;
14
15class LEDCOutput final : public output::FloatOutput, public Component {
16 public:
17 explicit LEDCOutput(InternalGPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
18
19 void set_channel(uint8_t channel) { this->channel_ = channel; }
21 void set_phase_angle(float angle) { this->phase_angle_ = angle; }
22#ifdef USE_LEDC_DITHERING
23 void set_dithering(bool dithering) { this->dithering_ = dithering; }
24#endif
26 void update_frequency(float frequency) override;
27
29 void setup() override;
30 void loop() override;
31 void dump_config() override;
33 float get_setup_priority() const override { return setup_priority::HARDWARE; }
34
36 void write_state(float state) override;
37
38 protected:
39 void write_duty_();
40
42 uint8_t channel_{};
43 uint8_t bit_depth_{};
44 bool initialized_ = false;
45#ifdef USE_LEDC_DITHERING
46 bool dithering_{false};
47#endif
48 float phase_angle_{0.0f};
49 float frequency_{};
50 float duty_{0.0f};
51 // Duty in sixteenths of a timer tick when dithering is enabled, otherwise whole ticks.
52 uint32_t last_duty_{UINT32_MAX};
53};
54
55} // namespace esphome::ledc
56
57#endif
uint16_le_t frequency
Definition bl0942.h:6
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
void set_dithering(bool dithering)
Definition ledc_output.h:23
float get_setup_priority() const override
HARDWARE setup priority.
Definition ledc_output.h:33
void set_phase_angle(float angle)
Definition ledc_output.h:21
void set_frequency(float frequency)
Definition ledc_output.h:20
void setup() override
Setup LEDC.
void write_state(float state) override
Override FloatOutput's write_state.
LEDCOutput(InternalGPIOPin *pin)
Definition ledc_output.h:17
void set_channel(uint8_t channel)
Definition ledc_output.h:19
void dump_config() override
InternalGPIOPin * pin_
Definition ledc_output.h:41
Base class for all output components that can output a variable level, like PWM.
bool state
Definition fan.h:2
uint8_t next_ledc_channel
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
static void uint32_t