ESPHome  2024.12.2
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 
6 namespace esphome {
7 namespace libretiny_pwm {
8 
9 static 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
void setup() override
Setup LibreTinyPWM.
virtual void turn_off()
Disable this binary output.
Definition: binary_output.h:51
virtual void pin_mode(gpio::Flags flags)=0
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
virtual uint8_t get_pin() const =0
uint16_le_t frequency
Definition: bl0942.h:72
void write_state(float state) override
Override FloatOutput&#39;s write_state.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual bool is_inverted() const =0
bool state
Definition: fan.h:34