mirror of
https://github.com/esphome/esphome.git
synced 2025-09-16 18:22:22 +01:00
Initial Support for RP2040 platform (#3284)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
57
esphome/components/rp2040_pwm/rp2040_pwm.h
Normal file
57
esphome/components/rp2040_pwm/rp2040_pwm.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_RP2040
|
||||
|
||||
#include "esphome/components/output/float_output.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
#include "drivers/PwmOut.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace rp2040_pwm {
|
||||
|
||||
class RP2040PWM : public output::FloatOutput, public Component {
|
||||
public:
|
||||
void set_pin(InternalGPIOPin *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;
|
||||
|
||||
InternalGPIOPin *pin_;
|
||||
mbed::PwmOut *pwm_;
|
||||
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(RP2040PWM *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(float, frequency);
|
||||
|
||||
void play(Ts... x) {
|
||||
float freq = this->frequency_.value(x...);
|
||||
this->parent_->update_frequency(freq);
|
||||
}
|
||||
|
||||
RP2040PWM *parent_;
|
||||
};
|
||||
|
||||
} // namespace rp2040_pwm
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_RP2040
|
Reference in New Issue
Block a user