From edb557f79e1cf3fc33a93d8da59748ea2c097fb0 Mon Sep 17 00:00:00 2001 From: Philipp Riederer Date: Wed, 22 Sep 2021 19:50:19 +0200 Subject: [PATCH] ledc: do not try to write_state to an uninitialized output (#1732) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Philipp ToĢˆlke Co-authored-by: Otto winter --- esphome/components/ledc/ledc_output.cpp | 11 +++++++++++ esphome/components/ledc/ledc_output.h | 1 + 2 files changed, 12 insertions(+) diff --git a/esphome/components/ledc/ledc_output.cpp b/esphome/components/ledc/ledc_output.cpp index 77610a476f..21a747e34d 100644 --- a/esphome/components/ledc/ledc_output.cpp +++ b/esphome/components/ledc/ledc_output.cpp @@ -31,6 +31,11 @@ optional ledc_bit_depth_for_frequency(float frequency) { } void LEDCOutput::write_state(float state) { + if (!initialized_) { + ESP_LOGW(TAG, "LEDC output hasn't been initialized yet!"); + return; + } + if (this->pin_->is_inverted()) state = 1.0f - state; @@ -81,6 +86,7 @@ void LEDCOutput::setup() { chan_conf.duty = inverted_ == pin_->is_inverted() ? 0 : (1U << bit_depth_); chan_conf.hpoint = 0; ledc_channel_config(&chan_conf); + initialized_ = true; #endif } @@ -101,8 +107,13 @@ void LEDCOutput::update_frequency(float frequency) { this->frequency_ = frequency; #ifdef USE_ARDUINO ledcSetup(this->channel_, frequency, this->bit_depth_); + initialized_ = true; #endif // USE_ARDUINO #ifdef USE_ESP_IDF + if (!initialized_) { + ESP_LOGW(TAG, "LEDC output hasn't been initialized yet!"); + return; + } auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE; auto timer_num = static_cast((channel_ % 8) / 2); diff --git a/esphome/components/ledc/ledc_output.h b/esphome/components/ledc/ledc_output.h index f810ce1e35..e02cefd170 100644 --- a/esphome/components/ledc/ledc_output.h +++ b/esphome/components/ledc/ledc_output.h @@ -36,6 +36,7 @@ class LEDCOutput : public output::FloatOutput, public Component { uint8_t bit_depth_{}; float frequency_{}; float duty_{0.0f}; + bool initialized_ = false; }; template class SetFrequencyAction : public Action {