ESPHome  2024.12.2
rp2040_pwm.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_RP2040
4 
8 #include "esphome/core/hal.h"
9 
10 namespace esphome {
11 namespace rp2040_pwm {
12 
13 class RP2040PWM : public output::FloatOutput, public Component {
14  public:
15  void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
16  void set_frequency(float frequency) { this->frequency_ = frequency; }
18  void update_frequency(float frequency) override {
19  this->set_frequency(frequency);
20  this->frequency_changed_ = true;
21  this->write_state(this->last_output_);
22  }
23 
25  void setup() override;
26  void dump_config() override;
28  float get_setup_priority() const override { return setup_priority::HARDWARE; }
29 
30  protected:
31  void write_state(float state) override;
32 
33  void setup_pwm_();
34 
36  float frequency_{1000.0};
37  uint16_t wrap_{65535};
39  float last_output_{0.0};
40  bool frequency_changed_{false};
41 };
42 
43 template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
44  public:
45  SetFrequencyAction(RP2040PWM *parent) : parent_(parent) {}
46  TEMPLATABLE_VALUE(float, frequency);
47 
48  void play(Ts... x) {
49  float freq = this->frequency_.value(x...);
50  this->parent_->update_frequency(freq);
51  }
52 
54 };
55 
56 } // namespace rp2040_pwm
57 } // namespace esphome
58 
59 #endif // USE_RP2040
void update_frequency(float frequency) override
Dynamically update frequency.
Definition: rp2040_pwm.h:18
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
void setup() override
Initialize pin.
Definition: rp2040_pwm.cpp:19
uint16_t x
Definition: tt21100.cpp:17
void set_frequency(float frequency)
Definition: rp2040_pwm.h:16
float get_setup_priority() const override
HARDWARE setup_priority.
Definition: rp2040_pwm.h:28
InternalGPIOPin * pin_
Definition: rp2040_pwm.h:35
void write_state(float state) override
Definition: rp2040_pwm.cpp:48
void set_pin(InternalGPIOPin *pin)
Definition: rp2040_pwm.h:15
uint16_le_t frequency
Definition: bl0942.h:72
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float last_output_
Cache last output level for dynamic frequency updating.
Definition: rp2040_pwm.h:39
bool state
Definition: fan.h:34