将项目版本更新至1.5.9,优化内存使用,增加主任务栈大小至8192,优化固件升级流程,重构主循环为MainEventLoop,添加新版本检查功能,更新音频编解码器的DMA配置常量。

This commit is contained in:
Terrence
2025-04-13 11:47:15 +08:00
parent 4ef6ef3552
commit 78d7b40914
20 changed files with 125 additions and 113 deletions

View File

@ -4,7 +4,7 @@
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
set(PROJECT_VER "1.5.8")
set(PROJECT_VER "1.5.9")
# Add this line to disable the specific warning
add_compile_options(-Wno-missing-field-initializers)

View File

@ -48,6 +48,7 @@ Application::Application() {
.skip_unhandled_events = true
};
esp_timer_create(&clock_timer_args, &clock_timer_handle_);
esp_timer_start_periodic(clock_timer_handle_, 1000000);
}
Application::~Application() {
@ -81,13 +82,9 @@ void Application::CheckNewVersion() {
if (ota_.HasNewVersion()) {
Alert(Lang::Strings::OTA_UPGRADE, Lang::Strings::UPGRADING, "happy", Lang::Sounds::P3_UPGRADE);
// Wait for the chat state to be idle
do {
vTaskDelay(pdMS_TO_TICKS(3000));
} while (GetDeviceState() != kDeviceStateIdle);
// Use main task to do the upgrade, not cancelable
Schedule([this, display]() {
vTaskDelay(pdMS_TO_TICKS(3000));
SetDeviceState(kDeviceStateUpgrading);
display->SetIcon(FONT_AWESOME_DOWNLOAD);
@ -123,16 +120,11 @@ void Application::CheckNewVersion() {
ESP_LOGI(TAG, "Firmware upgrade failed...");
vTaskDelay(pdMS_TO_TICKS(3000));
Reboot();
});
return;
}
// No new version, mark the current version as valid
ota_.MarkCurrentVersionValid();
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
display->ShowNotification(message.c_str());
if (ota_.HasActivationCode()) {
// Activation code is valid
SetDeviceState(kDeviceStateActivating);
@ -148,11 +140,8 @@ void Application::CheckNewVersion() {
continue;
}
SetDeviceState(kDeviceStateIdle);
display->SetChatMessage("system", "");
ResetDecoder();
PlaySound(Lang::Sounds::P3_SUCCESS);
// Exit the loop if upgrade or idle
xEventGroupSetBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT);
// Exit the loop if done checking new version
break;
}
}
@ -180,8 +169,6 @@ void Application::ShowActivationCode() {
// This sentence uses 9KB of SRAM, so we need to wait for it to finish
Alert(Lang::Strings::ACTIVATION, message.c_str(), "happy", Lang::Sounds::P3_ACTIVATION);
vTaskDelay(pdMS_TO_TICKS(1000));
background_task_->WaitForCompletion();
for (const auto& digit : code) {
auto it = std::find_if(digit_sounds.begin(), digit_sounds.end(),
@ -214,6 +201,15 @@ void Application::DismissAlert() {
}
void Application::PlaySound(const std::string_view& sound) {
// Wait for the previous sound to finish
{
std::unique_lock<std::mutex> lock(mutex_);
audio_decode_cv_.wait(lock, [this]() {
return audio_decode_queue_.empty();
});
}
background_task_->WaitForCompletion();
// The assets are encoded at 16000Hz, 60ms frame duration
SetDecodeSampleRate(16000, 60);
const char* data = sound.data();
@ -295,6 +291,16 @@ void Application::StartListening() {
}
void Application::StopListening() {
const std::array<int, 3> valid_states = {
kDeviceStateListening,
kDeviceStateSpeaking,
kDeviceStateIdle,
};
// If not valid, do nothing
if (std::find(valid_states.begin(), valid_states.end(), device_state_) == valid_states.end()) {
return;
}
Schedule([this]() {
if (device_state_ == kDeviceStateListening) {
protocol_->SendStopListening();
@ -337,16 +343,13 @@ void Application::Start() {
vTaskDelete(NULL);
}, "audio_loop", 4096 * 2, this, 8, &audio_loop_task_handle_, realtime_chat_enabled_ ? 1 : 0);
/* Start the main loop */
xTaskCreatePinnedToCore([](void* arg) {
Application* app = (Application*)arg;
app->MainLoop();
vTaskDelete(NULL);
}, "main_loop", 4096 * 2, this, 4, &main_loop_task_handle_, 0);
/* Wait for the network to be ready */
board.StartNetwork();
// Check for new firmware version or get the MQTT broker address
display->SetStatus(Lang::Strings::CHECKING_NEW_VERSION);
CheckNewVersion();
// Initialize the protocol
display->SetStatus(Lang::Strings::LOADING_PROTOCOL);
#ifdef CONFIG_CONNECTION_TYPE_WEBSOCKET
@ -444,13 +447,6 @@ void Application::Start() {
});
protocol_->Start();
// Check for new firmware version or get the MQTT broker address
xTaskCreate([](void* arg) {
Application* app = (Application*)arg;
app->CheckNewVersion();
vTaskDelete(NULL);
}, "check_new_version", 4096 * 2, this, 2, nullptr);
#if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Initialize(codec, realtime_chat_enabled_);
audio_processor_.OnOutput([this](std::vector<int16_t>&& data) {
@ -509,15 +505,18 @@ void Application::Start() {
wake_word_detect_.StartDetection();
#endif
// Wait for the new version check to finish
xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
SetDeviceState(kDeviceStateIdle);
esp_timer_start_periodic(clock_timer_handle_, 1000000);
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
display->ShowNotification(message.c_str());
display->SetChatMessage("system", "");
// Play the success sound to indicate the device is ready
ResetDecoder();
PlaySound(Lang::Sounds::P3_SUCCESS);
#if 0
while (true) {
SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(10000));
}
#endif
// Enter the main event loop
MainEventLoop();
}
void Application::OnClockTimer() {
@ -553,10 +552,10 @@ void Application::Schedule(std::function<void()> callback) {
xEventGroupSetBits(event_group_, SCHEDULE_EVENT);
}
// The Main Loop controls the chat state and websocket connection
// The Main Event Loop controls the chat state and websocket connection
// If other tasks need to access the websocket or chat state,
// they should use Schedule to call this function
void Application::MainLoop() {
void Application::MainEventLoop() {
while (true) {
auto bits = xEventGroupWaitBits(event_group_, SCHEDULE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
@ -601,12 +600,14 @@ void Application::OnAudioOutput() {
if (device_state_ == kDeviceStateListening) {
audio_decode_queue_.clear();
audio_decode_cv_.notify_all();
return;
}
auto opus = std::move(audio_decode_queue_.front());
audio_decode_queue_.pop_front();
lock.unlock();
audio_decode_cv_.notify_all();
background_task_->Schedule([this, codec, opus = std::move(opus)]() mutable {
if (aborted_) {
@ -630,10 +631,9 @@ void Application::OnAudioOutput() {
}
void Application::OnAudioInput() {
std::vector<int16_t> data;
#if CONFIG_USE_WAKE_WORD_DETECT
if (wake_word_detect_.IsDetectionRunning()) {
std::vector<int16_t> data;
ReadAudio(data, 16000, wake_word_detect_.GetFeedSize());
wake_word_detect_.Feed(data);
return;
@ -641,12 +641,14 @@ void Application::OnAudioInput() {
#endif
#if CONFIG_USE_AUDIO_PROCESSOR
if (audio_processor_.IsRunning()) {
std::vector<int16_t> data;
ReadAudio(data, 16000, audio_processor_.GetFeedSize());
audio_processor_.Feed(data);
return;
}
#else
if (device_state_ == kDeviceStateListening) {
std::vector<int16_t> data;
ReadAudio(data, 16000, 30 * 16000 / 1000);
background_task_->Schedule([this, data = std::move(data)]() mutable {
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
@ -792,6 +794,7 @@ void Application::ResetDecoder() {
std::lock_guard<std::mutex> lock(mutex_);
opus_decoder_->ResetState();
audio_decode_queue_.clear();
audio_decode_cv_.notify_all();
last_output_time_ = std::chrono::steady_clock::now();
auto codec = Board::GetInstance().GetAudioCodec();

View File

@ -9,6 +9,8 @@
#include <string>
#include <mutex>
#include <list>
#include <vector>
#include <condition_variable>
#include <opus_encoder.h>
#include <opus_decoder.h>
@ -28,6 +30,7 @@
#define SCHEDULE_EVENT (1 << 0)
#define AUDIO_INPUT_READY_EVENT (1 << 1)
#define AUDIO_OUTPUT_READY_EVENT (1 << 2)
#define CHECK_NEW_VERSION_DONE_EVENT (1 << 3)
enum DeviceState {
kDeviceStateUnknown,
@ -97,7 +100,6 @@ private:
bool aborted_ = false;
bool voice_detected_ = false;
int clock_ticks_ = 0;
TaskHandle_t main_loop_task_handle_ = nullptr;
TaskHandle_t check_new_version_task_handle_ = nullptr;
// Audio encode / decode
@ -105,6 +107,7 @@ private:
BackgroundTask* background_task_ = nullptr;
std::chrono::steady_clock::time_point last_output_time_;
std::list<std::vector<uint8_t>> audio_decode_queue_;
std::condition_variable audio_decode_cv_;
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
@ -113,7 +116,7 @@ private:
OpusResampler reference_resampler_;
OpusResampler output_resampler_;
void MainLoop();
void MainEventLoop();
void OnAudioInput();
void OnAudioOutput();
void ReadAudio(std::vector<int16_t>& data, int sample_rate, int samples);

View File

@ -7,7 +7,8 @@
"INFO": "Information",
"ERROR": "Error",
"VERSION": "Ver ",
"LOADING_PROTOCOL": "Loading Protocol...",
"LOADING_PROTOCOL": "Logging in...",
"CHECKING_NEW_VERSION": "Checking for new version...",
"INITIALIZING": "Initializing...",
"PIN_ERROR": "Please insert SIM card",
"REG_ERROR": "Unable to access network, please check SIM card status",

View File

@ -7,12 +7,13 @@
"INFO": "情報",
"ERROR": "エラー",
"VERSION": "バージョン ",
"LOADING_PROTOCOL": "プロトコルを読み込み中...",
"LOADING_PROTOCOL": "サーバーにログイン中...",
"INITIALIZING": "初期化中...",
"PIN_ERROR": "SIMカードを挿入してください",
"REG_ERROR": "ネットワークに接続できません。ネットワーク状態を確認してください",
"DETECTING_MODULE": "モジュールを検出中...",
"REGISTERING_NETWORK": "ネットワーク接続待機中...",
"CHECKING_NEW_VERSION": "新しいバージョンを確認中...",
"STANDBY": "待機中",
"CONNECT_TO": "接続先 ",

View File

@ -7,12 +7,13 @@
"INFO":"信息",
"ERROR":"错误",
"VERSION": "版本 ",
"LOADING_PROTOCOL":"加载协议...",
"LOADING_PROTOCOL":"登录服务器...",
"INITIALIZING":"正在初始化...",
"PIN_ERROR":"请插入 SIM 卡",
"REG_ERROR":"无法接入网络,请检查流量卡状态",
"DETECTING_MODULE":"检测模组...",
"REGISTERING_NETWORK":"等待网络...",
"CHECKING_NEW_VERSION":"检查新版本...",
"STANDBY":"待命",
"CONNECT_TO":"连接 ",

View File

@ -7,12 +7,13 @@
"INFO": "資訊",
"ERROR": "錯誤",
"VERSION": "版本 ",
"LOADING_PROTOCOL": "加載協議...",
"LOADING_PROTOCOL": "登入伺服器...",
"INITIALIZING": "正在初始化...",
"PIN_ERROR": "請插入 SIM 卡",
"REG_ERROR": "無法接入網絡,請檢查網路狀態",
"DETECTING_MODULE": "檢測模組...",
"REGISTERING_NETWORK": "等待網絡...",
"CHECKING_NEW_VERSION": "檢查新版本...",
"STANDBY": "待命",
"CONNECT_TO": "連接 ",

View File

@ -11,6 +11,9 @@
#include "board.h"
#define AUDIO_CODEC_DMA_DESC_NUM 6
#define AUDIO_CODEC_DMA_FRAME_NUM 240
class AudioCodec {
public:
AudioCodec();

View File

@ -96,8 +96,8 @@ void BoxAudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -80,8 +80,8 @@ void Es8311AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -85,8 +85,8 @@ void Es8388AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -23,8 +23,8 @@ NoAudioCodecDuplex::NoAudioCodecDuplex(int input_sample_rate, int output_sample_
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,
@ -82,8 +82,8 @@ ATK_NoAudioCodecDuplex::ATK_NoAudioCodecDuplex(int input_sample_rate, int output
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,
@ -141,8 +141,8 @@ NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sampl
i2s_chan_config_t chan_cfg = {
.id = (i2s_port_t)0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,
@ -210,8 +210,8 @@ NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sampl
i2s_chan_config_t chan_cfg = {
.id = (i2s_port_t)0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,
@ -278,8 +278,8 @@ NoAudioCodecSimplexPdm::NoAudioCodecSimplexPdm(int input_sample_rate, int output
// Create a new channel for speaker
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)1, I2S_ROLE_MASTER);
tx_chan_cfg.dma_desc_num = 6;
tx_chan_cfg.dma_frame_num = 240;
tx_chan_cfg.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM;
tx_chan_cfg.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM;
tx_chan_cfg.auto_clear_after_cb = true;
tx_chan_cfg.auto_clear_before_cb = false;
tx_chan_cfg.intr_priority = 0;

View File

@ -74,8 +74,8 @@ void K10AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -97,8 +97,8 @@ void BoxAudioCodecLite::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, g
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -99,8 +99,8 @@ void CoreS3AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -98,8 +98,8 @@ void SensecapAudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk,
i2s_chan_config_t chan_cfg = {
.id = I2S_NUM_0,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 6,
.dma_frame_num = 240,
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
.auto_clear_after_cb = true,
.auto_clear_before_cb = false,
.intr_priority = 0,

View File

@ -8,8 +8,8 @@ dependencies:
espressif/esp_io_expander_tca9554: "==2.0.0"
espressif/esp_lcd_panel_io_additions: "^1.0.1"
78/esp_lcd_nv3023: "~1.0.0"
78/esp-wifi-connect: "~2.3.1"
78/esp-opus-encoder: "~2.3.0"
78/esp-wifi-connect: "~2.3.2"
78/esp-opus-encoder: "~2.3.1"
78/esp-ml307: "~1.8.1"
78/xiaozhi-fonts: "~1.3.2"
espressif/led_strip: "^2.4.1"

View File

@ -26,5 +26,4 @@ extern "C" void app_main(void)
// Launch the application
Application::GetInstance().Start();
// The main thread will exit and release the stack memory
}

View File

@ -58,15 +58,15 @@ bool Ota::CheckVersion() {
http->SetHeader("Accept-Language", Lang::CODE);
http->SetHeader("Content-Type", "application/json");
std::string post_data = board.GetJson();
std::string method = post_data.length() > 0 ? "POST" : "GET";
if (!http->Open(method, check_version_url_, post_data)) {
std::string data = board.GetJson();
std::string method = data.length() > 0 ? "POST" : "GET";
if (!http->Open(method, check_version_url_, data)) {
ESP_LOGE(TAG, "Failed to open HTTP connection");
delete http;
return false;
}
auto response = http->GetBody();
data = http->GetBody();
http->Close();
delete http;
@ -74,7 +74,7 @@ bool Ota::CheckVersion() {
// Parse the JSON response and check if the version is newer
// If it is, set has_new_version_ to true and store the new version and URL
cJSON *root = cJSON_Parse(response.c_str());
cJSON *root = cJSON_Parse(data.c_str());
if (root == NULL) {
ESP_LOGE(TAG, "Failed to parse JSON response");
return false;

View File

@ -17,7 +17,7 @@ CONFIG_ESP_TASK_WDT_TIMEOUT_S=10
CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y
CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=n
CONFIG_ESP_WIFI_IRAM_OPT=n