feat(voice): migrate to push2talk and serialize websocket text sends

This commit is contained in:
admin
2026-03-01 01:21:18 +08:00
parent 9deeab738e
commit dd108e2658
19 changed files with 891 additions and 224 deletions

View File

@@ -7,6 +7,7 @@ idf_component_register(
"src/display_lcd_module.c"
"src/display_lcd_decode_image.c"
"src/display_st7789p3.c"
"src/platform_mic_key.c"
"src/voice_audio.c"
"src/runtime_policy.c"
"src/runtime_diagnostics.c"
@@ -26,6 +27,7 @@ idf_component_register(
esp_driver_spi
lvgl
espressif__libpng
espressif__button
esp_codec_dev
esp_audio_codec
driver

View File

@@ -95,6 +95,18 @@ esp_err_t platform_display_show_png(const uint8_t *png,
void platform_display_mark_shared_spi_dirty(void);
void platform_display_deinit(void);
// ---------- mic_key ----------
typedef enum {
PLATFORM_MIC_KEY_EVENT_PRESS_DOWN = 0,
PLATFORM_MIC_KEY_EVENT_PRESS_UP,
} platform_mic_key_event_t;
typedef void (*platform_mic_key_event_cb_t)(platform_mic_key_event_t event, void *user_data);
esp_err_t platform_mic_key_init(void);
void platform_mic_key_deinit(void);
esp_err_t platform_mic_key_set_event_callback(platform_mic_key_event_cb_t cb, void *user_data);
// ---------- voice_audio ----------
typedef struct {
uint32_t sample_rate;

View File

@@ -0,0 +1,126 @@
#include "platform.h"
#include <stddef.h>
#include "button_gpio.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "iot_button.h"
#include "sdkconfig.h"
static const char *TAG = "platform_mic_key";
static const uint16_t MIC_KEY_SHORT_PRESS_MS = 40;
static const uint16_t MIC_KEY_HOLD_START_MS = 80;
static button_handle_t s_mic_key_button;
static platform_mic_key_event_cb_t s_event_cb;
static void *s_event_cb_user_data;
static portMUX_TYPE s_event_cb_lock = portMUX_INITIALIZER_UNLOCKED;
static void mic_key_dispatch_event(platform_mic_key_event_t event) {
platform_mic_key_event_cb_t cb = NULL;
void *user_data = NULL;
portENTER_CRITICAL(&s_event_cb_lock);
cb = s_event_cb;
user_data = s_event_cb_user_data;
portEXIT_CRITICAL(&s_event_cb_lock);
if (cb != NULL) {
cb(event, user_data);
}
}
static void mic_key_hold_start_cb(void *button_handle, void *usr_data) {
(void)button_handle;
(void)usr_data;
mic_key_dispatch_event(PLATFORM_MIC_KEY_EVENT_PRESS_DOWN);
}
static void mic_key_press_up_cb(void *button_handle, void *usr_data) {
(void)button_handle;
(void)usr_data;
mic_key_dispatch_event(PLATFORM_MIC_KEY_EVENT_PRESS_UP);
}
esp_err_t platform_mic_key_init(void) {
if (CONFIG_TQ_MIC_KEY_PIN < 0) {
ESP_LOGW(TAG, "MIC_KEY disabled by config: pin=%d", CONFIG_TQ_MIC_KEY_PIN);
return ESP_ERR_NOT_SUPPORTED;
}
if (s_mic_key_button != NULL) {
return ESP_OK;
}
button_config_t button_cfg = {
.long_press_time = MIC_KEY_HOLD_START_MS,
.short_press_time = MIC_KEY_SHORT_PRESS_MS,
};
button_gpio_config_t gpio_cfg = {
.gpio_num = CONFIG_TQ_MIC_KEY_PIN,
.active_level = 0,
.enable_power_save = false,
.disable_pull = false,
};
esp_err_t err = iot_button_new_gpio_device(&button_cfg, &gpio_cfg, &s_mic_key_button);
if (err != ESP_OK) {
ESP_LOGE(TAG,
"iot_button_new_gpio_device failed: pin=%d err=%s",
CONFIG_TQ_MIC_KEY_PIN,
esp_err_to_name(err));
return err;
}
err = iot_button_register_cb(s_mic_key_button,
BUTTON_LONG_PRESS_START,
NULL,
mic_key_hold_start_cb,
NULL);
if (err != ESP_OK) {
ESP_LOGE(TAG, "register BUTTON_LONG_PRESS_START failed: %s", esp_err_to_name(err));
(void)iot_button_delete(s_mic_key_button);
s_mic_key_button = NULL;
return err;
}
err = iot_button_register_cb(s_mic_key_button, BUTTON_PRESS_UP, NULL, mic_key_press_up_cb, NULL);
if (err != ESP_OK) {
ESP_LOGE(TAG, "register BUTTON_PRESS_UP failed: %s", esp_err_to_name(err));
(void)iot_button_delete(s_mic_key_button);
s_mic_key_button = NULL;
return err;
}
ESP_LOGI(TAG,
"MIC_KEY initialized: gpio=%d active_level=low short_press_ms=%u hold_start_ms=%u",
CONFIG_TQ_MIC_KEY_PIN,
(unsigned)MIC_KEY_SHORT_PRESS_MS,
(unsigned)MIC_KEY_HOLD_START_MS);
return ESP_OK;
}
void platform_mic_key_deinit(void) {
if (s_mic_key_button != NULL) {
(void)iot_button_delete(s_mic_key_button);
s_mic_key_button = NULL;
}
portENTER_CRITICAL(&s_event_cb_lock);
s_event_cb = NULL;
s_event_cb_user_data = NULL;
portEXIT_CRITICAL(&s_event_cb_lock);
}
esp_err_t platform_mic_key_set_event_callback(platform_mic_key_event_cb_t cb, void *user_data) {
if (s_mic_key_button == NULL) {
return ESP_ERR_INVALID_STATE;
}
portENTER_CRITICAL(&s_event_cb_lock);
s_event_cb = cb;
s_event_cb_user_data = user_data;
portEXIT_CRITICAL(&s_event_cb_lock);
return ESP_OK;
}