ESPHome 2026.5.0b1
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
7
8static const char *const TAG = "libretiny.pwm";
9
11 if (!this->initialized_) {
12 ESP_LOGW(TAG, "LibreTinyPWM output hasn't been initialized yet!");
13 return;
14 }
15
16 if (this->pin_->is_inverted())
17 state = 1.0f - state;
18
19 this->duty_ = state;
20 const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
21 const float duty_rounded = roundf(state * max_duty);
22 auto duty = static_cast<uint32_t>(duty_rounded);
23
24 analogWrite(this->pin_->get_pin(), duty); // NOLINT
25}
26
28 this->update_frequency(this->frequency_);
29 this->turn_off();
30}
31
33 ESP_LOGCONFIG(TAG,
34 "PWM Output:\n"
35 " Frequency: %.1f Hz",
36 this->frequency_);
37 LOG_PIN(" Pin ", this->pin_);
38}
39
41 this->frequency_ = frequency;
42 // force changing the frequency by removing PWM mode
44 analogWriteResolution(this->bit_depth_); // NOLINT
45 analogWriteFrequency(frequency); // NOLINT
46 this->initialized_ = true;
47 // re-apply duty
48 this->write_state(this->duty_);
49}
50
51} // namespace esphome::libretiny_pwm
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:2
@ FLAG_INPUT
Definition gpio.h:27
static void uint32_t