阶段1: 将 dzbj 项目的 LVGL 8.3.11 LCD 显示集成到 AI小智 主项目, 开机显示 ScreenHome 界面,同时优化配网模式下的内存使用, 确保 WiFi+BLE+LVGL 三者共存运行。 ## 新增功能 ### dzbj 显示模块集成 - 新增 main/dzbj/ 目录,移植 LCD 驱动(ST77916 QSPI)、触摸驱动(CST816S)、 LVGL 初始化和 SquareLine Studio UI 界面 - I2C 总线共享:dzbj 触摸控制器复用主项目的 I2C_NUM_1 总线 - GPIO 冲突解决:LED(GPIO21)、Touch1(GPIO1)、Touch4(GPIO7) 改为 NC, 电池 ADC 从 GPIO6 改为 GPIO3 - 添加 LVGL、esp_lcd_st77916、esp_lcd_touch_cst816s 等组件依赖 - managed_components 纳入版本管理 ### 配网模式轻量化启动 - BoxAudioCodec: 新增 output_only 模式,仅创建 I2S TX 通道(省 ~13KB DMA) 跳过 ES7210 ADC 初始化(省 ~2-4KB) - AudioCodec: 新增 StartOutputOnly() 方法,仅启用扬声器输出 - Application: 配网模式跳过 Opus 编码器、输入重采样器、协议初始化、 天气位置检测等网络业务 - 板级构造函数: 配网模式跳过电池检测、IMU传感器、PowerSaveTimer ### WifiBoard 配网流程修复 - NeedsProvisioning() 静态方法: 读取 NVS force_ap 和 SSID 列表, 用于提前判断配网模式 - force_ap 竞态修复: 构造函数不再清零 force_ap,改在 StartNetwork() 清零, 确保 NeedsProvisioning() 能正确读到 force_ap=1 - Application 缓存 provisioning_mode_ 成员变量,避免重复读 NVS ### BLE 配网重启修复 - 配网成功后用 esp_timer 延迟重启替代 xTaskCreate, 避免内存紧张时任务创建失败导致设备不重启 - 注释掉 WiFi 连接成功后的 MAC 地址发送步骤 ### sdkconfig 内存优化 - BT_ALLOCATION_FROM_SPIRAM_FIRST=y (BLE 动态分配优先 PSRAM) - SPIRAM_MALLOC_RESERVE_INTERNAL=32768 - NVS_ALLOCATE_CACHE_IN_SPIRAM=y - WiFi 静态缓冲区数量优化 (RX=10, TX=8) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
310 lines
10 KiB
C++
310 lines
10 KiB
C++
#include "box_audio_codec.h"
|
||
|
||
#include <esp_log.h>
|
||
#include <driver/i2c.h>
|
||
#include <driver/i2s_tdm.h>
|
||
|
||
static const char TAG[] = "BoxAudioCodec";
|
||
|
||
BoxAudioCodec::BoxAudioCodec(void* i2c_master_handle, int input_sample_rate, int output_sample_rate,
|
||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||
gpio_num_t pa_pin, uint8_t es8311_addr, uint8_t es7210_addr, bool input_reference,
|
||
bool output_only) {
|
||
output_only_ = output_only;
|
||
duplex_ = !output_only; // 配网模式不需要双工
|
||
input_reference_ = input_reference;
|
||
input_channels_ = input_reference_ ? 2 : 1;
|
||
input_sample_rate_ = input_sample_rate;
|
||
output_sample_rate_ = output_sample_rate;
|
||
|
||
if (output_only_) {
|
||
// 配网模式:仅创建 TX 通道,省 ~11.5KB RX DMA 内存
|
||
CreateTxOnlyChannel(mclk, bclk, ws, dout);
|
||
} else {
|
||
CreateDuplexChannels(mclk, bclk, ws, dout, din);
|
||
}
|
||
|
||
// 初始化 I2S 数据接口
|
||
audio_codec_i2s_cfg_t i2s_cfg = {
|
||
.port = I2S_NUM_0,
|
||
.rx_handle = rx_handle_, // 配网模式下为 nullptr
|
||
.tx_handle = tx_handle_,
|
||
};
|
||
data_if_ = audio_codec_new_i2s_data(&i2s_cfg);
|
||
assert(data_if_ != NULL);
|
||
|
||
// Output(ES8311 DAC)
|
||
audio_codec_i2c_cfg_t i2c_cfg = {
|
||
.port = (i2c_port_t)1,
|
||
.addr = es8311_addr,
|
||
.bus_handle = i2c_master_handle,
|
||
};
|
||
out_ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||
assert(out_ctrl_if_ != NULL);
|
||
|
||
gpio_if_ = audio_codec_new_gpio();
|
||
assert(gpio_if_ != NULL);
|
||
|
||
es8311_codec_cfg_t es8311_cfg = {};
|
||
es8311_cfg.ctrl_if = out_ctrl_if_;
|
||
es8311_cfg.gpio_if = gpio_if_;
|
||
es8311_cfg.codec_mode = ESP_CODEC_DEV_WORK_MODE_DAC;
|
||
es8311_cfg.pa_pin = pa_pin;
|
||
es8311_cfg.use_mclk = true;
|
||
es8311_cfg.hw_gain.pa_voltage = 5.0;
|
||
es8311_cfg.hw_gain.codec_dac_voltage = 3.3;
|
||
out_codec_if_ = es8311_codec_new(&es8311_cfg);
|
||
assert(out_codec_if_ != NULL);
|
||
|
||
esp_codec_dev_cfg_t dev_cfg = {
|
||
.dev_type = ESP_CODEC_DEV_TYPE_OUT,
|
||
.codec_if = out_codec_if_,
|
||
.data_if = data_if_,
|
||
};
|
||
output_dev_ = esp_codec_dev_new(&dev_cfg);
|
||
assert(output_dev_ != NULL);
|
||
|
||
// Input(ES7210 ADC)— 配网模式跳过,省 ~2-4KB
|
||
if (!output_only_) {
|
||
i2c_cfg.addr = es7210_addr;
|
||
in_ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||
assert(in_ctrl_if_ != NULL);
|
||
|
||
es7210_codec_cfg_t es7210_cfg = {};
|
||
es7210_cfg.ctrl_if = in_ctrl_if_;
|
||
es7210_cfg.mic_selected = ES7120_SEL_MIC1 | ES7120_SEL_MIC2;
|
||
in_codec_if_ = es7210_codec_new(&es7210_cfg);
|
||
assert(in_codec_if_ != NULL);
|
||
|
||
dev_cfg.dev_type = ESP_CODEC_DEV_TYPE_IN;
|
||
dev_cfg.codec_if = in_codec_if_;
|
||
input_dev_ = esp_codec_dev_new(&dev_cfg);
|
||
assert(input_dev_ != NULL);
|
||
}
|
||
|
||
ESP_LOGI(TAG, "BoxAudioDevice initialized (%s)", output_only_ ? "output only" : "duplex");
|
||
}
|
||
|
||
BoxAudioCodec::~BoxAudioCodec() {
|
||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||
esp_codec_dev_delete(output_dev_);
|
||
|
||
if (input_dev_) {
|
||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||
esp_codec_dev_delete(input_dev_);
|
||
}
|
||
|
||
if (in_codec_if_) audio_codec_delete_codec_if(in_codec_if_);
|
||
if (in_ctrl_if_) audio_codec_delete_ctrl_if(in_ctrl_if_);
|
||
audio_codec_delete_codec_if(out_codec_if_);
|
||
audio_codec_delete_ctrl_if(out_ctrl_if_);
|
||
audio_codec_delete_gpio_if(gpio_if_);
|
||
audio_codec_delete_data_if(data_if_);
|
||
}
|
||
|
||
void BoxAudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
|
||
assert(input_sample_rate_ == output_sample_rate_);
|
||
|
||
i2s_chan_config_t chan_cfg = {
|
||
.id = I2S_NUM_0,
|
||
.role = I2S_ROLE_MASTER,
|
||
.dma_desc_num = 6,
|
||
.dma_frame_num = 240,
|
||
.auto_clear_after_cb = true,
|
||
.auto_clear_before_cb = false,
|
||
.intr_priority = 0,
|
||
};
|
||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
|
||
|
||
i2s_std_config_t std_cfg = {
|
||
.clk_cfg = {
|
||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||
.ext_clk_freq_hz = 0,
|
||
.mclk_multiple = I2S_MCLK_MULTIPLE_256
|
||
},
|
||
.slot_cfg = {
|
||
.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
|
||
.slot_mode = I2S_SLOT_MODE_STEREO,
|
||
.slot_mask = I2S_STD_SLOT_BOTH,
|
||
.ws_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||
.ws_pol = false,
|
||
.bit_shift = true,
|
||
.left_align = true,
|
||
.big_endian = false,
|
||
.bit_order_lsb = false
|
||
},
|
||
.gpio_cfg = {
|
||
.mclk = mclk,
|
||
.bclk = bclk,
|
||
.ws = ws,
|
||
.dout = dout,
|
||
.din = I2S_GPIO_UNUSED,
|
||
.invert_flags = {
|
||
.mclk_inv = false,
|
||
.bclk_inv = false,
|
||
.ws_inv = false
|
||
}
|
||
}
|
||
};
|
||
|
||
i2s_tdm_config_t tdm_cfg = {
|
||
.clk_cfg = {
|
||
.sample_rate_hz = (uint32_t)input_sample_rate_,
|
||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||
.ext_clk_freq_hz = 0,
|
||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||
.bclk_div = 8,
|
||
},
|
||
.slot_cfg = {
|
||
.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
|
||
.slot_mode = I2S_SLOT_MODE_STEREO,
|
||
.slot_mask = i2s_tdm_slot_mask_t(I2S_TDM_SLOT0 | I2S_TDM_SLOT1 | I2S_TDM_SLOT2 | I2S_TDM_SLOT3),
|
||
.ws_width = I2S_TDM_AUTO_WS_WIDTH,
|
||
.ws_pol = false,
|
||
.bit_shift = true,
|
||
.left_align = false,
|
||
.big_endian = false,
|
||
.bit_order_lsb = false,
|
||
.skip_mask = false,
|
||
.total_slot = I2S_TDM_AUTO_SLOT_NUM
|
||
},
|
||
.gpio_cfg = {
|
||
.mclk = mclk,
|
||
.bclk = bclk,
|
||
.ws = ws,
|
||
.dout = I2S_GPIO_UNUSED,
|
||
.din = din,
|
||
.invert_flags = {
|
||
.mclk_inv = false,
|
||
.bclk_inv = false,
|
||
.ws_inv = false
|
||
}
|
||
}
|
||
};
|
||
|
||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||
ESP_ERROR_CHECK(i2s_channel_init_tdm_mode(rx_handle_, &tdm_cfg));
|
||
ESP_LOGI(TAG, "Duplex channels created");
|
||
}
|
||
|
||
void BoxAudioCodec::CreateTxOnlyChannel(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout) {
|
||
i2s_chan_config_t chan_cfg = {
|
||
.id = I2S_NUM_0,
|
||
.role = I2S_ROLE_MASTER,
|
||
.dma_desc_num = 6,
|
||
.dma_frame_num = 240,
|
||
.auto_clear_after_cb = true,
|
||
.auto_clear_before_cb = false,
|
||
.intr_priority = 0,
|
||
};
|
||
// 仅创建 TX 通道,rx_handle_ 保持 nullptr,省 ~11.5KB RX DMA 内存
|
||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, NULL));
|
||
|
||
i2s_std_config_t std_cfg = {
|
||
.clk_cfg = {
|
||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||
.ext_clk_freq_hz = 0,
|
||
.mclk_multiple = I2S_MCLK_MULTIPLE_256
|
||
},
|
||
.slot_cfg = {
|
||
.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
|
||
.slot_mode = I2S_SLOT_MODE_STEREO,
|
||
.slot_mask = I2S_STD_SLOT_BOTH,
|
||
.ws_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||
.ws_pol = false,
|
||
.bit_shift = true,
|
||
.left_align = true,
|
||
.big_endian = false,
|
||
.bit_order_lsb = false
|
||
},
|
||
.gpio_cfg = {
|
||
.mclk = mclk,
|
||
.bclk = bclk,
|
||
.ws = ws,
|
||
.dout = dout,
|
||
.din = I2S_GPIO_UNUSED,
|
||
.invert_flags = {
|
||
.mclk_inv = false,
|
||
.bclk_inv = false,
|
||
.ws_inv = false
|
||
}
|
||
}
|
||
};
|
||
|
||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||
ESP_LOGI(TAG, "TX-only channel created (provisioning mode)");
|
||
}
|
||
|
||
void BoxAudioCodec::SetOutputVolume(int volume) {
|
||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, volume));
|
||
AudioCodec::SetOutputVolume(volume);
|
||
}
|
||
|
||
void BoxAudioCodec::EnableInput(bool enable) {
|
||
if (enable == input_enabled_) {
|
||
return;
|
||
}
|
||
if (enable) {
|
||
esp_codec_dev_sample_info_t fs = {
|
||
.bits_per_sample = 16,
|
||
.channel = static_cast<uint8_t>(input_channels_),
|
||
.channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0),
|
||
// .sample_rate = (uint32_t)output_sample_rate_,
|
||
.sample_rate = (uint32_t)input_sample_rate_,
|
||
.mclk_multiple = 0,
|
||
};
|
||
if (input_reference_) {
|
||
fs.channel_mask |= ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1);
|
||
}
|
||
ESP_ERROR_CHECK(esp_codec_dev_open(input_dev_, &fs));
|
||
ESP_ERROR_CHECK(esp_codec_dev_set_in_channel_gain(input_dev_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), 27.0));
|
||
if (input_reference_) {
|
||
ESP_ERROR_CHECK(esp_codec_dev_set_in_channel_gain(input_dev_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1), 21.0));
|
||
}
|
||
ESP_LOGI(TAG, "Input opened: sr=%u ch=%u mask=0x%x ref=%d", (unsigned)fs.sample_rate, (unsigned)fs.channel, (unsigned)fs.channel_mask, (int)input_reference_);
|
||
} else {
|
||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||
}
|
||
AudioCodec::EnableInput(enable);
|
||
}
|
||
|
||
void BoxAudioCodec::EnableOutput(bool enable) {
|
||
if (enable == output_enabled_) {
|
||
return;
|
||
}
|
||
if (enable) {
|
||
// Play 16bit audio with configured channels
|
||
esp_codec_dev_sample_info_t fs = {
|
||
.bits_per_sample = 16,
|
||
.channel = static_cast<uint8_t>(output_channels_),
|
||
.channel_mask = 0,
|
||
.sample_rate = (uint32_t)output_sample_rate_,
|
||
.mclk_multiple = 0,
|
||
};
|
||
ESP_ERROR_CHECK(esp_codec_dev_open(output_dev_, &fs));
|
||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, output_volume_));
|
||
} else {
|
||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||
}
|
||
AudioCodec::EnableOutput(enable);
|
||
}
|
||
|
||
int BoxAudioCodec::Read(int16_t* dest, int samples) {
|
||
if (input_enabled_) {
|
||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_read(input_dev_, (void*)dest, samples * sizeof(int16_t)));
|
||
}
|
||
return samples;
|
||
}
|
||
|
||
int BoxAudioCodec::Write(const int16_t* data, int samples) {
|
||
if (output_enabled_) {
|
||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_write(output_dev_, (void*)data, samples * sizeof(int16_t)));
|
||
}
|
||
return samples;
|
||
}
|