2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/core/esphal.h"
|
2019-04-22 21:56:30 +02:00
|
|
|
#include "esphome/core/automation.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/components/output/float_output.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace esp8266_pwm {
|
|
|
|
|
|
|
|
class ESP8266PWM : public output::FloatOutput, public Component {
|
|
|
|
public:
|
2019-04-22 21:56:30 +02:00
|
|
|
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
2019-04-17 12:06:00 +02:00
|
|
|
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
2019-04-22 21:56:30 +02:00
|
|
|
/// Dynamically update frequency
|
2020-07-25 12:57:11 -03:00
|
|
|
void update_frequency(float frequency) override {
|
2019-04-22 21:56:30 +02:00
|
|
|
this->set_frequency(frequency);
|
|
|
|
this->write_state(this->last_output_);
|
|
|
|
}
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
/// Initialize pin
|
|
|
|
void setup() override;
|
|
|
|
void dump_config() override;
|
|
|
|
/// HARDWARE setup_priority
|
|
|
|
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void write_state(float state) override;
|
|
|
|
|
|
|
|
GPIOPin *pin_;
|
|
|
|
float frequency_{1000.0};
|
2019-04-22 21:56:30 +02:00
|
|
|
/// Cache last output level for dynamic frequency updating
|
|
|
|
float last_output_{0.0};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
|
|
|
|
public:
|
|
|
|
SetFrequencyAction(ESP8266PWM *parent) : parent_(parent) {}
|
|
|
|
TEMPLATABLE_VALUE(float, frequency);
|
|
|
|
|
2020-05-24 23:27:28 -03:00
|
|
|
void play(Ts... x) {
|
2019-04-22 21:56:30 +02:00
|
|
|
float freq = this->frequency_.value(x...);
|
|
|
|
this->parent_->update_frequency(freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP8266PWM *parent_;
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace esp8266_pwm
|
|
|
|
} // namespace esphome
|