ESPHome 2025.5.0
Loading...
Searching...
No Matches
libretiny_pwm.cpp
Go to the documentation of this file.
1#include "libretiny_pwm.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_LIBRETINY
5
6namespace esphome {
7namespace libretiny_pwm {
8
9static const char *const TAG = "libretiny.pwm";
10
12 if (!this->initialized_) {
13 ESP_LOGW(TAG, "LibreTinyPWM output hasn't been initialized yet!");
14 return;
15 }
16
17 if (this->pin_->is_inverted())
18 state = 1.0f - state;
19
20 this->duty_ = state;
21 const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
22 const float duty_rounded = roundf(state * max_duty);
23 auto duty = static_cast<uint32_t>(duty_rounded);
24
25 analogWrite(this->pin_->get_pin(), duty); // NOLINT
26}
27
29 this->update_frequency(this->frequency_);
30 this->turn_off();
31}
32
34 ESP_LOGCONFIG(TAG, "PWM Output:");
35 LOG_PIN(" Pin ", this->pin_);
36 ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
37}
38
40 this->frequency_ = frequency;
41 // force changing the frequency by removing PWM mode
43 analogWriteResolution(this->bit_depth_); // NOLINT
44 analogWriteFrequency(frequency); // NOLINT
45 this->initialized_ = true;
46 // re-apply duty
47 this->write_state(this->duty_);
48}
49
50} // namespace libretiny_pwm
51} // namespace esphome
52
53#endif
uint16_le_t frequency
Definition bl0942.h:6
virtual void pin_mode(gpio::Flags flags)=0
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
void write_state(float state) override
Override FloatOutput's write_state.
void setup() override
Setup LibreTinyPWM.
virtual void turn_off()
Disable this binary output.
bool state
Definition fan.h:0
@ FLAG_INPUT
Definition gpio.h:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7