finished implementing all registers

This commit is contained in:
Gábor Poczkodi 2024-10-13 18:55:55 +02:00
parent 101ccdda4c
commit 6c2ecd4e9c
17 changed files with 419 additions and 24 deletions

View file

@ -64,6 +64,12 @@ CONF_ALC_GAIN = "alc_gain"
CONF_XTAL_SEL = "xtal_sel"
CONF_AU_ENHANCE = "au_enhance"
CONF_FREQUENCY_DEVIATION = "frequency_deviation"
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"
SetFrequencyAction = kt0803_ns.class_(
"SetFrequencyAction", automation.Action, cg.Parented.template(KT0803Component)
@ -231,27 +237,27 @@ REFERENCE_CLOCK = {
}
AlcHigh = kt0803_ns.enum("AlcHigh", True)
ALC_HIGH_THRESHOLD_SELECTION = {
"50ms": AlcHigh.ALCHIGHTH_50MS,
"100ms": AlcHigh.ALCHIGHTH_100MS,
"150ms": AlcHigh.ALCHIGHTH_150MS,
"200ms": AlcHigh.ALCHIGHTH_200MS,
"1s": AlcHigh.ALCHIGHTH_1S,
"5s": AlcHigh.ALCHIGHTH_5S,
"10s": AlcHigh.ALCHIGHTH_10S,
"15s": AlcHigh.ALCHIGHTH_15S,
ALC_HIGH = {
"0.6": AlcHigh.ALCHOLD_06,
"0.5": AlcHigh.ALCHOLD_05,
"0.4": AlcHigh.ALCHOLD_04,
"0.3": AlcHigh.ALCHOLD_03,
"0.2": AlcHigh.ALCHOLD_02,
"0.1": AlcHigh.ALCHOLD_01,
"0.05": AlcHigh.ALCHOLD_005,
"0.01": AlcHigh.ALCHOLD_001,
}
AlcHoldTime = kt0803_ns.enum("AlcHoldTime", True)
ALC_HOLD_TIME = {
"0.6": AlcHoldTime.ALCHOLD_06,
"0.5": AlcHoldTime.ALCHOLD_05,
"0.4": AlcHoldTime.ALCHOLD_04,
"0.3": AlcHoldTime.ALCHOLD_03,
"0.2": AlcHoldTime.ALCHOLD_02,
"0.1": AlcHoldTime.ALCHOLD_01,
"0.05": AlcHoldTime.ALCHOLD_005,
"0.01": AlcHoldTime.ALCHOLD_001,
"50ms": AlcHoldTime.ALCHIGHTH_50MS,
"100ms": AlcHoldTime.ALCHIGHTH_100MS,
"150ms": AlcHoldTime.ALCHIGHTH_150MS,
"200ms": AlcHoldTime.ALCHIGHTH_200MS,
"1s": AlcHoldTime.ALCHIGHTH_1S,
"5s": AlcHoldTime.ALCHIGHTH_5S,
"10s": AlcHoldTime.ALCHIGHTH_10S,
"15s": AlcHoldTime.ALCHIGHTH_15S,
}
AlcLow = kt0803_ns.enum("AlcLow", True)
@ -310,6 +316,12 @@ CONFIG_SCHEMA = (
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,
@ -368,5 +380,11 @@ async def to_code(config):
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)

View file

@ -162,6 +162,12 @@ void KT0803Component::setup() {
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() {
@ -295,6 +301,8 @@ void KT0803Component::set_pga(float value) {
this->write_reg_(0x04);
}
ESP_LOGV(TAG, "PGA %02X", (int) pga);
this->publish_pga();
}
@ -667,7 +675,7 @@ void KT0803Component::set_xtal_sel(XtalSel value) {
if (value >= XtalSel::LAST) {
ESP_LOGE(TAG, "set_xtal_sel(%d) invalid", value);
return;
}
}
this->state_.XTAL_SEL = (uint8_t) value;
if (this->chip_id_ == ChipId::KT0803L) {
this->write_reg_(0x17);
@ -716,6 +724,92 @@ FrequencyDeviation KT0803Component::get_frequency_deviation() {
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; }
void KT0803Component::set_alc_hold_time(AlcHoldTime value) {
if (value >= AlcHoldTime::LAST) {
ESP_LOGE(TAG, "set_alc_hold(%d) invalid", (int) value);
return;
}
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;
}
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); }
@ -806,6 +900,24 @@ 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) {
if (s != nullptr) {
if (!s->has_state() || s->state != state) {

View file

@ -66,6 +66,12 @@ class KT0803Component : public PollingComponent, public i2c::I2CDevice {
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();
@ -96,6 +102,12 @@ class KT0803Component : public PollingComponent, public i2c::I2CDevice {
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);
@ -118,7 +130,7 @@ class KT0803Component : public PollingComponent, public i2c::I2CDevice {
ChipId get_chip_id();
std::string get_chip_string() const;
void set_frequency(float value); // MHz
void set_frequency(float value);
float get_frequency();
void set_pga(float value);
float get_pga();
@ -172,6 +184,18 @@ class KT0803Component : public PollingComponent, public i2c::I2CDevice {
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();
};
} // namespace kt0803

View file

@ -24,6 +24,10 @@ from .. import (
CONF_SILENCE_LOW_COUNTER,
CONF_XTAL_SEL,
CONF_FREQUENCY_DEVIATION,
CONF_REF_CLK,
CONF_ALC_HIGH,
CONF_ALC_HOLD_TIME,
CONF_ALC_LOW,
ICON_SLEEP,
ICON_SINE_WAVE,
ICON_SPEAKER,
@ -41,6 +45,10 @@ from .. import (
SILENCE_LOW_LEVEL_COUNTER,
XTAL_SEL,
FREQUENCY_DEVIATION,
REFERENCE_CLOCK,
ALC_HIGH,
ALC_HOLD_TIME,
ALC_LOW,
)
PreEmphasisSelect = kt0803_ns.class_("PreEmphasisSelect", select.Select)
@ -57,6 +65,10 @@ SilenceHighCounterSelect = kt0803_ns.class_("SilenceHighCounterSelect", select.S
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)
CONFIG_SCHEMA = cv.Schema(
{
@ -79,12 +91,12 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional(CONF_ALC_ATTACK_TIME): select.select_schema(
AlcAttackTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SPEAKER,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_ALC_DECAY_TIME): select.select_schema(
AlcDecayTimeSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SPEAKER,
icon=ICON_SINE_WAVE,
),
cv.Optional(CONF_AUDIO_LIMITER_LEVEL): select.select_schema(
AudioLimiterLevelSelect,
@ -131,6 +143,26 @@ CONFIG_SCHEMA = cv.Schema(
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,
),
}
)
@ -158,3 +190,7 @@ async def to_code(config):
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)

View file

@ -0,0 +1,14 @@
#include "alc_high_select.h"
namespace esphome {
namespace kt0803 {
void AlcHighSelect::control(const std::string &value) {
this->publish_state(value);
if (auto index = this->active_index()) {
this->parent_->set_alc_high((AlcHigh) *index);
}
}
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../kt0803.h"
namespace esphome {
namespace kt0803 {
class AlcHighSelect : public select::Select, public Parented<KT0803Component> {
public:
AlcHighSelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,14 @@
#include "alc_hold_time_select.h"
namespace esphome {
namespace kt0803 {
void AlcHoldTimeSelect::control(const std::string &value) {
this->publish_state(value);
if (auto index = this->active_index()) {
this->parent_->set_alc_hold_time((AlcHoldTime) *index);
}
}
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../kt0803.h"
namespace esphome {
namespace kt0803 {
class AlcHoldTimeSelect : public select::Select, public Parented<KT0803Component> {
public:
AlcHoldTimeSelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,14 @@
#include "alc_low_select.h"
namespace esphome {
namespace kt0803 {
void AlcLowSelect::control(const std::string &value) {
this->publish_state(value);
if (auto index = this->active_index()) {
this->parent_->set_alc_low((AlcLow) *index);
}
}
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../kt0803.h"
namespace esphome {
namespace kt0803 {
class AlcLowSelect : public select::Select, public Parented<KT0803Component> {
public:
AlcLowSelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,14 @@
#include "ref_clk_select.h"
namespace esphome {
namespace kt0803 {
void RefClkSelect::control(const std::string &value) {
this->publish_state(value);
if (auto index = this->active_index()) {
this->parent_->set_ref_clk((ReferenceClock) *index);
}
}
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../kt0803.h"
namespace esphome {
namespace kt0803 {
class RefClkSelect : public select::Select, public Parented<KT0803Component> {
public:
RefClkSelect() = default;
protected:
void control(const std::string &value) override;
};
} // namespace kt0803
} // namespace esphome

View file

@ -4,7 +4,7 @@ import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_SWITCH,
ENTITY_CATEGORY_CONFIG,
ICON_SECURITY,
ICON_PULSE,
)
from .. import (
CONF_KT0803_ID,
@ -19,12 +19,12 @@ from .. import (
CONF_PA_BIAS,
CONF_SILENCE_DETECTION,
CONF_AU_ENHANCE,
CONF_XTAL_ENABLE,
CONF_REF_CLK_ENABLE,
ICON_VOLUME_MUTE,
ICON_EAR_HEARING,
ICON_SINE_WAVE,
ICON_SLEEP,
ICON_RADIO_TOWER,
ICON_FORMAT_TEXT,
)
MuteSwitch = kt0803_ns.class_("MuteSwitch", switch.Switch)
@ -36,6 +36,9 @@ 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)
CONFIG_SCHEMA = cv.Schema(
{
@ -94,6 +97,18 @@ CONFIG_SCHEMA = cv.Schema(
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,
),
}
)
@ -116,3 +131,5 @@ async def to_code(config):
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)

View file

@ -0,0 +1,12 @@
#include "ref_clk_enable_switch.h"
namespace esphome {
namespace kt0803 {
void RefClkEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_ref_clk_enable(value);
}
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/switch/switch.h"
#include "../kt0803.h"
namespace esphome {
namespace kt0803 {
class RefClkEnableSwitch : public switch_::Switch, public Parented<KT0803Component> {
public:
RefClkEnableSwitch() = default;
protected:
void write_state(bool value) override;
};
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "xtal_enable_switch.h"
namespace esphome {
namespace kt0803 {
void XtalEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_xtal_enable(value);
}
} // namespace kt0803
} // namespace esphome

View file

@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/switch/switch.h"
#include "../kt0803.h"
namespace esphome {
namespace kt0803 {
class XtalEnableSwitch : public switch_::Switch, public Parented<KT0803Component> {
public:
XtalEnableSwitch() = default;
protected:
void write_state(bool value) override;
};
} // namespace kt0803
} // namespace esphome