mirror of
https://github.com/esphome/esphome.git
synced 2024-12-02 11:44:13 +01:00
aec02afcdc
* Fix clang-tidy header filter * Allow private members * Fix clang-tidy detections * Run clang-format * Fix remaining detections * Fix graph * Run clang-format
60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include "esphome/core/component.h"
|
|
#include "esphome/core/hal.h"
|
|
#include "esphome/core/automation.h"
|
|
#include "esphome/components/output/float_output.h"
|
|
|
|
#ifdef USE_ESP32
|
|
|
|
namespace esphome {
|
|
namespace ledc {
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
extern uint8_t next_ledc_channel;
|
|
|
|
class LEDCOutput : public output::FloatOutput, public Component {
|
|
public:
|
|
explicit LEDCOutput(InternalGPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
|
|
|
|
void set_channel(uint8_t channel) { this->channel_ = channel; }
|
|
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
|
/// Dynamically change frequency at runtime
|
|
void update_frequency(float frequency) override;
|
|
|
|
/// Setup LEDC.
|
|
void setup() override;
|
|
void dump_config() override;
|
|
/// HARDWARE setup priority
|
|
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
|
|
|
/// Override FloatOutput's write_state.
|
|
void write_state(float state) override;
|
|
|
|
protected:
|
|
InternalGPIOPin *pin_;
|
|
uint8_t channel_{};
|
|
uint8_t bit_depth_{};
|
|
float frequency_{};
|
|
float duty_{0.0f};
|
|
bool initialized_ = false;
|
|
};
|
|
|
|
template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
|
|
public:
|
|
SetFrequencyAction(LEDCOutput *parent) : parent_(parent) {}
|
|
TEMPLATABLE_VALUE(float, frequency);
|
|
|
|
void play(Ts... x) {
|
|
float freq = this->frequency_.value(x...);
|
|
this->parent_->update_frequency(freq);
|
|
}
|
|
|
|
protected:
|
|
LEDCOutput *parent_;
|
|
};
|
|
|
|
} // namespace ledc
|
|
} // namespace esphome
|
|
|
|
#endif
|