mirror of
https://github.com/esphome/esphome.git
synced 2024-12-02 11:44:13 +01:00
a9630ac847
Co-authored-by: Kuba Szczodrzyński <kuba@szczodrzynski.pl> Co-authored-by: Sam Neirinck <git@samneirinck.com> Co-authored-by: David Buezas <dbuezas@users.noreply.github.com> Co-authored-by: Stroe Andrei Catalin <catalin2402@gmail.com> Co-authored-by: Sam Neirinck <github@samneirinck.be> Co-authored-by: Péter Sárközi <xmisterhu@gmail.com> Co-authored-by: Hajo Noerenberg <hn@users.noreply.github.com>
55 lines
1.3 KiB
C++
55 lines
1.3 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_LIBRETINY
|
|
|
|
namespace esphome {
|
|
namespace libretiny_pwm {
|
|
|
|
class LibreTinyPWM : public output::FloatOutput, public Component {
|
|
public:
|
|
explicit LibreTinyPWM(InternalGPIOPin *pin) : pin_(pin) {}
|
|
|
|
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
|
/// Dynamically change frequency at runtime
|
|
void update_frequency(float frequency) override;
|
|
|
|
/// Setup LibreTinyPWM.
|
|
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 bit_depth_{10};
|
|
float frequency_{};
|
|
float duty_{0.0f};
|
|
bool initialized_ = false;
|
|
};
|
|
|
|
template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
|
|
public:
|
|
SetFrequencyAction(LibreTinyPWM *parent) : parent_(parent) {}
|
|
TEMPLATABLE_VALUE(float, frequency);
|
|
|
|
void play(Ts... x) {
|
|
float freq = this->frequency_.value(x...);
|
|
this->parent_->update_frequency(freq);
|
|
}
|
|
|
|
protected:
|
|
LibreTinyPWM *parent_;
|
|
};
|
|
|
|
} // namespace libretiny_pwm
|
|
} // namespace esphome
|
|
|
|
#endif
|