Oxan van Leeuwen 40c474cd83
Run clang-tidy against ESP32 (#2147)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Otto winter <otto@otto-winter.com>
2021-09-13 18:11:27 +02:00

55 lines
1.3 KiB
C++

#pragma once
#ifdef ARDUINO_ARCH_ESP8266
#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/core/automation.h"
#include "esphome/components/output/float_output.h"
namespace esphome {
namespace esp8266_pwm {
class ESP8266PWM : public output::FloatOutput, public Component {
public:
void set_pin(GPIOPin *pin) { pin_ = pin; }
void set_frequency(float frequency) { this->frequency_ = frequency; }
/// Dynamically update frequency
void update_frequency(float frequency) override {
this->set_frequency(frequency);
this->write_state(this->last_output_);
}
/// 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};
/// 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);
void play(Ts... x) {
float freq = this->frequency_.value(x...);
this->parent_->update_frequency(freq);
}
ESP8266PWM *parent_;
};
} // namespace esp8266_pwm
} // namespace esphome
#endif