From fb0d5806881a08dc8436ce8d582ae08730cb877b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Poczkodi?= Date: Fri, 18 Oct 2024 23:15:46 +0200 Subject: [PATCH] config restructuring --- esphome/components/si4713_i2c/__init__.py | 11 ++++++---- .../components/si4713_i2c/number/__init__.py | 8 +++---- esphome/components/si4713_i2c/si4713.cpp | 21 ++++++++++++++----- esphome/components/si4713_i2c/si4713.h | 4 +++- esphome/components/si4713_i2c/si4713defs.h | 4 ++-- .../components/si4713_i2c/switch/__init__.py | 17 +++++++++++++++ .../si4713_i2c/switch/power_enable_switch.cpp | 12 +++++++++++ .../si4713_i2c/switch/power_enable_switch.h | 18 ++++++++++++++++ .../si4713_i2c/test.esp32-s3-ard.yaml | 4 ++++ .../si4713_i2c/test.esp32-s3-idf.yaml | 4 ++++ 10 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 esphome/components/si4713_i2c/switch/power_enable_switch.cpp create mode 100644 esphome/components/si4713_i2c/switch/power_enable_switch.h diff --git a/esphome/components/si4713_i2c/__init__.py b/esphome/components/si4713_i2c/__init__.py index f5bd5fce46..90c1723732 100644 --- a/esphome/components/si4713_i2c/__init__.py +++ b/esphome/components/si4713_i2c/__init__.py @@ -65,6 +65,7 @@ CONF_MUTE = "mute" CONF_MONO = "mono" CONF_PRE_EMPHASIS = "pre_emphasis" # tuner +CONF_ENABLE = "enable" # CONF_FREQUENCY = "frequency" CONF_DEVIATION = "deviation" # CONF_POWER = "power" @@ -79,7 +80,7 @@ CONF_SAMPLE_BITS = "sample_bits" # CONF_MODE = "mode" CONF_CLOCK_EDGE = "clock_edge" # pilot -CONF_ENABLE = "enable" +# CONF_ENABLE = "enable" # CONF_FREQUENCY = "frequency" CONF_DEVIATION = "deviation" # refclk @@ -135,7 +136,7 @@ MeasureFrequencyAction = si4713_ns.class_( OpMode = si4713_ns.enum("OpMode", True) OP_MODE = { "Analog": OpMode.OPMODE_ANALOG, - "Digital": OpMode.OPMODE_Digital, + "Digital": OpMode.OPMODE_DIGITAL, } PreEmphasis = si4713_ns.enum("PreEmphasis", True) @@ -220,17 +221,18 @@ ACOMP_PRESET = { TUNER_SCHEMA = cv.Schema( { + cv.Optional(CONF_ENABLE, default="True"): cv.boolean, cv.Optional(CONF_FREQUENCY, default=87.50): cv.float_range(76, 108), # MHz cv.Optional(CONF_DEVIATION, default=68.25): cv.float_range(0, 90), # kHz cv.Optional(CONF_POWER, default=115): cv.int_range(88, 115), - cv.Optional(CONF_ANTCAP, default=0): cv.int_range(0, 191), + cv.Optional(CONF_ANTCAP, default=0): cv.float_range(0, 47.75), # pF } ) ANALOG_SCHEMA = cv.Schema( { cv.Optional(CONF_LEVEL, default=636): cv.int_range(0, 1023), - cv.Optional(CONF_ATTENUATION, default=636): cv.enum(LINE_ATTENUATION), + cv.Optional(CONF_ATTENUATION, default="60kOhm"): cv.enum(LINE_ATTENUATION), } ) @@ -434,6 +436,7 @@ async def to_code(config): await set_var(config, CONF_MONO, var.set_mono) await set_var(config, CONF_PRE_EMPHASIS, var.set_pre_emphasis) if tuner_config := config.get(CONF_TUNER): + await set_var(tuner_config, CONF_ENABLE, var.set_power_enable) await set_var(tuner_config, CONF_FREQUENCY, var.set_frequency) await set_var(tuner_config, CONF_DEVIATION, var.set_audio_deviation) await set_var(tuner_config, CONF_POWER, var.set_power) diff --git a/esphome/components/si4713_i2c/number/__init__.py b/esphome/components/si4713_i2c/number/__init__.py index c6ec70b9f7..e3b73bce60 100644 --- a/esphome/components/si4713_i2c/number/__init__.py +++ b/esphome/components/si4713_i2c/number/__init__.py @@ -236,10 +236,10 @@ async def to_code(config): if tuner_config := config.get(CONF_TUNER): await new_number(p, tuner_config, CONF_FREQUENCY, p.set_frequency_number, 76, 108, 0.05) await new_number(p, tuner_config, CONF_DEVIATION, p.set_audio_deviation_number, 0, 90, 0.01) - await new_number(p, tuner_config, CONF_POWER, p.set_power_number, 88, 120, 1, mode = number.NumberMode.NUMBER_MODE_SLIDER) - await new_number(p, tuner_config, CONF_ANTCAP, p.set_antcap_number, 0, 191, 1, mode = number.NumberMode.NUMBER_MODE_SLIDER) + await new_number(p, tuner_config, CONF_POWER, p.set_power_number, 88, 120, 1, mode=number.NumberMode.NUMBER_MODE_SLIDER) + await new_number(p, tuner_config, CONF_ANTCAP, p.set_antcap_number, 0, 47.75, 0.25) if analog_config := config.get(CONF_ANALOG): - await new_number(p, analog_config, CONF_LEVEL, p.set_analog_level_number, 0, 1023, 1, mode = number.NumberMode.NUMBER_MODE_SLIDER) + await new_number(p, analog_config, CONF_LEVEL, p.set_analog_level_number, 0, 1023, 1, mode=number.NumberMode.NUMBER_MODE_SLIDER) if digital_config := config.get(CONF_DIGITAL): await new_number(p, digital_config, CONF_SAMPLE_RATE, p.set_digital_sample_rate_number, 32000, 48000, 1) if pilot_config := config.get(CONF_PILOT): @@ -252,7 +252,7 @@ async def to_code(config): await new_number(p, compressor_config, CONF_THRESHOLD, p.set_acomp_threshold_number, -40, 0, 1) await new_number(p, compressor_config, CONF_GAIN, p.set_acomp_gain_number, 0, 20, 1) if limiter_config := config.get(CONF_LIMITER): - await new_number(p, limiter_config, CONF_RELEASE_TIME, p.set_limiter_release_time_number, 0.25, 102.4, 0.01, mode = number.NumberMode.NUMBER_MODE_SLIDER) + await new_number(p, limiter_config, CONF_RELEASE_TIME, p.set_limiter_release_time_number, 0.25, 102.4, 0.01, mode=number.NumberMode.NUMBER_MODE_SLIDER) if asq_config := config.get(CONF_ASQ): await new_number(p, asq_config, CONF_LEVEL_LOW, p.set_asq_level_low_number, -70, 0, 1) await new_number(p, asq_config, CONF_DURATION_LOW, p.set_asq_duration_low_number, 0, 65535, 1) diff --git a/esphome/components/si4713_i2c/si4713.cpp b/esphome/components/si4713_i2c/si4713.cpp index 304a104e61..95ad74d693 100644 --- a/esphome/components/si4713_i2c/si4713.cpp +++ b/esphome/components/si4713_i2c/si4713.cpp @@ -16,6 +16,7 @@ Si4713Component::Si4713Component() { this->reset_pin_ = nullptr; this->reset_ = false; this->op_mode_ = OpMode::OPMODE_ANALOG; + this->power_enable_ = true; this->frequency_ = 8750; this->power_ = 115; this->antcap_ = 0; @@ -129,7 +130,7 @@ bool Si4713Component::detect_chip_id() { bool Si4713Component::tune_freq(uint16_t freq) { return this->send_cmd(CmdTxTuneFreq(freq)) && this->stc_wait(); } bool Si4713Component::tune_power(uint8_t power, uint8_t antcap) { - return this->send_cmd(CmdTxTunePower(power, antcap)) && this->stc_wait(); + return this->send_cmd(CmdTxTunePower(this->power_enable_ ? power : 0, antcap)) && this->stc_wait(); } bool Si4713Component::stc_wait() { @@ -218,6 +219,7 @@ void Si4713Component::setup() { this->publish_mute(); this->publish_mono(); this->publish_pre_emphasis(); + this->publish_power_enable(); this->publish_frequency(); this->publish_audio_deviation(); this->publish_power(); @@ -384,6 +386,15 @@ void Si4713Component::set_pre_emphasis(PreEmphasis value) { PreEmphasis Si4713Component::get_pre_emphasis() { return (PreEmphasis) tx_pre_emphasis_.FMPE; } +void Si4713Component::set_power_enable(bool value) { + this->power_enable_ = value; + this->tune_power(this->power_, this->antcap_); + // this->set_prop(this->tx_component_enable_); + this->publish_power_enable(); +} + +bool Si4713Component::get_power_enable() { return this->power_enable_; } + void Si4713Component::set_frequency(float value) { CHECK_FLOAT_RANGE(value, FREQ_MIN, FREQ_MAX) this->frequency_ = (uint16_t) clamp((int) std::lround(value * 20) * 5, FREQ_RAW_MIN, FREQ_RAW_MAX); @@ -411,14 +422,14 @@ void Si4713Component::set_power(int value) { int Si4713Component::get_power() { return (int) this->power_; } -void Si4713Component::set_antcap(int value) { - CHECK_INT_RANGE(value, ANTCAP_MIN, ANTCAP_MAX) - this->antcap_ = (uint8_t) value; +void Si4713Component::set_antcap(float value) { + CHECK_FLOAT_RANGE(value, ANTCAP_MIN, ANTCAP_MAX) + this->antcap_ = (uint8_t) lround(value * 4); this->tune_power(this->power_, this->antcap_); this->publish_antcap(); } -int Si4713Component::get_antcap() { return (int) this->antcap_; } +float Si4713Component::get_antcap() { return (float) this->antcap_ * 0.25f; } void Si4713Component::set_analog_level(int value) { CHECK_INT_RANGE(value, LILEVEL_MIN, LILEVEL_MAX) diff --git a/esphome/components/si4713_i2c/si4713.h b/esphome/components/si4713_i2c/si4713.h index 6ce02f1f15..7ffc945a5d 100644 --- a/esphome/components/si4713_i2c/si4713.h +++ b/esphome/components/si4713_i2c/si4713.h @@ -75,6 +75,7 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice { // config state OpMode op_mode_; + bool power_enable_; uint16_t frequency_; uint8_t power_; uint8_t antcap_; @@ -160,10 +161,11 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice { SUB_SWITCH_EX(mute) SUB_SWITCH_EX(mono) SUB_SELECT_EX(pre_emphasis, PreEmphasis) + SUB_SWITCH_EX(power_enable) SUB_NUMBER_EX(frequency, float) SUB_NUMBER_EX(audio_deviation, float) SUB_NUMBER_EX(power, int) - SUB_NUMBER_EX(antcap, int) + SUB_NUMBER_EX(antcap, float) SUB_NUMBER_EX(analog_level, int) SUB_SELECT_EX(analog_attenuation, LineAttenuation) SUB_NUMBER_EX(digital_sample_rate, int) diff --git a/esphome/components/si4713_i2c/si4713defs.h b/esphome/components/si4713_i2c/si4713defs.h index 87c7316671..b640fd9bc7 100644 --- a/esphome/components/si4713_i2c/si4713defs.h +++ b/esphome/components/si4713_i2c/si4713defs.h @@ -11,8 +11,8 @@ static const int FREQ_RAW_MIN = 7600; static const int FREQ_RAW_MAX = 10800; static const int POWER_MIN = 88; static const int POWER_MAX = 120; -static const int ANTCAP_MIN = 0; -static const int ANTCAP_MAX = 191; +static const float ANTCAP_MIN = 0; +static const float ANTCAP_MAX = 47.75; static const int LILEVEL_MIN = 0; static const int LILEVEL_MAX = 1023; // no max in the datasheet, only that it is 10 bits static const int DISR_MIN = 32000; diff --git a/esphome/components/si4713_i2c/switch/__init__.py b/esphome/components/si4713_i2c/switch/__init__.py index d1a36e4937..7f4ab7e75b 100644 --- a/esphome/components/si4713_i2c/switch/__init__.py +++ b/esphome/components/si4713_i2c/switch/__init__.py @@ -10,6 +10,7 @@ from .. import ( CONF_SI4713_ID, Si4713Component, si4713_ns, + CONF_TUNER, CONF_PILOT, CONF_COMPRESSOR, CONF_LIMITER, @@ -24,6 +25,7 @@ from .. import ( CONF_GPIO1, CONF_GPIO2, CONF_GPIO3, + ICON_RADIO_TOWER, ICON_SINE_WAVE, ICON_VOLUME_MUTE, ICON_EAR_HEARING, @@ -32,6 +34,7 @@ from .. import ( MuteSwitch = si4713_ns.class_("MuteSwitch", switch.Switch) MonoSwitch = si4713_ns.class_("MonoSwitch", switch.Switch) +PowerEnableSwitch = si4713_ns.class_("PowerEnableSwitch", switch.Switch) PilotEnableSwitch = si4713_ns.class_("PilotEnableSwitch", switch.Switch) AcompEnableSwitch = si4713_ns.class_("AcompEnableSwitch", switch.Switch) LimiterEnableSwitch = si4713_ns.class_("LimiterEnableSwitch", switch.Switch) @@ -42,6 +45,17 @@ RdsEnable = si4713_ns.class_("RdsEnable", switch.Switch) RDSEnableSwitch = si4713_ns.class_("RDSEnableSwitch", switch.Switch) GPIOSwitch = si4713_ns.class_("GPIOSwitch", switch.Switch) +TUNER_SCHEMA = cv.Schema( + { + cv.Optional(CONF_ENABLE): switch.switch_schema( + PowerEnableSwitch, + device_class=DEVICE_CLASS_SWITCH, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_RADIO_TOWER, + ), + } +) + PILOT_SCHEMA = cv.Schema( { cv.Optional(CONF_ENABLE): switch.switch_schema( @@ -147,6 +161,7 @@ CONFIG_SCHEMA = cv.Schema( entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_EAR_HEARING, ), + cv.Optional(CONF_TUNER): TUNER_SCHEMA, cv.Optional(CONF_PILOT): PILOT_SCHEMA, cv.Optional(CONF_COMPRESSOR): COMPRESSOR_SCHEMA, cv.Optional(CONF_LIMITER): LIMITER_SCHEMA, @@ -169,6 +184,8 @@ async def to_code(config): p = await cg.get_variable(config[CONF_SI4713_ID]) await new_switch(p, config, CONF_MUTE, p.set_mute_switch) await new_switch(p, config, CONF_MONO, p.set_mono_switch) + if tuner_config := config.get(CONF_TUNER): + await new_switch(p, tuner_config, CONF_ENABLE, p.set_power_enable_switch) if pilot_config := config.get(CONF_PILOT): await new_switch(p, pilot_config, CONF_ENABLE, p.set_pilot_enable_switch) if compressor_config := config.get(CONF_COMPRESSOR): diff --git a/esphome/components/si4713_i2c/switch/power_enable_switch.cpp b/esphome/components/si4713_i2c/switch/power_enable_switch.cpp new file mode 100644 index 0000000000..fdb37ae090 --- /dev/null +++ b/esphome/components/si4713_i2c/switch/power_enable_switch.cpp @@ -0,0 +1,12 @@ +#include "power_enable_switch.h" + +namespace esphome { +namespace si4713 { + +void PowerEnableSwitch::write_state(bool value) { + this->publish_state(value); + this->parent_->set_power_enable(value); +} + +} // namespace si4713 +} // namespace esphome \ No newline at end of file diff --git a/esphome/components/si4713_i2c/switch/power_enable_switch.h b/esphome/components/si4713_i2c/switch/power_enable_switch.h new file mode 100644 index 0000000000..c4145a303f --- /dev/null +++ b/esphome/components/si4713_i2c/switch/power_enable_switch.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/switch/switch.h" +#include "../si4713.h" + +namespace esphome { +namespace si4713 { + +class PowerEnableSwitch : public switch_::Switch, public Parented { + public: + PowerEnableSwitch() = default; + + protected: + void write_state(bool value) override; +}; + +} // namespace si4713 +} // namespace esphome diff --git a/tests/components/si4713_i2c/test.esp32-s3-ard.yaml b/tests/components/si4713_i2c/test.esp32-s3-ard.yaml index 4dce5ceba2..904acc9028 100644 --- a/tests/components/si4713_i2c/test.esp32-s3-ard.yaml +++ b/tests/components/si4713_i2c/test.esp32-s3-ard.yaml @@ -11,6 +11,7 @@ si4713: mono: False pre_emphasis: '75us' tuner: + enable: True frequency: 87.50 deviation: 68.25 power: 115 @@ -130,6 +131,9 @@ switch: name: Mute mono: name: Mono + tuner: + enable: + name: Power Enable pilot: enable: name: Pilot Tone Enable diff --git a/tests/components/si4713_i2c/test.esp32-s3-idf.yaml b/tests/components/si4713_i2c/test.esp32-s3-idf.yaml index 4dce5ceba2..904acc9028 100644 --- a/tests/components/si4713_i2c/test.esp32-s3-idf.yaml +++ b/tests/components/si4713_i2c/test.esp32-s3-idf.yaml @@ -11,6 +11,7 @@ si4713: mono: False pre_emphasis: '75us' tuner: + enable: True frequency: 87.50 deviation: 68.25 power: 115 @@ -130,6 +131,9 @@ switch: name: Mute mono: name: Mono + tuner: + enable: + name: Power Enable pilot: enable: name: Pilot Tone Enable