2019-04-17 12:06:00 +02:00
|
|
|
#include "ledc_output.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
|
|
|
|
#include <esp32-hal-ledc.h>
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace ledc {
|
|
|
|
|
|
|
|
static const char *TAG = "ledc.output";
|
|
|
|
|
|
|
|
void LEDCOutput::write_state(float state) {
|
2019-10-18 11:22:08 +02:00
|
|
|
if (this->pin_->is_inverted())
|
2019-04-17 12:06:00 +02:00
|
|
|
state = 1.0f - state;
|
|
|
|
|
2019-10-18 11:22:08 +02:00
|
|
|
this->duty_ = state;
|
2019-04-17 12:06:00 +02:00
|
|
|
const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
|
|
|
|
const float duty_rounded = roundf(state * max_duty);
|
|
|
|
auto duty = static_cast<uint32_t>(duty_rounded);
|
|
|
|
ledcWrite(this->channel_, duty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LEDCOutput::setup() {
|
2019-10-18 11:22:08 +02:00
|
|
|
this->apply_frequency(this->frequency_);
|
2019-04-17 12:06:00 +02:00
|
|
|
this->turn_off();
|
2019-10-18 11:22:08 +02:00
|
|
|
// Attach pin after setting default value
|
|
|
|
ledcAttachPin(this->pin_->get_pin(), this->channel_);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LEDCOutput::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "LEDC Output:");
|
2019-10-18 11:22:08 +02:00
|
|
|
LOG_PIN(" Pin ", this->pin_);
|
2019-04-17 12:06:00 +02:00
|
|
|
ESP_LOGCONFIG(TAG, " LEDC Channel: %u", this->channel_);
|
|
|
|
ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
|
2019-10-18 11:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float ledc_max_frequency_for_bit_depth(uint8_t bit_depth) { return 80e6f / float(1 << bit_depth); }
|
|
|
|
float ledc_min_frequency_for_bit_depth(uint8_t bit_depth) {
|
|
|
|
const float max_div_num = ((1 << 20) - 1) / 256.0f;
|
|
|
|
return 80e6f / (max_div_num * float(1 << bit_depth));
|
|
|
|
}
|
|
|
|
optional<uint8_t> ledc_bit_depth_for_frequency(float frequency) {
|
|
|
|
for (int i = 20; i >= 1; i--) {
|
|
|
|
const float min_frequency = ledc_min_frequency_for_bit_depth(frequency);
|
|
|
|
const float max_frequency = ledc_max_frequency_for_bit_depth(frequency);
|
|
|
|
if (min_frequency <= frequency && frequency <= max_frequency)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void LEDCOutput::apply_frequency(float frequency) {
|
|
|
|
auto bit_depth_opt = ledc_bit_depth_for_frequency(frequency);
|
|
|
|
if (!bit_depth_opt.has_value()) {
|
|
|
|
ESP_LOGW(TAG, "Frequency %f can't be achieved with any bit depth", frequency);
|
|
|
|
this->status_set_warning();
|
|
|
|
}
|
|
|
|
this->bit_depth_ = *bit_depth_opt;
|
|
|
|
this->frequency_ = frequency;
|
|
|
|
ledcSetup(this->channel_, frequency, this->bit_depth_);
|
|
|
|
// re-apply duty
|
|
|
|
this->write_state(this->duty_);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t next_ledc_channel = 0;
|
|
|
|
|
|
|
|
} // namespace ledc
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|