esphome/esphome/components/libretiny_pwm/libretiny_pwm.h
Kuba Szczodrzyński a9630ac847
Support for LibreTiny platform (RTL8710, BK7231 & other modules) (#3509)
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>
2023-09-05 10:16:08 +12:00

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