config restructuring

This commit is contained in:
Gábor Poczkodi 2024-10-18 23:15:46 +02:00
parent 1fea9f6063
commit fb0d580688
10 changed files with 87 additions and 16 deletions

View file

@ -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)

View file

@ -237,7 +237,7 @@ async def to_code(config):
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_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)
if digital_config := config.get(CONF_DIGITAL):

View file

@ -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)

View file

@ -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)

View file

@ -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;

View file

@ -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):

View file

@ -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

View file

@ -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<Si4713Component> {
public:
PowerEnableSwitch() = default;
protected:
void write_state(bool value) override;
};
} // namespace si4713
} // namespace esphome

View file

@ -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

View file

@ -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