code cleanup

This commit is contained in:
Gábor Poczkodi 2024-10-18 15:57:30 +02:00
parent 2c148246fb
commit 50ca53f878
10 changed files with 850 additions and 1018 deletions

View file

@ -4,7 +4,12 @@ from esphome import automation
from esphome.components import i2c, binary_sensor
from esphome.const import (
CONF_ID,
CONF_SENSOR,
CONF_FREQUENCY,
CONF_GAIN,
CONF_DURATION,
CONF_HIGH,
CONF_LOW,
DEVICE_CLASS_POWER,
DEVICE_CLASS_EMPTY,
)
@ -43,41 +48,52 @@ KT0803Component = kt0803_ns.class_(
)
CONF_KT0803_ID = "kt0803_id"
CONF_REF_CLK = "ref_clk"
CONF_XTAL = "xtal"
CONF_ALC = "alc"
CONF_SILENCE = "silence"
CONF_CHIP_ID = "chip_id"
CONF_PW_OK = "pw_ok"
CONF_SLNCID = "slncid"
CONF_PGA = "pga"
CONF_RFGAIN = "rfgain"
# general config
# CONF_FREQUENCY = "frequeny"
CONF_DEVIATION = "deviation"
CONF_MUTE = "mute"
CONF_MONO = "mono"
CONF_PRE_EMPHASIS = "pre_emphasis"
CONF_PGA = "pga"
CONF_RFGAIN = "rfgain"
CONF_PILOT_TONE_AMPLITUDE = "pilot_tone_amplitude"
CONF_BASS_BOOST_CONTROL = "bass_boost_control"
CONF_ALC_ENABLE = "alc_enable"
CONF_AUTO_PA_DOWN = "auto_pa_down"
CONF_PA_DOWN = "pa_down"
CONF_STANDBY_ENABLE = "standby_enable"
CONF_ALC_ATTACK_TIME = "alc_attack_time"
CONF_ALC_DECAY_TIME = "alc_decay_time"
CONF_PA_BIAS = "pa_bias"
CONF_AUDIO_LIMITER_LEVEL = "audio_limiter_level"
CONF_SWITCH_MODE = "switch_mode"
CONF_SILENCE_HIGH = "silence_high"
CONF_SILENCE_LOW = "silence_low"
CONF_SILENCE_DETECTION = "silence_detection"
CONF_SILENCE_DURATION = "silence_duration"
CONF_SILENCE_HIGH_COUNTER = "silence_high_counter"
CONF_SILENCE_LOW_COUNTER = "silence_low_counter"
CONF_ALC_GAIN = "alc_gain"
CONF_XTAL_SEL = "xtal_sel"
CONF_AU_ENHANCE = "au_enhance"
CONF_FREQUENCY_DEVIATION = "frequency_deviation"
# ref_clk
CONF_ENABLE = "enable"
CONF_REF_CLK = "ref_clk"
CONF_XTAL_ENABLE = "xtal_enable"
CONF_REF_CLK_ENABLE = "ref_clk_enable"
CONF_ALC_HIGH = "alc_high"
CONF_ALC_HOLD_TIME = "alc_hold_time"
CONF_ALC_LOW = "alc_low"
# xtal
# CONF_ENABLE = "enable"
CONF_SEL = "sel"
# alc
# CONF_ENABLE = "enable"
# CONF_GAIN = "gain"
CONF_ATTACK_TIME = "attack_time"
CONF_DECAY_TIME = "decay_time"
CONF_HOLD_TIME = "hold_time"
# CONF_HIGH = "high"
# CONF_LOW = "low"
# silence
CONF_DETECTION = "detection"
# CONF_DURATION = "duration"
# CONF_HIGH = "high"
# CONF_LOW = "low"
CONF_HIGH_COUNTER = "high_counter"
CONF_LOW_COUNTER = "low_counter"
# sensor
CONF_PW_OK = "pw_ok"
CONF_SLNCID = "slncid"
SetFrequencyAction = kt0803_ns.class_(
"SetFrequencyAction", automation.Action, cg.Parented.template(KT0803Component)
@ -294,13 +310,68 @@ AUDIO_LIMITER_LEVEL = {
"0.9625": AudioLimiterLevel.LMTLVL_09625,
}
REF_CLK_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_REF_CLK, default="32.768kHz"): cv.enum(REFERENCE_CLOCK),
}
)
XTAL_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENABLE, default=True): cv.boolean,
cv.Optional(CONF_SEL, default="32.768kHz"): cv.enum(XTAL_SEL),
}
)
ALC_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_GAIN, default=-3): cv.float_range(-15, 6),
cv.Optional(CONF_ATTACK_TIME, default="25us"): cv.enum(ALC_TIME),
cv.Optional(CONF_DECAY_TIME, default="25us"): cv.enum(ALC_TIME),
cv.Optional(CONF_HOLD_TIME, default="5s"): cv.enum(ALC_HOLD_TIME),
cv.Optional(CONF_HIGH, default="0.6"): cv.enum(ALC_HIGH),
cv.Optional(CONF_LOW, default="0.25"): cv.enum(ALC_LOW),
}
)
SILENCE_SCHEMA = cv.Schema(
{
cv.Optional(CONF_DETECTION, default=False): cv.boolean,
cv.Optional(CONF_DURATION, default="100ms"): cv.enum(
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME
),
cv.Optional(CONF_HIGH, default="32mV"): cv.enum(SILENCE_HIGH),
cv.Optional(CONF_LOW, default="8mV"): cv.enum(SILENCE_LOW),
cv.Optional(CONF_HIGH_COUNTER, default="15"): cv.enum(
SILENCE_HIGH_LEVEL_COUNTER
),
cv.Optional(CONF_LOW_COUNTER, default="1"): cv.enum(SILENCE_LOW_LEVEL_COUNTER),
}
)
SENSOR_SCHEMA = cv.Schema(
{
cv.Optional(CONF_PW_OK): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_POWER,
icon=ICON_RADIO_TOWER,
),
cv.Optional(CONF_SLNCID): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_EMPTY,
icon=ICON_VOLUME_MUTE,
),
}
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(KT0803Component),
cv.Required(CONF_CHIP_ID): cv.enum(CHIP_ID),
cv.Optional(CONF_FREQUENCY, default=87.50): cv.float_range(70, 108),
cv.Optional(CONF_PGA, default=-15): cv.float_range(-15, 12),
cv.Optional(CONF_DEVIATION, default="75kHz"): cv.enum(FREQUENCY_DEVIATION),
cv.Optional(CONF_PGA, default=0): cv.float_range(-15, 12),
cv.Optional(CONF_RFGAIN, default=108): cv.float_range(95.5, 108),
cv.Optional(CONF_MUTE, default=False): cv.boolean,
cv.Optional(CONF_MONO, default=False): cv.boolean,
@ -311,49 +382,20 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_BASS_BOOST_CONTROL, default="Disabled"): cv.enum(
BASS_BOOST_CONTROL
),
cv.Optional(CONF_ALC_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_AUTO_PA_DOWN, default=True): cv.boolean,
cv.Optional(CONF_PA_DOWN, default=False): cv.boolean,
cv.Optional(CONF_STANDBY_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_ALC_ATTACK_TIME, default="25us"): cv.enum(ALC_TIME),
cv.Optional(CONF_ALC_DECAY_TIME, default="25us"): cv.enum(ALC_TIME),
cv.Optional(CONF_PA_BIAS, default=True): cv.boolean,
cv.Optional(CONF_AUDIO_LIMITER_LEVEL, default="0.875"): cv.enum(
AUDIO_LIMITER_LEVEL
),
cv.Optional(CONF_SWITCH_MODE, default="Mute"): cv.enum(SWITCH_MODE),
cv.Optional(CONF_SILENCE_HIGH, default="32mV"): cv.enum(SILENCE_HIGH),
cv.Optional(CONF_SILENCE_LOW, default="8mV"): cv.enum(SILENCE_LOW),
cv.Optional(CONF_SILENCE_DETECTION, default=False): cv.boolean,
cv.Optional(CONF_SILENCE_DURATION, default="100ms"): cv.enum(
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME
),
cv.Optional(CONF_SILENCE_HIGH_COUNTER, default="15"): cv.enum(
SILENCE_HIGH_LEVEL_COUNTER
),
cv.Optional(CONF_SILENCE_LOW_COUNTER, default="1"): cv.enum(
SILENCE_LOW_LEVEL_COUNTER
),
cv.Optional(CONF_ALC_GAIN, default=-3): cv.float_range(-15, 6),
cv.Optional(CONF_XTAL_SEL, default="32.768kHz"): cv.enum(XTAL_SEL),
cv.Optional(CONF_AU_ENHANCE, default=False): cv.boolean,
cv.Optional(CONF_FREQUENCY_DEVIATION, default="75kHz"): cv.enum(
FREQUENCY_DEVIATION
),
cv.Optional(CONF_REF_CLK, default="32.768kHz"): cv.enum(REFERENCE_CLOCK),
cv.Optional(CONF_XTAL_ENABLE, default=True): cv.boolean,
cv.Optional(CONF_REF_CLK_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_ALC_HIGH, default="0.6"): cv.enum(ALC_HIGH),
cv.Optional(CONF_ALC_HOLD_TIME, default="5s"): cv.enum(ALC_HOLD_TIME),
cv.Optional(CONF_ALC_LOW, default="0.25"): cv.enum(ALC_LOW),
cv.Optional(CONF_PW_OK): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_POWER,
icon=ICON_RADIO_TOWER,
),
cv.Optional(CONF_SLNCID): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_EMPTY,
icon=ICON_VOLUME_MUTE,
),
cv.Optional(CONF_REF_CLK): REF_CLK_SCHEMA,
cv.Optional(CONF_XTAL): XTAL_SCHEMA,
cv.Optional(CONF_ALC): ALC_SCHEMA,
cv.Optional(CONF_SILENCE): SILENCE_SCHEMA,
cv.Optional(CONF_SENSOR): SENSOR_SCHEMA,
}
)
.extend(cv.polling_component_schema("60s"))
@ -398,37 +440,44 @@ async def to_code(config):
await i2c.register_i2c_device(var, config)
await set_var(config, CONF_CHIP_ID, var.set_chip_id)
await set_var(config, CONF_FREQUENCY, var.set_frequency)
await set_var(config, CONF_PGA, var.set_pga)
await set_var(config, CONF_RFGAIN, var.set_rfgain)
await set_var(config, CONF_DEVIATION, var.set_deviation)
await set_var(config, CONF_MUTE, var.set_mute)
await set_var(config, CONF_MONO, var.set_mono)
await set_var(config, CONF_PRE_EMPHASIS, var.set_pre_emphasis)
await set_var(config, CONF_PGA, var.set_pga)
await set_var(config, CONF_RFGAIN, var.set_rfgain)
await set_var(config, CONF_PILOT_TONE_AMPLITUDE, var.set_pilot_tone_amplitude)
await set_var(config, CONF_BASS_BOOST_CONTROL, var.set_bass_boost_control)
await set_var(config, CONF_ALC_ENABLE, var.set_alc_enable)
await set_var(config, CONF_AUTO_PA_DOWN, var.set_auto_pa_down)
await set_var(config, CONF_PA_DOWN, var.set_pa_down)
await set_var(config, CONF_STANDBY_ENABLE, var.set_standby_enable)
await set_var(config, CONF_ALC_ATTACK_TIME, var.set_alc_attack_time)
await set_var(config, CONF_ALC_DECAY_TIME, var.set_alc_decay_time)
await set_var(config, CONF_PA_BIAS, var.set_pa_bias)
await set_var(config, CONF_AUDIO_LIMITER_LEVEL, var.set_audio_limiter_level)
await set_var(config, CONF_SWITCH_MODE, var.set_switch_mode)
await set_var(config, CONF_SILENCE_HIGH, var.set_silence_high)
await set_var(config, CONF_SILENCE_LOW, var.set_silence_low)
await set_var(config, CONF_SILENCE_DETECTION, var.set_silence_detection)
await set_var(config, CONF_SILENCE_DURATION, var.set_silence_duration)
await set_var(config, CONF_SILENCE_HIGH_COUNTER, var.set_silence_high_counter)
await set_var(config, CONF_SILENCE_LOW_COUNTER, var.set_silence_low_counter)
await set_var(config, CONF_ALC_GAIN, var.set_alc_gain)
await set_var(config, CONF_XTAL_SEL, var.set_xtal_sel)
await set_var(config, CONF_AU_ENHANCE, var.set_au_enhance)
await set_var(config, CONF_FREQUENCY_DEVIATION, var.set_frequency_deviation)
await set_var(config, CONF_REF_CLK, var.set_ref_clk)
await set_var(config, CONF_XTAL_ENABLE, var.set_xtal_enable)
await set_var(config, CONF_REF_CLK_ENABLE, var.set_ref_clk_enable)
await set_var(config, CONF_ALC_HIGH, var.set_alc_high)
await set_var(config, CONF_ALC_HOLD_TIME, var.set_alc_hold_time)
await set_var(config, CONF_ALC_LOW, var.set_alc_low)
await set_binary_sensor(config, CONF_PW_OK, var.set_pw_ok_binary_sensor)
await set_binary_sensor(config, CONF_SLNCID, var.set_slncid_binary_sensor)
if ref_clk_config := config.get(CONF_REF_CLK):
await set_var(ref_clk_config, CONF_ENABLE, var.set_ref_clk_enable)
await set_var(ref_clk_config, CONF_REF_CLK, var.set_ref_clk)
if xtal_config := config.get(CONF_XTAL):
await set_var(xtal_config, CONF_ENABLE, var.set_xtal_enable)
await set_var(xtal_config, CONF_SEL, var.set_xtal_sel)
if alc_config := config.get(CONF_ALC):
await set_var(alc_config, CONF_ENABLE, var.set_alc_enable)
await set_var(alc_config, CONF_GAIN, var.set_alc_gain)
await set_var(alc_config, CONF_ATTACK_TIME, var.set_alc_attack_time)
await set_var(alc_config, CONF_DECAY_TIME, var.set_alc_decay_time)
await set_var(alc_config, CONF_HOLD_TIME, var.set_alc_hold_time)
await set_var(alc_config, CONF_HIGH, var.set_alc_high)
await set_var(alc_config, CONF_LOW, var.set_alc_low)
if silence_config := config.get(CONF_SILENCE):
await set_var(silence_config, CONF_DETECTION, var.set_silence_detection)
await set_var(silence_config, CONF_DURATION, var.set_silence_duration)
await set_var(silence_config, CONF_HIGH, var.set_silence_high)
await set_var(silence_config, CONF_LOW, var.set_silence_low)
await set_var(silence_config, CONF_HIGH_COUNTER, var.set_silence_high_counter)
await set_var(silence_config, CONF_LOW_COUNTER, var.set_silence_low_counter)
if sensor_config := config.get(CONF_SENSOR):
await set_binary_sensor(sensor_config, CONF_PW_OK, var.set_pw_ok_binary_sensor)
await set_binary_sensor(
sensor_config, CONF_SLNCID, var.set_slncid_binary_sensor
)

View file

@ -136,38 +136,38 @@ void KT0803Component::setup() {
this->publish_pw_ok();
this->publish_slncid();
this->publish_frequency();
this->publish_pga();
this->publish_rfgain();
this->publish_deviation();
this->publish_mute();
this->publish_mono();
this->publish_pre_emphasis();
this->publish_pga();
this->publish_rfgain();
this->publish_pilot_tone_amplitude();
this->publish_bass_boost_control();
this->publish_alc_enable();
this->publish_auto_pa_down();
this->publish_pa_down();
this->publish_standby_enable();
this->publish_alc_attack_time();
this->publish_alc_decay_time();
this->publish_pa_bias();
this->publish_audio_limiter_level();
this->publish_switch_mode();
this->publish_au_enhance();
this->publish_ref_clk_enable();
this->publish_ref_clk();
this->publish_xtal_enable();
this->publish_xtal_sel();
this->publish_alc_enable();
this->publish_alc_gain();
this->publish_alc_attack_time();
this->publish_alc_decay_time();
this->publish_alc_hold_time();
this->publish_alc_high();
this->publish_alc_low();
this->publish_silence_high();
this->publish_silence_low();
this->publish_silence_detection();
this->publish_silence_duration();
this->publish_silence_high_counter();
this->publish_silence_low_counter();
this->publish_alc_gain();
this->publish_xtal_sel();
this->publish_au_enhance();
this->publish_frequency_deviation();
this->publish_ref_clk();
this->publish_xtal_enable();
this->publish_ref_clk_enable();
this->publish_alc_high();
this->publish_alc_hold_time();
this->publish_alc_low();
}
void KT0803Component::dump_config() {
@ -201,6 +201,32 @@ void KT0803Component::loop() {}
// config
template<typename T> T GET_ENUM_LAST(T value) { return T::LAST; }
#define CHECK_ENUM(value) \
if ((value) >= GET_ENUM_LAST(value)) { \
ESP_LOGE(TAG, "%s(%d) invalid", __func__, (int) (value)); \
return; \
}
#define CHECK_FLOAT_RANGE(value, min_value, max_value) \
if (!((min_value) <= (value) && (value) <= (max_value))) { \
ESP_LOGE(TAG, "%s(%.2f) invalid (%.2f - %.2f)", __func__, value, min_value, max_value); \
return; \
}
#define CHECK_INT_RANGE(value, min_value, max_value) \
if (!((min_value) <= (value) && (value) <= (max_value))) { \
ESP_LOGE(TAG, "%s(%d) invalid (%d - %d)", __func__, value, min_value, max_value); \
return; \
}
#define CHECK_TEXT_RANGE(value, max_size) \
if ((value).size() > (max_size)) { \
ESP_LOGW(TAG, "%s(%s) trimmed (max %d characters)", __func__, (value).c_str(), max_size); \
(value).resize(max_size); \
}
void KT0803Component::set_chip_id(ChipId value) { this->chip_id_ = value; }
ChipId KT0803Component::get_chip_id() { return this->chip_id_; }
@ -221,11 +247,7 @@ std::string KT0803Component::get_chip_string() const {
}
void KT0803Component::set_frequency(float value) {
if (!(CHSEL_MIN <= value && value <= CHSEL_MAX)) {
ESP_LOGE(TAG, "set_frequency(%.2f) invalid (%.2f - %.2f)", value, CHSEL_MIN, CHSEL_MAX);
return;
}
CHECK_FLOAT_RANGE(value, CHSEL_MIN, CHSEL_MAX)
uint16_t ch = (uint16_t) std::lround(value * 20);
this->state_.CHSEL2 = (uint8_t) ((ch >> 9) & 0x07);
this->state_.CHSEL1 = (uint8_t) ((ch >> 1) & 0xff);
@ -235,7 +257,6 @@ void KT0803Component::set_frequency(float value) {
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x02);
}
this->publish_frequency();
}
@ -247,11 +268,56 @@ float KT0803Component::get_frequency() {
return (float) ch / 20;
}
void KT0803Component::set_pga(float value) {
if (!(PGA_MIN <= value && value <= PGA_MAX)) {
ESP_LOGE(TAG, "set_pga(%.2f) invalid (%.2f - %.2f)", value, PGA_MIN, PGA_MAX);
return;
void KT0803Component::set_deviation(FrequencyDeviation value) {
CHECK_ENUM(value)
if (this->chip_id_ == ChipId::KT0803K || this->chip_id_ == ChipId::KT0803M) {
this->state_.FDEV_K = (uint8_t) value;
this->write_reg_(0x04);
} else if (this->chip_id_ == ChipId::KT0803L) {
this->state_.FDEV_L = (uint8_t) value;
this->write_reg_(0x17);
}
this->publish_deviation();
}
FrequencyDeviation KT0803Component::get_deviation() {
if (this->chip_id_ == ChipId::KT0803K || this->chip_id_ == ChipId::KT0803M) {
return (FrequencyDeviation) this->state_.FDEV_K;
} else if (this->chip_id_ == ChipId::KT0803L) {
return (FrequencyDeviation) this->state_.FDEV_L;
}
return FrequencyDeviation::FDEV_75KHZ;
}
void KT0803Component::set_mute(bool value) {
this->state_.MUTE = value ? 1 : 0;
this->write_reg_(0x02);
this->publish_mute();
}
bool KT0803Component::get_mute() { return this->state_.MUTE == 1; }
void KT0803Component::set_mono(bool value) {
this->state_.MONO = value ? 1 : 0;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x04);
}
this->publish_mono();
}
bool KT0803Component::get_mono() { return this->state_.MONO == 1; }
void KT0803Component::set_pre_emphasis(PreEmphasis value) {
CHECK_ENUM(value)
this->state_.PHTCNST = (uint8_t) value;
this->write_reg_(0x02);
this->publish_pre_emphasis();
}
PreEmphasis KT0803Component::get_pre_emphasis() { return (PreEmphasis) this->state_.PHTCNST; }
void KT0803Component::set_pga(float value) {
CHECK_FLOAT_RANGE(value, PGA_MIN, PGA_MAX)
/*
12 01100 => 11111 31
@ -300,7 +366,6 @@ void KT0803Component::set_pga(float value) {
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x04);
}
this->publish_pga();
}
@ -320,13 +385,8 @@ static const uint16_t RF_GAIN_MAP[] = {
};
void KT0803Component::set_rfgain(float value) {
if (!(RFGAIN_MIN <= value && value <= RFGAIN_MAX)) {
ESP_LOGE(TAG, "set_rfgain(%.2f) invalid (%.2f - %.2f)", value, RFGAIN_MIN, RFGAIN_MAX);
return;
}
CHECK_FLOAT_RANGE(value, RFGAIN_MIN, RFGAIN_MAX)
uint16_t v = (uint16_t) std::lround(value * 10);
for (size_t i = 0; i < countof(RF_GAIN_MAP); i++) {
if (v <= RF_GAIN_MAP[i]) {
this->state_.RFGAIN0 = (i >> 0) & 3;
@ -335,14 +395,10 @@ void KT0803Component::set_rfgain(float value) {
break;
}
}
// TODO: there is an annoying hiss during this, mute/unmute does nothing
// maybe write all three registers in one go
// TODO: annoying hiss during this, mute/unmute does nothing, maybe write all three registers in one go
this->write_reg_(0x00);
this->write_reg_(0x01);
this->write_reg_(0x13);
this->publish_rfgain();
}
@ -351,87 +407,31 @@ float KT0803Component::get_rfgain() {
return (float) RF_GAIN_MAP[i] / 10;
}
void KT0803Component::set_mute(bool value) {
this->state_.MUTE = value ? 1 : 0;
this->write_reg_(0x02);
this->publish_mute();
}
bool KT0803Component::get_mute() { return this->state_.MUTE == 1; }
void KT0803Component::set_mono(bool value) {
this->state_.MONO = value ? 1 : 0;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x04);
}
this->publish_mono();
}
bool KT0803Component::get_mono() { return this->state_.MONO == 1; }
void KT0803Component::set_pre_emphasis(PreEmphasis value) {
if (value >= PreEmphasis::LAST) {
ESP_LOGE(TAG, "set_pre_emphasis(%d) invalid (50 or 75)", (int) value);
return;
}
this->state_.PHTCNST = (uint8_t) value;
this->write_reg_(0x02);
this->publish_pre_emphasis();
}
PreEmphasis KT0803Component::get_pre_emphasis() { return (PreEmphasis) this->state_.PHTCNST; }
void KT0803Component::set_pilot_tone_amplitude(PilotToneAmplitude value) {
if (value >= PilotToneAmplitude::LAST) {
ESP_LOGE(TAG, "PilotToneAmplitude(%d) invalid (LOW or HIGH)", (int) value);
return;
}
CHECK_ENUM(value)
this->state_.PLTADJ = (uint8_t) value;
this->write_reg_(0x02);
this->publish_pilot_tone_amplitude();
}
PilotToneAmplitude KT0803Component::get_pilot_tone_amplitude() { return (PilotToneAmplitude) this->state_.PLTADJ; }
void KT0803Component::set_bass_boost_control(BassBoostControl value) {
if (value >= BassBoostControl::LAST) {
ESP_LOGE(TAG, "set_bass(%d) invalid", (int) value);
return;
}
CHECK_ENUM(value)
this->state_.BASS = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x04);
}
this->publish_bass_boost_control();
}
BassBoostControl KT0803Component::get_bass_boost_control() { return (BassBoostControl) this->state_.BASS; }
void KT0803Component::set_alc_enable(bool value) {
this->state_.ALC_EN = value ? 1 : 0;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x04);
}
this->publish_alc_enable();
}
bool KT0803Component::get_alc_enable() { return this->state_.ALC_EN == 1; }
void KT0803Component::set_auto_pa_down(bool value) {
this->state_.AUTO_PADN = value ? 1 : 0;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x0B);
}
this->publish_auto_pa_down();
}
@ -442,7 +442,6 @@ void KT0803Component::set_pa_down(bool value) {
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x0B);
}
this->publish_pa_down();
}
@ -453,195 +452,110 @@ void KT0803Component::set_standby_enable(bool value) {
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x0B);
}
this->publish_standby_enable();
}
bool KT0803Component::get_standby_enable() { return this->state_.Standby == 1; }
void KT0803Component::set_alc_attack_time(AlcTime value) {
if (value >= AlcTime::LAST) {
ESP_LOGE(TAG, "set_alc_attack_time(%d) invalid", (int) value);
return;
}
this->state_.ALC_ATTACK_TIME = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x0C);
}
this->publish_alc_attack_time();
}
AlcTime KT0803Component::get_alc_attack_time() { return (AlcTime) this->state_.ALC_ATTACK_TIME; }
void KT0803Component::set_alc_decay_time(AlcTime value) {
if (value >= AlcTime::LAST) {
ESP_LOGE(TAG, "set_alc_decay_time(%d) invalid", (int) value);
return;
}
this->state_.ALC_DECAY_TIME = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x0C);
}
this->publish_alc_decay_time();
}
AlcTime KT0803Component::get_alc_decay_time() { return (AlcTime) this->state_.ALC_DECAY_TIME; }
void KT0803Component::set_pa_bias(bool value) {
this->state_.PA_BIAS = value ? 1 : 0;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x0E);
}
this->publish_pa_bias();
}
bool KT0803Component::get_pa_bias() { return this->state_.PA_BIAS == 1; }
void KT0803Component::set_audio_limiter_level(AudioLimiterLevel value) {
if (value >= AudioLimiterLevel::LAST) {
ESP_LOGE(TAG, "set_audio_limiter_level(%d) invalid", (int) value);
return;
}
CHECK_ENUM(value)
this->state_.LMTLVL = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803K || this->chip_id_ == ChipId::KT0803M) {
this->write_reg_(0x10);
}
this->publish_audio_limiter_level();
}
AudioLimiterLevel KT0803Component::get_audio_limiter_level() { return (AudioLimiterLevel) this->state_.LMTLVL; }
void KT0803Component::set_switch_mode(SwitchMode value) {
if (value >= SwitchMode::LAST) {
ESP_LOGE(TAG, "set_switch_mode(%d) invalid", (int) value);
return;
}
CHECK_ENUM(value)
this->state_.SW_MOD = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
}
this->publish_switch_mode();
}
SwitchMode KT0803Component::get_switch_mode() { return (SwitchMode) this->state_.SW_MOD; }
void KT0803Component::set_silence_high(SilenceHigh value) {
if (value >= SilenceHigh::LAST) {
ESP_LOGE(TAG, "set_silence_high(%d) invalid", (int) value);
return;
void KT0803Component::set_au_enhance(bool value) {
this->state_.AU_ENHANCE = value ? 1 : 0;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x17);
}
this->state_.SLNCTHH = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
}
this->publish_silence_high();
this->publish_au_enhance();
}
SilenceHigh KT0803Component::get_silence_high() { return (SilenceHigh) this->state_.SLNCTHH; }
bool KT0803Component::get_au_enhance() { return this->state_.AU_ENHANCE == 1; }
void KT0803Component::set_silence_low(SilenceLow value) {
if (value >= SilenceLow::LAST) {
ESP_LOGE(TAG, "set_silence_low(%d) invalid", (int) value);
return;
void KT0803Component::set_ref_clk_enable(bool value) {
this->state_.DCLK = value ? 1 : 0;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x1E);
}
this->state_.SLNCTHL = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
}
this->publish_silence_low();
this->publish_ref_clk_enable();
}
SilenceLow KT0803Component::get_silence_low() { return (SilenceLow) this->state_.SLNCTHL; }
bool KT0803Component::get_ref_clk_enable() { return this->state_.DCLK == 1; }
void KT0803Component::set_silence_detection(bool value) {
this->state_.SLNCDIS = value ? 0 : 1;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
void KT0803Component::set_ref_clk(ReferenceClock value) {
CHECK_ENUM(value)
this->state_.REF_CLK = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x1E);
}
this->publish_silence_detection();
this->publish_ref_clk();
}
bool KT0803Component::get_silence_detection() { return this->state_.SLNCDIS == 0; }
ReferenceClock KT0803Component::get_ref_clk() { return (ReferenceClock) this->state_.REF_CLK; }
void KT0803Component::set_silence_duration(SilenceLowAndHighLevelDurationTime value) {
if (value >= SilenceLowAndHighLevelDurationTime::LAST) {
ESP_LOGE(TAG, "set_silence_duration(%d) invalid", (int) value);
return;
void KT0803Component::set_xtal_enable(bool value) {
this->state_.XTALD = value ? 0 : 1;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x1E);
}
this->state_.SLNCTIME0 = (uint8_t) value & 7;
this->state_.SLNCTIME1 = (uint8_t) value >> 3;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x14);
}
this->publish_silence_duration();
this->publish_xtal_enable();
}
SilenceLowAndHighLevelDurationTime KT0803Component::get_silence_duration() {
return (SilenceLowAndHighLevelDurationTime) ((this->state_.SLNCTIME1 << 3) | this->state_.SLNCTIME0);
}
bool KT0803Component::get_xtal_enable() { return this->state_.XTALD == 0; }
void KT0803Component::set_silence_high_counter(SilenceHighLevelCounter value) {
if (value >= SilenceHighLevelCounter::LAST) {
ESP_LOGE(TAG, "set_silence_high_counter(%d) invalid", (int) value);
return;
void KT0803Component::set_xtal_sel(XtalSel value) {
CHECK_ENUM(value)
this->state_.XTAL_SEL = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x17);
}
this->publish_xtal_sel();
}
this->state_.SLNCCNTHIGH = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x14);
XtalSel KT0803Component::get_xtal_sel() { return (XtalSel) this->state_.XTAL_SEL; }
void KT0803Component::set_alc_enable(bool value) {
this->state_.ALC_EN = value ? 1 : 0;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x04);
}
this->publish_silence_high_counter();
this->publish_alc_enable();
}
SilenceHighLevelCounter KT0803Component::get_silence_high_counter() {
return (SilenceHighLevelCounter) this->state_.SLNCCNTHIGH;
}
void KT0803Component::set_silence_low_counter(SilenceLowLevelCounter value) {
if (value >= SilenceLowLevelCounter::LAST) {
ESP_LOGE(TAG, "set_silence_low_counter(%d) invalid", (int) value);
return;
}
this->state_.SLNCCNTLOW = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x16);
}
this->publish_silence_low_counter();
}
SilenceLowLevelCounter KT0803Component::get_silence_low_counter() {
return (SilenceLowLevelCounter) this->state_.SLNCCNTLOW;
}
bool KT0803Component::get_alc_enable() { return this->state_.ALC_EN == 1; }
static const int8_t ALC_GAIN_MAP[] = {-6, -9, -12, -15, 6, 3, 0, -3};
void KT0803Component::set_alc_gain(float value) {
if (!(ALC_GAIN_MIN <= value && value <= ALC_GAIN_MAX)) {
ESP_LOGE(TAG, "set_alc_gain(%.2f) invalid (%.2f - %.2f)", value, ALC_GAIN_MIN, ALC_GAIN_MAX);
return;
}
CHECK_FLOAT_RANGE(value, ALC_GAIN_MIN, ALC_GAIN_MAX)
int v = (int) std::lround(value);
if (v <= -15) {
this->state_.ALCCMPGAIN = 3;
} else if (v <= -12) {
@ -659,264 +573,146 @@ void KT0803Component::set_alc_gain(float value) {
} else if (v <= 6) {
this->state_.ALCCMPGAIN = 4;
}
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x15);
}
this->publish_alc_gain();
}
float KT0803Component::get_alc_gain() { return (float) ALC_GAIN_MAP[this->state_.ALCCMPGAIN]; }
void KT0803Component::set_xtal_sel(XtalSel value) {
if (value >= XtalSel::LAST) {
ESP_LOGE(TAG, "set_xtal_sel(%d) invalid", (int) value);
return;
}
this->state_.XTAL_SEL = (uint8_t) value;
void KT0803Component::set_alc_attack_time(AlcTime value) {
CHECK_ENUM(value)
this->state_.ALC_ATTACK_TIME = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x17);
this->write_reg_(0x0C);
}
this->publish_xtal_sel();
this->publish_alc_attack_time();
}
XtalSel KT0803Component::get_xtal_sel() { return (XtalSel) this->state_.XTAL_SEL; }
AlcTime KT0803Component::get_alc_attack_time() { return (AlcTime) this->state_.ALC_ATTACK_TIME; }
void KT0803Component::set_au_enhance(bool value) {
this->state_.AU_ENHANCE = value ? 1 : 0;
void KT0803Component::set_alc_decay_time(AlcTime value) {
CHECK_ENUM(value)
this->state_.ALC_DECAY_TIME = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x17);
this->write_reg_(0x0C);
}
this->publish_au_enhance();
this->publish_alc_decay_time();
}
bool KT0803Component::get_au_enhance() { return this->state_.AU_ENHANCE == 1; }
void KT0803Component::set_frequency_deviation(FrequencyDeviation value) {
if (value >= FrequencyDeviation::LAST) {
ESP_LOGE(TAG, "set_frequency_deviation(%d) invalid", (int) value);
return;
}
if (this->chip_id_ == ChipId::KT0803K || this->chip_id_ == ChipId::KT0803M) {
this->state_.FDEV_K = (uint8_t) value;
this->write_reg_(0x04);
} else if (this->chip_id_ == ChipId::KT0803L) {
this->state_.FDEV_L = (uint8_t) value;
this->write_reg_(0x17);
}
this->publish_frequency_deviation();
}
FrequencyDeviation KT0803Component::get_frequency_deviation() {
if (this->chip_id_ == ChipId::KT0803K || this->chip_id_ == ChipId::KT0803M) {
return (FrequencyDeviation) this->state_.FDEV_K;
} else if (this->chip_id_ == ChipId::KT0803L) {
return (FrequencyDeviation) this->state_.FDEV_L;
}
return FrequencyDeviation::FDEV_75KHZ;
}
void KT0803Component::set_ref_clk(ReferenceClock value) {
if (value >= ReferenceClock::LAST) {
ESP_LOGE(TAG, "set_ref_clk(%d) invalid", (int) value);
return;
}
this->state_.REF_CLK = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x1E);
}
this->publish_ref_clk();
}
ReferenceClock KT0803Component::get_ref_clk() { return (ReferenceClock) this->state_.REF_CLK; }
void KT0803Component::set_xtal_enable(bool value) {
this->state_.XTALD = value ? 0 : 1;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x1E);
}
this->publish_xtal_enable();
}
bool KT0803Component::get_xtal_enable() { return this->state_.XTALD == 0; }
void KT0803Component::set_ref_clk_enable(bool value) {
this->state_.DCLK = value ? 1 : 0;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x1E);
}
this->publish_ref_clk_enable();
}
bool KT0803Component::get_ref_clk_enable() { return this->state_.DCLK == 1; }
void KT0803Component::set_alc_high(AlcHigh value) {
if (value >= AlcHigh::LAST) {
ESP_LOGE(TAG, "set_alc_high(%d) invalid", (int) value);
return;
}
this->state_.ALCHIGHTH = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x26);
}
this->publish_alc_high();
}
AlcHigh KT0803Component::get_alc_high() { return (AlcHigh) this->state_.ALCHIGHTH; }
AlcTime KT0803Component::get_alc_decay_time() { return (AlcTime) this->state_.ALC_DECAY_TIME; }
void KT0803Component::set_alc_hold_time(AlcHoldTime value) {
if (value >= AlcHoldTime::LAST) {
ESP_LOGE(TAG, "set_alc_hold(%d) invalid", (int) value);
return;
}
CHECK_ENUM(value)
this->state_.ALCHOLD = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x26);
}
this->publish_alc_hold_time();
}
AlcHoldTime KT0803Component::get_alc_hold_time() { return (AlcHoldTime) this->state_.ALCHOLD; }
void KT0803Component::set_alc_low(AlcLow value) {
if (value >= AlcLow::LAST) {
ESP_LOGE(TAG, "set_alc_low(%d) invalid", (int) value);
return;
void KT0803Component::set_alc_high(AlcHigh value) {
CHECK_ENUM(value)
this->state_.ALCHIGHTH = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x26);
}
this->publish_alc_high();
}
AlcHigh KT0803Component::get_alc_high() { return (AlcHigh) this->state_.ALCHIGHTH; }
void KT0803Component::set_alc_low(AlcLow value) {
CHECK_ENUM(value)
this->state_.ALCLOWTH = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x26);
}
this->publish_alc_low();
}
AlcLow KT0803Component::get_alc_low() { return (AlcLow) this->state_.ALCLOWTH; }
// publish
void KT0803Component::publish_pw_ok() { this->publish(this->pw_ok_binary_sensor_, this->state_.PW_OK == 1); }
void KT0803Component::publish_slncid() { this->publish(this->slncid_binary_sensor_, this->state_.SLNCID == 1); }
void KT0803Component::publish_frequency() { this->publish(this->frequency_number_, this->get_frequency()); }
void KT0803Component::publish_pga() { this->publish(this->pga_number_, this->get_pga()); }
void KT0803Component::publish_rfgain() { this->publish(this->rfgain_number_, this->get_rfgain()); }
void KT0803Component::publish_mute() { this->publish(this->mute_switch_, this->get_mute()); }
void KT0803Component::publish_mono() { this->publish(this->mono_switch_, this->get_mono()); }
void KT0803Component::publish_pre_emphasis() {
this->publish(this->pre_emphasis_select_, (size_t) this->get_pre_emphasis());
void KT0803Component::set_silence_detection(bool value) {
this->state_.SLNCDIS = value ? 0 : 1;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
}
this->publish_silence_detection();
}
void KT0803Component::publish_pilot_tone_amplitude() {
this->publish(this->pilot_tone_amplitude_select_, (size_t) this->get_pilot_tone_amplitude());
bool KT0803Component::get_silence_detection() { return this->state_.SLNCDIS == 0; }
void KT0803Component::set_silence_duration(SilenceLowAndHighLevelDurationTime value) {
CHECK_ENUM(value)
this->state_.SLNCTIME0 = (uint8_t) value & 7;
this->state_.SLNCTIME1 = (uint8_t) value >> 3;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x14);
}
this->publish_silence_duration();
}
void KT0803Component::publish_bass_boost_control() {
this->publish(this->bass_boost_control_select_, (size_t) this->get_bass_boost_control());
SilenceLowAndHighLevelDurationTime KT0803Component::get_silence_duration() {
return (SilenceLowAndHighLevelDurationTime) ((this->state_.SLNCTIME1 << 3) | this->state_.SLNCTIME0);
}
void KT0803Component::publish_alc_enable() { this->publish(this->alc_enable_switch_, this->get_alc_enable()); }
void KT0803Component::publish_auto_pa_down() { this->publish(this->auto_pa_down_switch_, this->get_auto_pa_down()); }
void KT0803Component::publish_pa_down() { this->publish(this->pa_down_switch_, this->get_pa_down()); }
void KT0803Component::publish_alc_attack_time() {
this->publish(this->alc_attack_time_select_, (size_t) this->get_alc_attack_time());
void KT0803Component::set_silence_high(SilenceHigh value) {
CHECK_ENUM(value)
this->state_.SLNCTHH = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
}
this->publish_silence_high();
}
void KT0803Component::publish_alc_decay_time() {
this->publish(this->alc_decay_time_select_, (size_t) this->get_alc_decay_time());
SilenceHigh KT0803Component::get_silence_high() { return (SilenceHigh) this->state_.SLNCTHH; }
void KT0803Component::set_silence_low(SilenceLow value) {
CHECK_ENUM(value)
this->state_.SLNCTHL = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x12);
}
this->publish_silence_low();
}
void KT0803Component::publish_standby_enable() {
this->publish(this->standby_enable_switch_, this->get_standby_enable());
SilenceLow KT0803Component::get_silence_low() { return (SilenceLow) this->state_.SLNCTHL; }
void KT0803Component::set_silence_high_counter(SilenceHighLevelCounter value) {
CHECK_ENUM(value)
this->state_.SLNCCNTHIGH = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x14);
}
this->publish_silence_high_counter();
}
void KT0803Component::publish_pa_bias() { this->publish(this->pa_bias_switch_, this->get_pa_bias()); }
void KT0803Component::publish_audio_limiter_level() {
this->publish(this->audio_limiter_level_select_, (size_t) this->get_audio_limiter_level());
SilenceHighLevelCounter KT0803Component::get_silence_high_counter() {
return (SilenceHighLevelCounter) this->state_.SLNCCNTHIGH;
}
void KT0803Component::publish_switch_mode() {
this->publish(this->switch_mode_select_, (size_t) this->get_switch_mode());
void KT0803Component::set_silence_low_counter(SilenceLowLevelCounter value) {
CHECK_ENUM(value)
this->state_.SLNCCNTLOW = (uint8_t) value;
if (this->chip_id_ != ChipId::KT0803) {
this->write_reg_(0x16);
}
this->publish_silence_low_counter();
}
void KT0803Component::publish_silence_high() {
this->publish(this->silence_high_select_, (size_t) this->get_silence_high());
SilenceLowLevelCounter KT0803Component::get_silence_low_counter() {
return (SilenceLowLevelCounter) this->state_.SLNCCNTLOW;
}
void KT0803Component::publish_silence_low() {
this->publish(this->silence_low_select_, (size_t) this->get_silence_low());
}
bool KT0803Component::get_pw_ok() { return this->state_.PW_OK != 0; }
void KT0803Component::publish_silence_detection() {
this->publish(this->silence_detection_switch_, this->get_silence_detection());
}
bool KT0803Component::get_slncid() { return this->state_.SLNCID != 0; }
void KT0803Component::publish_silence_duration() {
this->publish(this->silence_duration_select_, (size_t) this->get_silence_duration());
}
void KT0803Component::publish_silence_high_counter() {
this->publish(this->silence_high_counter_select_, (size_t) this->get_silence_high_counter());
}
void KT0803Component::publish_silence_low_counter() {
this->publish(this->silence_low_counter_select_, (size_t) this->get_silence_low_counter());
}
void KT0803Component::publish_alc_gain() { this->publish(this->alc_gain_number_, this->get_alc_gain()); }
void KT0803Component::publish_xtal_sel() { this->publish(this->xtal_sel_select_, (size_t) this->get_xtal_sel()); }
void KT0803Component::publish_au_enhance() { this->publish(this->au_enhance_switch_, this->get_au_enhance()); }
void KT0803Component::publish_frequency_deviation() {
this->publish(this->frequency_deviation_select_, (size_t) this->get_frequency_deviation());
}
void KT0803Component::publish_ref_clk() { this->publish(this->ref_clk_select_, (size_t) this->get_ref_clk()); }
void KT0803Component::publish_xtal_enable() {
this->publish(this->xtal_enable_switch_, (size_t) this->get_xtal_enable());
}
void KT0803Component::publish_ref_clk_enable() {
this->publish(this->ref_clk_enable_switch_, (size_t) this->get_ref_clk_enable());
}
void KT0803Component::publish_alc_high() { this->publish(this->alc_high_select_, (size_t) this->get_alc_high()); }
void KT0803Component::publish_alc_hold_time() {
this->publish(this->alc_hold_time_select_, (size_t) this->get_alc_hold_time());
}
void KT0803Component::publish_alc_low() { this->publish(this->alc_low_select_, (size_t) this->get_alc_low()); }
void KT0803Component::publish(text_sensor::TextSensor *s, const std::string &state) {
template<class S, class T> void KT0803Component::publish(S *s, T state) {
if (s != nullptr) {
if (!s->has_state() || s->state != state) {
s->publish_state(state);
@ -924,31 +720,7 @@ void KT0803Component::publish(text_sensor::TextSensor *s, const std::string &sta
}
}
void KT0803Component::publish(binary_sensor::BinarySensor *s, bool state) {
if (s != nullptr) {
if (!s->has_state() || s->state != state) {
s->publish_state(state);
}
}
}
void KT0803Component::publish(sensor::Sensor *s, float state) {
if (s != nullptr) {
if (!s->has_state() || s->state != state) {
s->publish_state(state);
}
}
}
void KT0803Component::publish(number::Number *n, float state) {
if (n != nullptr) {
if (!n->has_state() || n->state != state) {
n->publish_state(state);
}
}
}
void KT0803Component::publish(switch_::Switch *s, bool state) {
void KT0803Component::publish_switch(switch_::Switch *s, bool state) {
if (s != nullptr) {
if (s->state != state) { // ?
s->publish_state(state);
@ -956,7 +728,7 @@ void KT0803Component::publish(switch_::Switch *s, bool state) {
}
}
void KT0803Component::publish(select::Select *s, size_t index) {
void KT0803Component::publish_select(select::Select *s, size_t index) {
if (s != nullptr) {
if (auto state = s->at(index)) {
if (!s->has_state() || s->state != *state) {
@ -966,13 +738,5 @@ void KT0803Component::publish(select::Select *s, size_t index) {
}
}
void KT0803Component::publish(text::Text *t, const std::string &state) {
if (t != nullptr) {
if (!t->has_state() || t->state != state) {
t->publish_state(state);
}
}
}
} // namespace kt0803
} // namespace esphome

View file

@ -25,33 +25,44 @@ namespace kt0803 {
void set_##name##_text(text::Text *text) { this->name##_text_ = text; }
#endif
#define SUB_NUMBER_EX(name) \
#define SUB_NUMBER_EX(name, type) \
SUB_NUMBER(name) \
void publish_##name() { this->publish(this->name##_number_, (float) this->get_##name()); }
void publish_##name() { this->publish(this->name##_number_, (float) this->get_##name()); } \
void set_##name(type value); \
type get_##name();
#define SUB_SWITCH_EX(name) \
SUB_SWITCH(name) \
void publish_##name() { this->publish_switch(this->name##_switch_, this->get_##name()); }
void publish_##name() { this->publish_switch(this->name##_switch_, this->get_##name()); } \
void set_##name(bool value); \
bool get_##name();
#define SUB_SELECT_EX(name) \
#define SUB_SELECT_EX(name, type) \
SUB_SELECT(name) \
void publish_##name() { this->publish_select(this->name##_select_, (size_t) this->get_##name()); }
void publish_##name() { this->publish_select(this->name##_select_, (size_t) this->get_##name()); } \
void set_##name(type value); \
type get_##name();
#define SUB_TEXT_EX(name) \
SUB_TEXT(name) \
void publish_##name() { this->publish(this->name##_text_, this->get_##name()); }
void publish_##name() { this->publish(this->name##_text_, this->get_##name()); } \
void set_##name(const std::string &value); \
std::string get_##name();
#define SUB_SENSOR_EX(name) \
SUB_SENSOR(name) \
void publish_##name() { this->publish(this->name##_sensor_, (float) this->get_##name()); }
void publish_##name() { this->publish(this->name##_sensor_, (float) this->get_##name()); } \
float get_##name();
#define SUB_BINARY_SENSOR_EX(name) \
SUB_BINARY_SENSOR(name) \
void publish_##name() { this->publish(this->name##_binary_sensor_, this->get_##name()); }
void publish_##name() { this->publish(this->name##_binary_sensor_, this->get_##name()); } \
bool get_##name();
#define SUB_TEXT_SENSOR_EX(name) \
SUB_TEXT_SENSOR(name) \
void publish_##name() { this->publish(this->name##_text_sensor_, this->get_##name()); }
void publish_##name() { this->publish(this->name##_text_sensor_, this->get_##name()); } \
std::string get_##name();
class KT0803Component : public PollingComponent, public i2c::I2CDevice {
ChipId chip_id_; // no way to detect it
@ -65,165 +76,58 @@ class KT0803Component : public PollingComponent, public i2c::I2CDevice {
void write_reg_(uint8_t addr);
bool read_reg_(uint8_t addr);
SUB_BINARY_SENSOR(pw_ok)
SUB_BINARY_SENSOR(slncid)
SUB_NUMBER(frequency)
SUB_NUMBER(pga)
SUB_NUMBER(rfgain)
SUB_SWITCH(mute)
SUB_SWITCH(mono)
SUB_SELECT(pre_emphasis)
SUB_SELECT(pilot_tone_amplitude)
SUB_SELECT(bass_boost_control)
SUB_SWITCH(alc_enable)
SUB_SWITCH(auto_pa_down)
SUB_SWITCH(pa_down)
SUB_SWITCH(standby_enable)
SUB_SELECT(alc_attack_time)
SUB_SELECT(alc_decay_time)
SUB_SWITCH(pa_bias)
SUB_SELECT(audio_limiter_level)
SUB_SELECT(switch_mode)
SUB_SELECT(silence_high)
SUB_SELECT(silence_low)
SUB_SWITCH(silence_detection)
SUB_SELECT(silence_duration)
SUB_SELECT(silence_high_counter)
SUB_SELECT(silence_low_counter)
SUB_NUMBER(alc_gain)
SUB_SELECT(xtal_sel)
SUB_SWITCH(au_enhance)
SUB_SELECT(frequency_deviation)
SUB_SELECT(ref_clk)
SUB_SWITCH(xtal_enable)
SUB_SWITCH(ref_clk_enable)
SUB_SELECT(alc_high)
SUB_SELECT(alc_hold_time)
SUB_SELECT(alc_low)
void publish_pw_ok();
void publish_slncid();
void publish_frequency();
void publish_pga();
void publish_rfgain();
void publish_mute();
void publish_mono();
void publish_pre_emphasis();
void publish_pilot_tone_amplitude();
void publish_bass_boost_control();
void publish_alc_enable();
void publish_auto_pa_down();
void publish_pa_down();
void publish_standby_enable();
void publish_alc_attack_time();
void publish_alc_decay_time();
void publish_pa_bias();
void publish_audio_limiter_level();
void publish_switch_mode();
void publish_silence_high();
void publish_silence_low();
void publish_silence_detection();
void publish_silence_duration();
void publish_silence_high_counter();
void publish_silence_low_counter();
void publish_alc_gain();
void publish_xtal_sel();
void publish_au_enhance();
void publish_frequency_deviation();
void publish_ref_clk();
void publish_xtal_enable();
void publish_ref_clk_enable();
void publish_alc_high();
void publish_alc_hold_time();
void publish_alc_low();
void publish(sensor::Sensor *s, float state);
void publish(binary_sensor::BinarySensor *s, bool state);
void publish(text_sensor::TextSensor *s, const std::string &state);
void publish(number::Number *n, float state);
void publish(switch_::Switch *s, bool state);
void publish(select::Select *s, size_t index);
void publish(text::Text *t, const std::string &state);
template<class S, class T> void publish(S *s, T state);
// template specialization here is not supported by the compiler yet
void publish_switch(switch_::Switch *s, bool state);
void publish_select(select::Select *s, size_t index);
public:
KT0803Component();
// float get_setup_priority() const override { return setup_priority::HARDWARE; }
void setup() override;
void dump_config() override;
void update() override;
void loop() override;
SUB_NUMBER_EX(frequency, float)
SUB_SELECT_EX(deviation, FrequencyDeviation)
SUB_SWITCH_EX(mute)
SUB_SWITCH_EX(mono)
SUB_SELECT_EX(pre_emphasis, PreEmphasis)
SUB_NUMBER_EX(pga, float)
SUB_NUMBER_EX(rfgain, float)
SUB_SELECT_EX(pilot_tone_amplitude, PilotToneAmplitude)
SUB_SELECT_EX(bass_boost_control, BassBoostControl)
SUB_SWITCH_EX(auto_pa_down)
SUB_SWITCH_EX(pa_down)
SUB_SWITCH_EX(standby_enable)
SUB_SWITCH_EX(pa_bias)
SUB_SELECT_EX(audio_limiter_level, AudioLimiterLevel)
SUB_SELECT_EX(switch_mode, SwitchMode)
SUB_SWITCH_EX(au_enhance)
SUB_SWITCH_EX(ref_clk_enable)
SUB_SELECT_EX(ref_clk, ReferenceClock)
SUB_SWITCH_EX(xtal_enable)
SUB_SELECT_EX(xtal_sel, XtalSel)
SUB_SWITCH_EX(alc_enable)
SUB_NUMBER_EX(alc_gain, float)
SUB_SELECT_EX(alc_attack_time, AlcTime)
SUB_SELECT_EX(alc_decay_time, AlcTime)
SUB_SELECT_EX(alc_hold_time, AlcHoldTime)
SUB_SELECT_EX(alc_high, AlcHigh)
SUB_SELECT_EX(alc_low, AlcLow)
SUB_SWITCH_EX(silence_detection)
SUB_SELECT_EX(silence_duration, SilenceLowAndHighLevelDurationTime)
SUB_SELECT_EX(silence_high, SilenceHigh)
SUB_SELECT_EX(silence_low, SilenceLow)
SUB_SELECT_EX(silence_high_counter, SilenceHighLevelCounter)
SUB_SELECT_EX(silence_low_counter, SilenceLowLevelCounter)
SUB_BINARY_SENSOR_EX(pw_ok)
SUB_BINARY_SENSOR_EX(slncid)
void set_chip_id(ChipId value);
ChipId get_chip_id();
std::string get_chip_string() const;
void set_frequency(float value);
float get_frequency();
void set_pga(float value);
float get_pga();
void set_rfgain(float value);
float get_rfgain();
void set_mute(bool value);
bool get_mute();
void set_mono(bool value);
bool get_mono();
void set_pre_emphasis(PreEmphasis value);
PreEmphasis get_pre_emphasis();
void set_pilot_tone_amplitude(PilotToneAmplitude value);
PilotToneAmplitude get_pilot_tone_amplitude();
void set_bass_boost_control(BassBoostControl value);
BassBoostControl get_bass_boost_control();
void set_alc_enable(bool value);
bool get_alc_enable();
void set_auto_pa_down(bool value);
bool get_auto_pa_down();
void set_pa_down(bool value);
bool get_pa_down();
void set_standby_enable(bool value);
bool get_standby_enable();
void set_alc_attack_time(AlcTime value);
AlcTime get_alc_attack_time();
void set_alc_decay_time(AlcTime value);
AlcTime get_alc_decay_time();
void set_pa_bias(bool value);
bool get_pa_bias();
void set_audio_limiter_level(AudioLimiterLevel value);
AudioLimiterLevel get_audio_limiter_level();
void set_switch_mode(SwitchMode value);
SwitchMode get_switch_mode();
void set_silence_high(SilenceHigh value);
SilenceHigh get_silence_high();
void set_silence_low(SilenceLow value);
SilenceLow get_silence_low();
void set_silence_detection(bool value);
bool get_silence_detection();
void set_silence_duration(SilenceLowAndHighLevelDurationTime value);
SilenceLowAndHighLevelDurationTime get_silence_duration();
void set_silence_high_counter(SilenceHighLevelCounter value);
SilenceHighLevelCounter get_silence_high_counter();
void set_silence_low_counter(SilenceLowLevelCounter value);
SilenceLowLevelCounter get_silence_low_counter();
void set_alc_gain(float value);
float get_alc_gain();
void set_xtal_sel(XtalSel value);
XtalSel get_xtal_sel();
void set_au_enhance(bool value);
bool get_au_enhance();
void set_frequency_deviation(FrequencyDeviation value);
FrequencyDeviation get_frequency_deviation();
void set_ref_clk(ReferenceClock value);
ReferenceClock get_ref_clk();
void set_xtal_enable(bool value);
bool get_xtal_enable();
void set_ref_clk_enable(bool value);
bool get_ref_clk_enable();
void set_alc_high(AlcHigh value);
AlcHigh get_alc_high();
void set_alc_hold_time(AlcHoldTime value);
AlcHoldTime get_alc_hold_time();
void set_alc_low(AlcLow value);
AlcLow get_alc_low();
};
template<typename... Ts> class SetFrequencyAction : public Action<Ts...>, public Parented<KT0803Component> {

View file

@ -3,6 +3,7 @@ from esphome.components import number
import esphome.config_validation as cv
from esphome.const import (
CONF_FREQUENCY,
CONF_GAIN,
UNIT_DECIBEL,
DEVICE_CLASS_FREQUENCY,
DEVICE_CLASS_SIGNAL_STRENGTH,
@ -12,9 +13,9 @@ from .. import (
CONF_KT0803_ID,
KT0803Component,
kt0803_ns,
CONF_ALC,
CONF_PGA,
CONF_RFGAIN,
CONF_ALC_GAIN,
UNIT_MEGA_HERTZ,
UNIT_DECIBEL_MICRO_VOLT,
)
@ -24,6 +25,17 @@ PgaNumber = kt0803_ns.class_("PgaNumber", number.Number)
RfGainNumber = kt0803_ns.class_("RfGainNumber", number.Number)
AlcGainNumber = kt0803_ns.class_("AlcGainNumber", number.Number)
ALC_SCHEMA = cv.Schema(
{
cv.Optional(CONF_GAIN): number.number_schema(
AlcGainNumber,
unit_of_measurement=UNIT_DECIBEL,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
entity_category=ENTITY_CATEGORY_CONFIG,
),
}
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_KT0803_ID): cv.use_id(KT0803Component),
@ -45,56 +57,57 @@ CONFIG_SCHEMA = cv.Schema(
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_ALC_GAIN): number.number_schema(
AlcGainNumber,
unit_of_measurement=UNIT_DECIBEL,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_ALC): ALC_SCHEMA,
}
)
async def new_number(config, id, setter, min_value, max_value, step):
async def new_number(p, config, id, setter, min_value, max_value, step):
if c := config.get(id):
n = await number.new_number(
c, min_value=min_value, max_value=max_value, step=step
)
await cg.register_parented(n, config[CONF_KT0803_ID])
await cg.register_parented(n, p)
cg.add(setter(n))
return n
async def to_code(config):
kt0803_component = await cg.get_variable(config[CONF_KT0803_ID])
p = await cg.get_variable(config[CONF_KT0803_ID])
await new_number(
p,
config,
CONF_FREQUENCY,
kt0803_component.set_frequency_number,
p.set_frequency_number,
kt0803_ns.CHSEL_MIN,
kt0803_ns.CHSEL_MAX,
kt0803_ns.CHSEL_STEP,
)
await new_number(
p,
config,
CONF_PGA,
kt0803_component.set_pga_number,
p.set_pga_number,
kt0803_ns.PGA_MIN,
kt0803_ns.PGA_MAX,
kt0803_ns.PGA_STEP,
)
await new_number(
p,
config,
CONF_RFGAIN,
kt0803_component.set_rfgain_number,
p.set_rfgain_number,
kt0803_ns.RFGAIN_MIN,
kt0803_ns.RFGAIN_MAX,
0.1,
)
await new_number(
config,
CONF_ALC_GAIN,
kt0803_component.set_alc_gain_number,
kt0803_ns.ALC_GAIN_MIN,
kt0803_ns.ALC_GAIN_MAX,
3,
)
if alc_config := config.get(CONF_ALC):
await new_number(
p,
alc_config,
CONF_GAIN,
p.set_alc_gain_number,
kt0803_ns.ALC_GAIN_MIN,
kt0803_ns.ALC_GAIN_MAX,
3,
)

View file

@ -2,6 +2,9 @@ import esphome.codegen as cg
from esphome.components import select
import esphome.config_validation as cv
from esphome.const import (
CONF_HIGH,
CONF_LOW,
CONF_DURATION,
ENTITY_CATEGORY_CONFIG,
ICON_PULSE,
)
@ -9,68 +12,151 @@ from .. import (
CONF_KT0803_ID,
KT0803Component,
kt0803_ns,
CONF_REF_CLK,
CONF_XTAL,
CONF_ALC,
CONF_SILENCE,
CONF_DEVIATION,
CONF_PRE_EMPHASIS,
CONF_PILOT_TONE_AMPLITUDE,
CONF_BASS_BOOST_CONTROL,
CONF_ALC_ATTACK_TIME,
CONF_ALC_DECAY_TIME,
CONF_AUDIO_LIMITER_LEVEL,
CONF_SWITCH_MODE,
CONF_SILENCE_HIGH,
CONF_SILENCE_LOW,
CONF_SILENCE_DURATION,
CONF_SILENCE_HIGH_COUNTER,
CONF_SILENCE_LOW_COUNTER,
CONF_XTAL_SEL,
CONF_FREQUENCY_DEVIATION,
CONF_REF_CLK,
CONF_ALC_HIGH,
CONF_ALC_HOLD_TIME,
CONF_ALC_LOW,
CONF_SEL,
CONF_HOLD_TIME,
CONF_ATTACK_TIME,
CONF_DECAY_TIME,
CONF_HIGH_COUNTER,
CONF_LOW_COUNTER,
ICON_SLEEP,
ICON_SINE_WAVE,
ICON_SPEAKER,
FREQUENCY_DEVIATION,
PRE_EMPHASIS,
PILOT_TONE_AMPLITUDE,
BASS_BOOST_CONTROL,
ALC_TIME,
AUDIO_LIMITER_LEVEL,
SWITCH_MODE,
REFERENCE_CLOCK,
XTAL_SEL,
ALC_TIME,
ALC_HOLD_TIME,
ALC_HIGH,
ALC_LOW,
SILENCE_HIGH,
SILENCE_LOW,
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME,
SILENCE_HIGH_LEVEL_COUNTER,
SILENCE_LOW_LEVEL_COUNTER,
XTAL_SEL,
FREQUENCY_DEVIATION,
REFERENCE_CLOCK,
ALC_HIGH,
ALC_HOLD_TIME,
ALC_LOW,
)
FrequencyDeviationSelect = kt0803_ns.class_("FrequencyDeviationSelect", select.Select)
PreEmphasisSelect = kt0803_ns.class_("PreEmphasisSelect", select.Select)
PilotToneAmplitudeSelect = kt0803_ns.class_("PilotToneAmplitudeSelect", select.Select)
BassBoostControlSelect = kt0803_ns.class_("BassBoostControlSelect", select.Select)
AlcAttackTimeSelect = kt0803_ns.class_("AlcAttackTimeSelect", select.Select)
AlcDecayTimeSelect = kt0803_ns.class_("AlcDecayTimeSelect", select.Select)
AudioLimiterLevelSelect = kt0803_ns.class_("AudioLimiterLevelSelect", select.Select)
SwitchModeSelect = kt0803_ns.class_("SwitchModeSelect", select.Select)
RefClkSelect = kt0803_ns.class_("RefClkSelect", select.Select)
XtalSelSelect = kt0803_ns.class_("XtalSelSelect", select.Select)
AlcAttackTimeSelect = kt0803_ns.class_("AlcAttackTimeSelect", select.Select)
AlcDecayTimeSelect = kt0803_ns.class_("AlcDecayTimeSelect", select.Select)
AlcHoldTimeSelect = kt0803_ns.class_("AlcHoldTimeSelect", select.Select)
AlcHighSelect = kt0803_ns.class_("AlcHighSelect", select.Select)
AlcLowSelect = kt0803_ns.class_("AlcLowSelect", select.Select)
SilenceDurationSelect = kt0803_ns.class_("SilenceDurationSelect", select.Select)
SilenceHighSelect = kt0803_ns.class_("SilenceHighSelect", select.Select)
SilenceLowSelect = kt0803_ns.class_("SilenceLowSelect", select.Select)
SilenceDurationSelect = kt0803_ns.class_("SilenceDurationSelect", select.Select)
SilenceHighCounterSelect = kt0803_ns.class_("SilenceHighCounterSelect", select.Select)
SilenceLowCounterSelect = kt0803_ns.class_("SilenceLowCounterSelect", select.Select)
XtalSelSelect = kt0803_ns.class_("XtalSelSelect", select.Select)
FrequencyDeviationSelect = kt0803_ns.class_("FrequencyDeviationSelect", select.Select)
RefClkSelect = kt0803_ns.class_("RefClkSelect", select.Select)
AlcHighSelect = kt0803_ns.class_("AlcHighSelect", select.Select)
AlcHoldTimeSelect = kt0803_ns.class_("AlcHoldTimeSelect", select.Select)
AlcLowSelect = kt0803_ns.class_("AlcLowSelect", select.Select)
REF_CLK_SCHEMA = cv.Schema(
{
cv.Optional(CONF_REF_CLK): select.select_schema(
RefClkSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
}
)
XTAL_SCHEMA = cv.Schema(
{
cv.Optional(CONF_SEL): select.select_schema(
XtalSelSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
),
}
)
ALC_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ATTACK_TIME): select.select_schema(
AlcAttackTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_DECAY_TIME): select.select_schema(
AlcDecayTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_HOLD_TIME): select.select_schema(
AlcHoldTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_HIGH): select.select_schema(
AlcHighSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_LOW): select.select_schema(
AlcLowSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
}
)
SILENCE_SCHEMA = cv.Schema(
{
cv.Optional(CONF_DURATION): select.select_schema(
SilenceDurationSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_HIGH): select.select_schema(
SilenceHighSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_LOW): select.select_schema(
SilenceLowSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_HIGH_COUNTER): select.select_schema(
SilenceHighCounterSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_LOW_COUNTER): select.select_schema(
SilenceLowCounterSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
}
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_KT0803_ID): cv.use_id(KT0803Component),
cv.Optional(CONF_DEVIATION): select.select_schema(
FrequencyDeviationSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_PRE_EMPHASIS): select.select_schema(
PreEmphasisSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
@ -86,16 +172,6 @@ CONFIG_SCHEMA = cv.Schema(
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SPEAKER,
),
cv.Optional(CONF_ALC_ATTACK_TIME): select.select_schema(
AlcAttackTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_ALC_DECAY_TIME): select.select_schema(
AlcDecayTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_AUDIO_LIMITER_LEVEL): select.select_schema(
AudioLimiterLevelSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
@ -106,128 +182,95 @@ CONFIG_SCHEMA = cv.Schema(
entity_category=ENTITY_CATEGORY_CONFIG,
# icon=ICON_,
),
cv.Optional(CONF_SILENCE_HIGH): select.select_schema(
SilenceHighSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_SILENCE_LOW): select.select_schema(
SilenceLowSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_SILENCE_DURATION): select.select_schema(
SilenceDurationSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_SILENCE_HIGH_COUNTER): select.select_schema(
SilenceHighCounterSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_SILENCE_LOW_COUNTER): select.select_schema(
SilenceLowCounterSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_XTAL_SEL): select.select_schema(
XtalSelSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
),
cv.Optional(CONF_FREQUENCY_DEVIATION): select.select_schema(
FrequencyDeviationSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_REF_CLK): select.select_schema(
RefClkSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_ALC_HIGH): select.select_schema(
AlcHighSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_ALC_HOLD_TIME): select.select_schema(
AlcHoldTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_ALC_LOW): select.select_schema(
AlcLowSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_REF_CLK): REF_CLK_SCHEMA,
cv.Optional(CONF_XTAL): XTAL_SCHEMA,
cv.Optional(CONF_ALC): ALC_SCHEMA,
cv.Optional(CONF_SILENCE): SILENCE_SCHEMA,
}
)
async def new_select(config, id, setter, options):
async def new_select(p, config, id, setter, options):
if c := config.get(id):
s = await select.new_select(c, options=list(options.keys()))
await cg.register_parented(s, config[CONF_KT0803_ID])
await cg.register_parented(s, p)
cg.add(setter(s))
return s
async def to_code(config):
c = await cg.get_variable(config[CONF_KT0803_ID])
await new_select(config, CONF_PRE_EMPHASIS, c.set_pre_emphasis_select, PRE_EMPHASIS)
p = await cg.get_variable(config[CONF_KT0803_ID])
await new_select(
p, config, CONF_DEVIATION, p.set_deviation_select, FREQUENCY_DEVIATION
)
await new_select(
p, config, CONF_PRE_EMPHASIS, p.set_pre_emphasis_select, PRE_EMPHASIS
)
await new_select(
p,
config,
CONF_PILOT_TONE_AMPLITUDE,
c.set_pilot_tone_amplitude_select,
p.set_pilot_tone_amplitude_select,
PILOT_TONE_AMPLITUDE,
)
await new_select(
p,
config,
CONF_BASS_BOOST_CONTROL,
c.set_bass_boost_control_select,
p.set_bass_boost_control_select,
BASS_BOOST_CONTROL,
)
await new_select(
config, CONF_ALC_ATTACK_TIME, c.set_alc_attack_time_select, ALC_TIME
)
await new_select(config, CONF_ALC_DECAY_TIME, c.set_alc_decay_time_select, ALC_TIME)
await new_select(
p,
config,
CONF_AUDIO_LIMITER_LEVEL,
c.set_audio_limiter_level_select,
p.set_audio_limiter_level_select,
AUDIO_LIMITER_LEVEL,
)
await new_select(config, CONF_SWITCH_MODE, c.set_switch_mode_select, SWITCH_MODE)
await new_select(config, CONF_SILENCE_HIGH, c.set_silence_high_select, SILENCE_HIGH)
await new_select(config, CONF_SILENCE_LOW, c.set_silence_low_select, SILENCE_LOW)
await new_select(
config,
CONF_SILENCE_DURATION,
c.set_silence_duration_select,
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME,
)
await new_select(
config,
CONF_SILENCE_HIGH_COUNTER,
c.set_silence_high_counter_select,
SILENCE_HIGH_LEVEL_COUNTER,
)
await new_select(
config,
CONF_SILENCE_LOW_COUNTER,
c.set_silence_low_counter_select,
SILENCE_LOW_LEVEL_COUNTER,
)
await new_select(config, CONF_XTAL_SEL, c.set_xtal_sel_select, XTAL_SEL)
await new_select(
config,
CONF_FREQUENCY_DEVIATION,
c.set_frequency_deviation_select,
FREQUENCY_DEVIATION,
)
await new_select(config, CONF_REF_CLK, c.set_ref_clk_select, REFERENCE_CLOCK)
await new_select(config, CONF_ALC_HIGH, c.set_alc_high_select, ALC_HIGH)
await new_select(
config, CONF_ALC_HOLD_TIME, c.set_alc_hold_time_select, ALC_HOLD_TIME
)
await new_select(config, CONF_ALC_LOW, c.set_alc_low_select, ALC_LOW)
await new_select(p, config, CONF_SWITCH_MODE, p.set_switch_mode_select, SWITCH_MODE)
if ref_clk_config := config.get(CONF_REF_CLK):
await new_select(
p, ref_clk_config, CONF_REF_CLK, p.set_ref_clk_select, REFERENCE_CLOCK
)
if xtal_config := config.get(CONF_XTAL):
await new_select(p, xtal_config, CONF_SEL, p.set_xtal_sel_select, XTAL_SEL)
if alc_config := config.get(CONF_ALC):
await new_select(
p, alc_config, CONF_ATTACK_TIME, p.set_alc_attack_time_select, ALC_TIME
)
await new_select(
p, alc_config, CONF_DECAY_TIME, p.set_alc_decay_time_select, ALC_TIME
)
await new_select(
p, alc_config, CONF_HOLD_TIME, p.set_alc_hold_time_select, ALC_HOLD_TIME
)
await new_select(p, alc_config, CONF_HIGH, p.set_alc_high_select, ALC_HIGH)
await new_select(p, alc_config, CONF_LOW, p.set_alc_low_select, ALC_LOW)
if silence_config := config.get(CONF_SILENCE):
await new_select(
p,
silence_config,
CONF_DURATION,
p.set_silence_duration_select,
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME,
)
await new_select(
p, silence_config, CONF_HIGH, p.set_silence_high_select, SILENCE_HIGH
)
await new_select(
p, silence_config, CONF_LOW, p.set_silence_low_select, SILENCE_LOW
)
await new_select(
p,
silence_config,
CONF_HIGH_COUNTER,
p.set_silence_high_counter_select,
SILENCE_HIGH_LEVEL_COUNTER,
)
await new_select(
p,
silence_config,
CONF_LOW_COUNTER,
p.set_silence_low_counter_select,
SILENCE_LOW_LEVEL_COUNTER,
)

View file

@ -1,4 +1,4 @@
#include "frequency_deviation_select.h"
#include "deviation_select.h"
namespace esphome {
namespace kt0803 {
@ -6,7 +6,7 @@ namespace kt0803 {
void FrequencyDeviationSelect::control(const std::string &value) {
this->publish_state(value);
if (auto index = this->active_index()) {
this->parent_->set_frequency_deviation((FrequencyDeviation) *index);
this->parent_->set_deviation((FrequencyDeviation) *index);
}
}

View file

@ -10,17 +10,19 @@ from .. import (
CONF_KT0803_ID,
KT0803Component,
kt0803_ns,
CONF_REF_CLK,
CONF_XTAL,
CONF_ALC,
CONF_SILENCE,
CONF_MUTE,
CONF_MONO,
CONF_ALC_ENABLE,
CONF_ENABLE,
CONF_AUTO_PA_DOWN,
CONF_PA_DOWN,
CONF_STANDBY_ENABLE,
CONF_PA_BIAS,
CONF_SILENCE_DETECTION,
CONF_DETECTION,
CONF_AU_ENHANCE,
CONF_XTAL_ENABLE,
CONF_REF_CLK_ENABLE,
ICON_VOLUME_MUTE,
ICON_EAR_HEARING,
ICON_SINE_WAVE,
@ -29,16 +31,60 @@ from .. import (
MuteSwitch = kt0803_ns.class_("MuteSwitch", switch.Switch)
MonoSwitch = kt0803_ns.class_("MonoSwitch", switch.Switch)
AlcEnableSwitch = kt0803_ns.class_("AlcEnableSwitch", switch.Switch)
AutoPaDownSwitch = kt0803_ns.class_("AutoPaDownSwitch", switch.Switch)
PaDownSwitch = kt0803_ns.class_("PaDownSwitch", switch.Switch)
StandbyEnableSwitch = kt0803_ns.class_("StandbyEnableSwitch", switch.Switch)
PaBiasSwitch = kt0803_ns.class_("PaBiasSwitch", switch.Switch)
SilenceDetectionSwitch = kt0803_ns.class_("SilenceDetectionSwitch", switch.Switch)
AuEnhanceSwitch = kt0803_ns.class_("AuEnhanceSwitch", switch.Switch)
AuEnhanceSwitch = kt0803_ns.class_("AuEnhanceSwitch", switch.Switch)
XtalEnableSwitch = kt0803_ns.class_("XtalEnableSwitch", switch.Switch)
RefClkEnableSwitch = kt0803_ns.class_("RefClkEnableSwitch", switch.Switch)
XtalEnableSwitch = kt0803_ns.class_("XtalEnableSwitch", switch.Switch)
AlcEnableSwitch = kt0803_ns.class_("AlcEnableSwitch", switch.Switch)
SilenceDetectionSwitch = kt0803_ns.class_("SilenceDetectionSwitch", switch.Switch)
REF_CLK_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENABLE): switch.switch_schema(
RefClkEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
),
}
)
XTAL_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENABLE): switch.switch_schema(
XtalEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
),
}
)
ALC_SCHEMA = cv.Schema(
{
cv.Optional(CONF_ENABLE): switch.switch_schema(
AlcEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
}
)
SILENCE_SCHEMA = cv.Schema(
{
cv.Optional(CONF_DETECTION): switch.switch_schema(
SilenceDetectionSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
}
)
CONFIG_SCHEMA = cv.Schema(
{
@ -55,12 +101,6 @@ CONFIG_SCHEMA = cv.Schema(
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_EAR_HEARING,
),
cv.Optional(CONF_ALC_ENABLE): switch.switch_schema(
AlcEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_AUTO_PA_DOWN): switch.switch_schema(
AutoPaDownSwitch,
device_class=DEVICE_CLASS_SWITCH,
@ -85,51 +125,44 @@ CONFIG_SCHEMA = cv.Schema(
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_SILENCE_DETECTION): switch.switch_schema(
SilenceDetectionSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SLEEP,
),
cv.Optional(CONF_AU_ENHANCE): switch.switch_schema(
AuEnhanceSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_XTAL_ENABLE): switch.switch_schema(
XtalEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
),
cv.Optional(CONF_REF_CLK_ENABLE): switch.switch_schema(
RefClkEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
),
cv.Optional(CONF_REF_CLK): REF_CLK_SCHEMA,
cv.Optional(CONF_XTAL): XTAL_SCHEMA,
cv.Optional(CONF_ALC): ALC_SCHEMA,
cv.Optional(CONF_SILENCE): SILENCE_SCHEMA,
}
)
async def new_switch(config, id, setter):
async def new_switch(p, config, id, setter):
if c := config.get(id):
s = await switch.new_switch(c)
await cg.register_parented(s, config[CONF_KT0803_ID])
await cg.register_parented(s, p)
cg.add(setter(s))
return s
async def to_code(config):
c = await cg.get_variable(config[CONF_KT0803_ID])
await new_switch(config, CONF_MUTE, c.set_mute_switch)
await new_switch(config, CONF_MONO, c.set_mono_switch)
await new_switch(config, CONF_ALC_ENABLE, c.set_alc_enable_switch)
await new_switch(config, CONF_AUTO_PA_DOWN, c.set_auto_pa_down_switch)
await new_switch(config, CONF_PA_DOWN, c.set_pa_down_switch)
await new_switch(config, CONF_STANDBY_ENABLE, c.set_standby_enable_switch)
await new_switch(config, CONF_PA_BIAS, c.set_pa_bias_switch)
await new_switch(config, CONF_SILENCE_DETECTION, c.set_silence_detection_switch)
await new_switch(config, CONF_AU_ENHANCE, c.set_au_enhance_switch)
await new_switch(config, CONF_XTAL_ENABLE, c.set_xtal_enable_switch)
await new_switch(config, CONF_REF_CLK_ENABLE, c.set_ref_clk_enable_switch)
p = await cg.get_variable(config[CONF_KT0803_ID])
await new_switch(p, config, CONF_MUTE, p.set_mute_switch)
await new_switch(p, config, CONF_MONO, p.set_mono_switch)
await new_switch(p, config, CONF_AUTO_PA_DOWN, p.set_auto_pa_down_switch)
await new_switch(p, config, CONF_PA_DOWN, p.set_pa_down_switch)
await new_switch(p, config, CONF_STANDBY_ENABLE, p.set_standby_enable_switch)
await new_switch(p, config, CONF_PA_BIAS, p.set_pa_bias_switch)
await new_switch(p, config, CONF_AU_ENHANCE, p.set_au_enhance_switch)
if ref_clk_config := config.get(CONF_REF_CLK):
await new_switch(p, ref_clk_config, CONF_ENABLE, p.set_ref_clk_enable_switch)
if xtal_config := config.get(CONF_XTAL):
await new_switch(p, xtal_config, CONF_ENABLE, p.set_xtal_enable_switch)
if alc_config := config.get(CONF_ALC):
await new_switch(p, alc_config, CONF_ENABLE, p.set_alc_enable_switch)
if silence_config := config.get(CONF_SILENCE):
await new_switch(
p, silence_config, CONF_DETECTION, p.set_silence_detection_switch
)

View file

@ -7,43 +7,47 @@ kt0803:
id: kt0803_transmitter
chip_id: KT0803L
frequency: 87.5
pga: -15
rfgain: 108
deviation: 75kHz
mute: False
mono: False
pre_emphasis: 75us
pga: 0
rfgain: 108
pilot_tone_amplitude: Low
bass_boost_control: Disabled
alc_enable: False
auto_pa_down: True
pa_down: False
standby_enable: False
alc_attack_time: 25us
alc_decay_time: 25us
pa_bias: False
audio_limiter_level: '0.875'
switch_mode: Mute
silence_detection: True
silence_high: '32mV'
silence_low: '8mV'
silence_duration: '100ms'
silence_high_counter: '15'
silence_low_counter: '1'
alc_gain: -3
xtal_sel: '32.768kHz'
au_enhance: False
frequency_deviation: 75kHz
ref_clk: '32.768kHz'
xtal_enable: True
ref_clk_enable: False
alc_high: '0.6'
alc_hold_time: 5s
alc_low: '0.25'
pw_ok:
name: Power OK
slncid:
name: Silence Detected
update_interval: 3s
ref_clk:
enable: False
ref_clk: '32.768kHz'
xtal:
enable: True
sel: '32.768kHz'
alc:
enable: False
gain: -3
attack_time: 25us
decay_time: 25us
hold_time: 5s
high: '0.6'
low: '0.25'
silence:
detection: True
duration: '100ms'
high: '32mV'
low: '8mV'
high_counter: '15'
low_counter: '1'
sensor:
pw_ok:
name: Power OK
slncid:
name: Silence Detected
number:
- platform: kt0803
@ -54,8 +58,9 @@ number:
name: PGA Gain Control
rfgain:
name: RF Gain
alc_gain:
name: ALC Gain
alc:
gain:
name: ALC Gain
switch:
- platform: kt0803
@ -63,8 +68,6 @@ switch:
name: Mute
mono:
name: Mono
alc_enable:
name: ALC Enable
auto_pa_down:
name: Automatic PA Power Down
pa_down:
@ -73,50 +76,60 @@ switch:
name: Standby Enable
pa_bias:
name: PA Bias
silence_detection:
name: Silence Detection
au_enhance:
name: Audio Frequency Response Enhancement
xtal_enable:
name: Crystal Oscillator Enable
ref_clk_enable:
name: Refence Clock Enable
ref_clk:
enable:
name: Refence Clock Enable
xtal:
enable:
name: Crystal Oscillator Enable
alc:
enable:
name: ALC Enable
silence:
detection:
name: Silence Detection
select:
- platform: kt0803
deviation:
name: Frequency Deviation
pre_emphasis:
name: Pre-emphasis
pilot_tone_amplitude:
name: Pilot Tone Amplitude
bass_boost_control:
name: Bass Boost Control
alc_attack_time:
name: ALC Attack Time
alc_decay_time:
name: ALC Decay Time
audio_limiter_level:
name: Audio Limiter Level
switch_mode:
name: Switching Channel Mode Selection
silence_high:
name: Silence High
silence_low:
name: Silence Low
silence_duration:
name: Silence Duration
silence_high_counter:
name: Silence High Counter
silence_low_counter:
name: Silence Low Counter
xtal_sel:
name: Xtal Selection
frequency_deviation:
name: Frequency Deviation
xtal:
sel:
name: Xtal Selection
ref_clk:
name: Refence Clock
alc_high:
name: ALC High Threshold
alc_hold_time:
name: ALC Hold Time
alc_low:
name: ALC Low Threshold
ref_clk:
name: Refence Clock
alc:
attack_time:
name: ALC Attack Time
decay_time:
name: ALC Decay Time
hold_time:
name: ALC Hold Time
high:
name: ALC High Threshold
low:
name: ALC Low Threshold
silence:
duration:
name: Silence Duration
high:
name: Silence High
low:
name: Silence Low
high_counter:
name: Silence High Counter
low_counter:
name: Silence Low Counter

View file

@ -7,43 +7,47 @@ kt0803:
id: kt0803_transmitter
chip_id: KT0803L
frequency: 87.5
pga: -15
rfgain: 108
deviation: 75kHz
mute: False
mono: False
pre_emphasis: 75us
pga: 0
rfgain: 108
pilot_tone_amplitude: Low
bass_boost_control: Disabled
alc_enable: False
auto_pa_down: True
pa_down: False
standby_enable: False
alc_attack_time: 25us
alc_decay_time: 25us
pa_bias: False
audio_limiter_level: '0.875'
switch_mode: Mute
silence_detection: True
silence_high: '32mV'
silence_low: '8mV'
silence_duration: '100ms'
silence_high_counter: '15'
silence_low_counter: '1'
alc_gain: -3
xtal_sel: '32.768kHz'
au_enhance: False
frequency_deviation: 75kHz
ref_clk: '32.768kHz'
xtal_enable: True
ref_clk_enable: False
alc_high: '0.6'
alc_hold_time: 5s
alc_low: '0.25'
pw_ok:
name: Power OK
slncid:
name: Silence Detected
update_interval: 3s
ref_clk:
enable: False
ref_clk: '32.768kHz'
xtal:
enable: True
sel: '32.768kHz'
alc:
enable: False
gain: -3
attack_time: 25us
decay_time: 25us
hold_time: 5s
high: '0.6'
low: '0.25'
silence:
detection: True
duration: '100ms'
high: '32mV'
low: '8mV'
high_counter: '15'
low_counter: '1'
sensor:
pw_ok:
name: Power OK
slncid:
name: Silence Detected
number:
- platform: kt0803
@ -54,8 +58,9 @@ number:
name: PGA Gain Control
rfgain:
name: RF Gain
alc_gain:
name: ALC Gain
alc:
gain:
name: ALC Gain
switch:
- platform: kt0803
@ -63,8 +68,6 @@ switch:
name: Mute
mono:
name: Mono
alc_enable:
name: ALC Enable
auto_pa_down:
name: Automatic PA Power Down
pa_down:
@ -73,50 +76,60 @@ switch:
name: Standby Enable
pa_bias:
name: PA Bias
silence_detection:
name: Silence Detection
au_enhance:
name: Audio Frequency Response Enhancement
xtal_enable:
name: Crystal Oscillator Enable
ref_clk_enable:
name: Refence Clock Enable
ref_clk:
enable:
name: Refence Clock Enable
xtal:
enable:
name: Crystal Oscillator Enable
alc:
enable:
name: ALC Enable
silence:
detection:
name: Silence Detection
select:
- platform: kt0803
deviation:
name: Frequency Deviation
pre_emphasis:
name: Pre-emphasis
pilot_tone_amplitude:
name: Pilot Tone Amplitude
bass_boost_control:
name: Bass Boost Control
alc_attack_time:
name: ALC Attack Time
alc_decay_time:
name: ALC Decay Time
audio_limiter_level:
name: Audio Limiter Level
switch_mode:
name: Switching Channel Mode Selection
silence_high:
name: Silence High
silence_low:
name: Silence Low
silence_duration:
name: Silence Duration
silence_high_counter:
name: Silence High Counter
silence_low_counter:
name: Silence Low Counter
xtal_sel:
name: Xtal Selection
frequency_deviation:
name: Frequency Deviation
xtal:
sel:
name: Xtal Selection
ref_clk:
name: Refence Clock
alc_high:
name: ALC High Threshold
alc_hold_time:
name: ALC Hold Time
alc_low:
name: ALC Low Threshold
ref_clk:
name: Refence Clock
alc:
attack_time:
name: ALC Attack Time
decay_time:
name: ALC Decay Time
hold_time:
name: ALC Hold Time
high:
name: ALC High Threshold
low:
name: ALC Low Threshold
silence:
duration:
name: Silence Duration
high:
name: Silence High
low:
name: Silence Low
high_counter:
name: Silence High Counter
low_counter:
name: Silence Low Counter