Skip to content

Commit

Permalink
yaml configuration options for pipeline stage
Browse files Browse the repository at this point in the history
  • Loading branch information
kahrendt committed Sep 20, 2024
1 parent 637018c commit 71a3d39
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
21 changes: 21 additions & 0 deletions esphome/components/voice_kit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand Down
23 changes: 16 additions & 7 deletions esphome/components/voice_kit/voice_kit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}
Expand Down Expand Up @@ -108,17 +110,23 @@ PipelineStages VoiceKit::read_pipeline_stage(MicrophoneChannels channel) {
return static_cast<PipelineStages>(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");
}
}

Expand Down Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion esphome/components/voice_kit/voice_kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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};
Expand Down

0 comments on commit 71a3d39

Please sign in to comment.