Skip to content

Commit 83f6f8c

Browse files
78Xiaoxia
andauthored
Switch to 2.0 branch (#1152)
* Adapt boards to v2 partition tables * fix esp log error * fix display style * reset emotion after download assets * fix compiling * update assets default url * Add user only tools * Add image cache * smaller cache and buffer, more heap * use MAIN_EVENT_CLOCK_TICK to avoid audio glitches * bump to 2.0.0 * fix compiling errors --------- Co-authored-by: Xiaoxia <[email protected]>
1 parent 3a3dfc0 commit 83f6f8c

File tree

196 files changed

+3918
-4902
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+3918
-4902
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ main/assets/lang_config.h
1313
main/mmap_generate_emoji.h
1414
.DS_Store
1515
.cache
16-
main/mmap_generate_emoji.h
1716
*.pyc
1817
*.bin
1918
mmap_generate_*.h

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# CMakeLists in this exact order for cmake to work correctly
55
cmake_minimum_required(VERSION 3.16)
66

7-
set(PROJECT_VER "1.9.0")
7+
set(PROJECT_VER "2.0.0")
88

99
# Add this line to disable the specific warning
1010
add_compile_options(-Wno-missing-field-initializers)

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(SOURCES "audio/audio_codec.cc"
1414
"display/display.cc"
1515
"display/lcd_display.cc"
1616
"display/oled_display.cc"
17+
"display/emoji_collection.cc"
1718
"protocols/protocol.cc"
1819
"protocols/mqtt_protocol.cc"
1920
"protocols/websocket_protocol.cc"
@@ -23,6 +24,7 @@ set(SOURCES "audio/audio_codec.cc"
2324
"ota.cc"
2425
"settings.cc"
2526
"device_state_event.cc"
27+
"assets.cc"
2628
"main.cc"
2729
)
2830

main/Kconfig.projbuild

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ config OTA_URL
66
help
77
The application will access this URL to check for new firmwares and server address.
88

9+
config DEFAULT_ASSETS_URL_PREFIX
10+
string "Default Assets URL Prefix"
11+
default "https://files.xiaozhi.me/assets/default/"
12+
help
13+
The assets will be downloaded from this URL.
914

1015
choice
1116
prompt "Default Language"

main/application.cc

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "websocket_protocol.h"
88
#include "assets/lang_config.h"
99
#include "mcp_server.h"
10+
#include "assets.h"
11+
#include "settings.h"
1012

1113
#include <cstring>
1214
#include <esp_log.h>
@@ -67,6 +69,65 @@ Application::~Application() {
6769
vEventGroupDelete(event_group_);
6870
}
6971

72+
void Application::CheckAssetsVersion() {
73+
auto& board = Board::GetInstance();
74+
auto display = board.GetDisplay();
75+
auto assets = board.GetAssets();
76+
if (!assets) {
77+
ESP_LOGE(TAG, "Assets is not set for board %s", BOARD_NAME);
78+
return;
79+
}
80+
81+
if (!assets->partition_valid()) {
82+
ESP_LOGE(TAG, "Assets partition is not valid for board %s", BOARD_NAME);
83+
return;
84+
}
85+
86+
Settings settings("assets", true);
87+
// Check if there is a new assets need to be downloaded
88+
std::string download_url = settings.GetString("download_url");
89+
if (!download_url.empty()) {
90+
settings.EraseKey("download_url");
91+
}
92+
if (download_url.empty() && !assets->checksum_valid()) {
93+
download_url = assets->default_assets_url();
94+
}
95+
96+
if (!download_url.empty()) {
97+
char message[256];
98+
snprintf(message, sizeof(message), Lang::Strings::FOUND_NEW_ASSETS, download_url.c_str());
99+
Alert(Lang::Strings::LOADING_ASSETS, message, "cloud_arrow_down", Lang::Sounds::OGG_UPGRADE);
100+
101+
// Wait for the audio service to be idle for 3 seconds
102+
vTaskDelay(pdMS_TO_TICKS(3000));
103+
SetDeviceState(kDeviceStateUpgrading);
104+
board.SetPowerSaveMode(false);
105+
display->SetChatMessage("system", Lang::Strings::PLEASE_WAIT);
106+
107+
bool success = assets->Download(download_url, [display](int progress, size_t speed) -> void {
108+
std::thread([display, progress, speed]() {
109+
char buffer[32];
110+
snprintf(buffer, sizeof(buffer), "%d%% %uKB/s", progress, speed / 1024);
111+
display->SetChatMessage("system", buffer);
112+
}).detach();
113+
});
114+
115+
board.SetPowerSaveMode(true);
116+
vTaskDelay(pdMS_TO_TICKS(1000));
117+
118+
if (!success) {
119+
Alert(Lang::Strings::ERROR, Lang::Strings::DOWNLOAD_ASSETS_FAILED, "circle_xmark", Lang::Sounds::OGG_EXCLAMATION);
120+
vTaskDelay(pdMS_TO_TICKS(2000));
121+
return;
122+
}
123+
}
124+
125+
// Apply assets
126+
assets->Apply();
127+
display->SetChatMessage("system", "");
128+
display->SetEmotion("microchip_ai");
129+
}
130+
70131
void Application::CheckNewVersion(Ota& ota) {
71132
const int MAX_RETRY = 10;
72133
int retry_count = 0;
@@ -358,6 +419,9 @@ void Application::Start() {
358419
// Update the status bar immediately to show the network state
359420
display->UpdateStatusBar(true);
360421

422+
// Check for new assets version
423+
CheckAssetsVersion();
424+
361425
// Check for new firmware version or get the MQTT broker address
362426
Ota ota;
363427
CheckNewVersion(ota);
@@ -366,7 +430,9 @@ void Application::Start() {
366430
display->SetStatus(Lang::Strings::LOADING_PROTOCOL);
367431

368432
// Add MCP common tools before initializing the protocol
369-
McpServer::GetInstance().AddCommonTools();
433+
auto& mcp_server = McpServer::GetInstance();
434+
mcp_server.AddCommonTools();
435+
mcp_server.AddUserOnlyTools();
370436

371437
if (ota.HasMqttConfig()) {
372438
protocol_ = std::make_unique<MqttProtocol>();
@@ -496,7 +562,6 @@ void Application::Start() {
496562
});
497563
bool protocol_started = protocol_->Start();
498564

499-
// Print heap stats
500565
SystemInfo::PrintHeapStats();
501566
SetDeviceState(kDeviceStateIdle);
502567

@@ -541,7 +606,7 @@ void Application::MainEventLoop() {
541606

542607
if (bits & MAIN_EVENT_SEND_AUDIO) {
543608
while (auto packet = audio_service_.PopPacketFromSendQueue()) {
544-
if (!protocol_->SendAudio(std::move(packet))) {
609+
if (protocol_ && !protocol_->SendAudio(std::move(packet))) {
545610
break;
546611
}
547612
}
@@ -623,7 +688,9 @@ void Application::OnWakeWordDetected() {
623688
void Application::AbortSpeaking(AbortReason reason) {
624689
ESP_LOGI(TAG, "Abort speaking");
625690
aborted_ = true;
626-
protocol_->SendAbortSpeaking(reason);
691+
if (protocol_) {
692+
protocol_->SendAbortSpeaking(reason);
693+
}
627694
}
628695

629696
void Application::SetListeningMode(ListeningMode mode) {
@@ -695,6 +762,12 @@ void Application::SetDeviceState(DeviceState state) {
695762

696763
void Application::Reboot() {
697764
ESP_LOGI(TAG, "Rebooting...");
765+
// Disconnect the audio channel
766+
if (protocol_ && protocol_->IsAudioChannelOpened()) {
767+
protocol_->CloseAudioChannel();
768+
}
769+
protocol_.reset();
770+
audio_service_.Stop();
698771
esp_restart();
699772
}
700773

main/application.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Application {
8585

8686
void OnWakeWordDetected();
8787
void CheckNewVersion(Ota& ota);
88+
void CheckAssetsVersion();
8889
void ShowActivationCode(const std::string& code, const std::string& message);
8990
void SetListeningMode(ListeningMode mode);
9091
};

0 commit comments

Comments
 (0)