diff --git a/esphome/components/es7210/es7210.cpp b/esphome/components/es7210/es7210.cpp index 4d1f2171e7..9a99e60995 100644 --- a/esphome/components/es7210/es7210.cpp +++ b/esphome/components/es7210/es7210.cpp @@ -89,7 +89,7 @@ void ES7210::setup() { } bool ES7210::set_mic_gain(float mic_gain) { - this->mic_gain_ = clamp(mic_gain, ES7210_MIC_GAINS[0], ES7210_MIC_GAINS[ES7210_MIC_GAIN_COUNT - 1]); + this->mic_gain_ = clamp(mic_gain, ES7210_MIC_GAIN_MIN, ES7210_MIC_GAIN_MAX); if (this->setup_complete_) { return this->configure_mic_gain_(); } @@ -169,14 +169,18 @@ bool ES7210::configure_mic_gain_() { } uint8_t ES7210::es7210_gain_reg_value_(float mic_gain) { - for (uint8_t i = 0; i < ES7210_MIC_GAIN_COUNT; i++) { - if (mic_gain == ES7210_MIC_GAINS[i]) { - return i; - } else if (mic_gain < ES7210_MIC_GAINS[i]) { - return i - 1; - } + // reg: 12 - 34.5dB, 13 - 36dB, 14 - 37.5dB + mic_gain += 0.5; + if (mic_gain <= 33.0) { + return (uint8_t) mic_gain / 3; } - return ES7210_MIC_GAIN_COUNT - 1; + if (mic_gain < 36.0) { + return 12; + } + if (mic_gain < 37.0) { + return 13; + } + return 14; } bool ES7210::configure_i2s_format_() { diff --git a/esphome/components/es7210/es7210.h b/esphome/components/es7210/es7210.h index 647c27ed52..8f6d9d8136 100644 --- a/esphome/components/es7210/es7210.h +++ b/esphome/components/es7210/es7210.h @@ -33,7 +33,6 @@ class ES7210 : public audio_adc::AudioAdc, public Component, public i2c::I2CDevi void set_sample_rate(uint32_t sample_rate) { this->sample_rate_ = sample_rate; } float mic_gain() override { return this->mic_gain_; }; - float actual_mic_gain() { return ES7210_MIC_GAINS[this->es7210_gain_reg_value_(this->mic_gain_)]; } protected: /// @brief Updates an I2C registry address by modifying the current state @@ -45,7 +44,7 @@ class ES7210 : public audio_adc::AudioAdc, public Component, public i2c::I2CDevi /// @brief Convert floating point mic gain value to register value /// @param mic_gain Gain value to convert - /// @return Corresponding register value closest to but not exceeding specified gain + /// @return Corresponding register value for specified gain uint8_t es7210_gain_reg_value_(float mic_gain); bool configure_i2s_format_(); diff --git a/esphome/components/es7210/es7210_const.h b/esphome/components/es7210/es7210_const.h index 5657f582f5..e5ffea5743 100644 --- a/esphome/components/es7210/es7210_const.h +++ b/esphome/components/es7210/es7210_const.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace esphome { namespace es7210 { @@ -120,10 +122,8 @@ static const ES7210Coefficient ES7210_COEFFICIENTS[] = { {19200000, 96000, 0x01, 0x05, 0x00, 0x01, 0x28, 0x00, 0x00, 0xc8}, }; -static const uint8_t ES7210_MIC_GAIN_COUNT = 15; -static const float ES7210_MIC_GAINS[ES7210_MIC_GAIN_COUNT] = { - 0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 34.5, 36.0, 37.5, -}; +static const float ES7210_MIC_GAIN_MIN = 0.0; +static const float ES7210_MIC_GAIN_MAX = 37.5; } // namespace es7210 } // namespace esphome