From e9765e2d259f81b15ef12e85f815cf0561b03b27 Mon Sep 17 00:00:00 2001 From: Kevin Ahrendt Date: Thu, 19 Sep 2024 11:17:01 -0400 Subject: [PATCH] yaml configuration options for pipeline stage --- esphome/components/voice_kit/__init__.py | 21 ++++++++++++++++++++ esphome/components/voice_kit/voice_kit.cpp | 23 +++++++++++++++------- esphome/components/voice_kit/voice_kit.h | 9 ++++++++- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/esphome/components/voice_kit/__init__.py b/esphome/components/voice_kit/__init__.py index 0099e24..14f6825 100644 --- a/esphome/components/voice_kit/__init__.py +++ b/esphome/components/voice_kit/__init__.py @@ -31,6 +31,18 @@ VoiceKit = voice_kit_ns.class_("VoiceKit", cg.Component, i2c.I2CDevice) VoiceKitFlashAction = voice_kit_ns.class_("VoiceKitFlashAction", automation.Action) +PipelineStages = voice_kit_ns.enum("PipelineStages") +PIPELINE_STAGES = { + "NONE": PipelineStages.PIPELINE_STAGE_NONE, + "AEC": PipelineStages.PIPELINE_STAGE_AEC, + "IC": PipelineStages.PIPELINE_STAGE_IC, + "NS": PipelineStages.PIPELINE_STAGE_NS, + "AGC": PipelineStages.PIPELINE_STAGE_AGC, +} + +CONF_CHANNEL_0_STAGE = "channel_0_stage" +CONF_CHANNEL_1_STAGE = "channel_1_stage" + DFUEndTrigger = voice_kit_ns.class_("DFUEndTrigger", automation.Trigger.template()) DFUErrorTrigger = voice_kit_ns.class_("DFUErrorTrigger", automation.Trigger.template()) DFUProgressTrigger = voice_kit_ns.class_( @@ -71,6 +83,12 @@ def download_firmware(config): cv.GenerateID(): cv.declare_id(VoiceKit), cv.Required(CONF_RESET_PIN): pins.gpio_output_pin_schema, cv.GenerateID(CONF_RAW_DATA_ID): cv.declare_id(cg.uint8), + cv.Optional(CONF_CHANNEL_0_STAGE, default="AGC"): cv.enum( + PIPELINE_STAGES, upper=True + ), + cv.Optional(CONF_CHANNEL_1_STAGE, default="NS"): cv.enum( + PIPELINE_STAGES, upper=True + ), cv.Optional(CONF_FIRMWARE): cv.All( { cv.Required(CONF_URL): cv.url, @@ -141,6 +159,9 @@ async def to_code(config): pin = await cg.gpio_pin_expression(config[CONF_RESET_PIN]) cg.add(var.set_reset_pin(pin)) + cg.add(var.set_channel_0_stage(config[CONF_CHANNEL_0_STAGE])) + cg.add(var.set_channel_1_stage(config[CONF_CHANNEL_1_STAGE])) + if config_fw := config.get(CONF_FIRMWARE): firmware_version = config_fw[CONF_VERSION].split(".") path = _compute_local_file_path(config_fw[CONF_URL]) diff --git a/esphome/components/voice_kit/voice_kit.cpp b/esphome/components/voice_kit/voice_kit.cpp index 32e10e7..08add5a 100644 --- a/esphome/components/voice_kit/voice_kit.cpp +++ b/esphome/components/voice_kit/voice_kit.cpp @@ -30,6 +30,8 @@ void VoiceKit::setup() { this->firmware_bin_version_minor_, this->firmware_bin_version_patch_, this->firmware_version_major_, this->firmware_version_minor_, this->firmware_version_patch_); this->start_dfu_update(); + } else { + this->write_pipeline_stages(); } }); } @@ -108,17 +110,23 @@ PipelineStages VoiceKit::read_pipeline_stage(MicrophoneChannels channel) { return static_cast(stage_resp[1]); } -void VoiceKit::write_pipeline_stage(MicrophoneChannels channel, PipelineStages stage) { - uint8_t channel_register = CONFIGURATION_SERVICER_RESID_CHANNEL_0_PIPELINE_STAGE; - if (channel == MICROPHONE_CHANNEL_1) { - channel_register = CONFIGURATION_SERVICER_RESID_CHANNEL_1_PIPELINE_STAGE; +void VoiceKit::write_pipeline_stages() { + // Write channel 0 stage + uint8_t stage_set[] = {CONFIGURATION_SERVICER_RESID, CONFIGURATION_SERVICER_RESID_CHANNEL_0_PIPELINE_STAGE, 1, + this->channel_0_stage_}; + + auto error_code = this->write(stage_set, sizeof(stage_set)); + if (error_code != i2c::ERROR_OK) { + ESP_LOGE(TAG, "Failed to write chanenl 0 stage"); } - const uint8_t stage_set[] = {CONFIGURATION_SERVICER_RESID, channel_register, 1, stage}; + // Write channel 1 stage + stage_set[1] = CONFIGURATION_SERVICER_RESID_CHANNEL_1_PIPELINE_STAGE; + stage_set[3] = this->channel_1_stage_; - auto error_code = this->write(stage_set, sizeof(stage_set)); + error_code = this->write(stage_set, sizeof(stage_set)); if (error_code != i2c::ERROR_OK) { - ESP_LOGE(TAG, "Failed to set stage"); + ESP_LOGE(TAG, "Failed to write channel 1 stage"); } } @@ -232,6 +240,7 @@ VoiceKitUpdaterStatus VoiceKit::dfu_update_send_block_() { #ifdef USE_VOICE_KIT_STATE_CALLBACK this->state_callback_.call(DFU_COMPLETE, 100.0f, UPDATE_OK); #endif + this->write_pipeline_stages(); return UPDATE_OK; default: diff --git a/esphome/components/voice_kit/voice_kit.h b/esphome/components/voice_kit/voice_kit.h index 5cd5e21..44bb69e 100644 --- a/esphome/components/voice_kit/voice_kit.h +++ b/esphome/components/voice_kit/voice_kit.h @@ -149,8 +149,12 @@ class VoiceKit : public Component, public i2c::I2CDevice { void start_dfu_update(); + void set_channel_0_stage(PipelineStages channel_0_stage) { this->channel_0_stage_ = channel_0_stage; } + void set_channel_1_stage(PipelineStages channel_1_stage) { this->channel_1_stage_ = channel_1_stage; } + + void write_pipeline_stages(); uint8_t read_vnr(); - void write_pipeline_stage(MicrophoneChannels channel, PipelineStages stage); + PipelineStages read_pipeline_stage(MicrophoneChannels channel); protected: @@ -169,6 +173,9 @@ class VoiceKit : public Component, public i2c::I2CDevice { bool dfu_set_alternate_(); bool dfu_check_if_ready_(); + PipelineStages channel_0_stage_; + PipelineStages channel_1_stage_; + GPIOPin *reset_pin_; uint8_t dfu_state_{0};