2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
2021-09-20 11:47:51 +02:00
|
|
|
#include "esphome/core/hal.h"
|
2019-10-18 11:22:08 +02:00
|
|
|
#include "esphome/core/automation.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/components/output/float_output.h"
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace ledc {
|
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2019-04-24 14:30:09 +02:00
|
|
|
extern uint8_t next_ledc_channel;
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
class LEDCOutput : public output::FloatOutput, public Component {
|
|
|
|
public:
|
2021-09-20 11:47:51 +02:00
|
|
|
explicit LEDCOutput(InternalGPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
void set_channel(uint8_t channel) { this->channel_ = channel; }
|
|
|
|
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
2024-03-20 09:17:32 +01:00
|
|
|
void set_phase_angle(float angle) { this->phase_angle_ = angle; }
|
2019-10-18 11:22:08 +02:00
|
|
|
/// Dynamically change frequency at runtime
|
2020-07-25 17:57:11 +02:00
|
|
|
void update_frequency(float frequency) override;
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
/// 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.
|
2019-10-18 11:22:08 +02:00
|
|
|
void write_state(float state) override;
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
protected:
|
2021-09-20 11:47:51 +02:00
|
|
|
InternalGPIOPin *pin_;
|
2019-04-17 12:06:00 +02:00
|
|
|
uint8_t channel_{};
|
|
|
|
uint8_t bit_depth_{};
|
2024-03-20 09:17:32 +01:00
|
|
|
float phase_angle_{0.0f};
|
2019-04-17 12:06:00 +02:00
|
|
|
float frequency_{};
|
2019-10-18 11:22:08 +02:00
|
|
|
float duty_{0.0f};
|
2021-09-22 19:50:19 +02:00
|
|
|
bool initialized_ = false;
|
2019-10-18 11:22:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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...);
|
2020-07-25 17:57:11 +02:00
|
|
|
this->parent_->update_frequency(freq);
|
2019-10-18 11:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
LEDCOutput *parent_;
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ledc
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|