feat: build default assets instead of downloading and v2 tables for esp-hi, echoear (#1203)

* fix: call flush ready on io ready

* eachear: update to v2 partition table but disable class Assets currently

* stop gif if previewing an image

* feat: build default assets instead of downloading

* version updates

* fix None error

* delay 1s before enter wifi config mode

* fix compiling with v1 partition table

---------

Co-authored-by: Xiaoxia <terrence.huang@tenclass.com>
This commit is contained in:
Xiaoxia 2025-09-16 15:24:24 +08:00 committed by GitHub
parent b413e3ec03
commit 5018f6c03a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 1188 additions and 663 deletions

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@ config OTA_URL
choice
prompt "Flash Assets"
default FLASH_NONE_ASSETS
default FLASH_DEFAULT_ASSETS
help
Select the assets to flash.
@ -532,7 +532,11 @@ config USE_AUDIO_PROCESSOR
config USE_DEVICE_AEC
bool "Enable Device-Side AEC"
default n
depends on USE_AUDIO_PROCESSOR && (BOARD_TYPE_ESP_BOX_3 || BOARD_TYPE_ESP_BOX || BOARD_TYPE_ESP_BOX_LITE || BOARD_TYPE_LICHUANG_DEV || BOARD_TYPE_ESP32S3_KORVO2_V3 || BOARD_TYPE_ESP32S3_Touch_AMOLED_1_75 || BOARD_TYPE_ESP32S3_Touch_AMOLED_2_06 || BOARD_TYPE_ESP32P4_WIFI6_Touch_LCD_4B || BOARD_TYPE_ESP32P4_WIFI6_Touch_LCD_XC || BOARD_TYPE_ESP_S3_LCD_EV_Board_2 || BOARD_TYPE_YUNLIAO_S3)
depends on USE_AUDIO_PROCESSOR && (BOARD_TYPE_ESP_BOX_3 || BOARD_TYPE_ESP_BOX || BOARD_TYPE_ESP_BOX_LITE \
|| BOARD_TYPE_LICHUANG_DEV || BOARD_TYPE_ESP32S3_KORVO2_V3 || BOARD_TYPE_ESP32S3_Touch_AMOLED_1_75 \
|| BOARD_TYPE_ESP32S3_Touch_AMOLED_2_06 || BOARD_TYPE_ESP32P4_WIFI6_Touch_LCD_4B \
|| BOARD_TYPE_ESP32P4_WIFI6_Touch_LCD_XC || BOARD_TYPE_ESP_S3_LCD_EV_Board_2 || BOARD_TYPE_YUNLIAO_S3 \
|| BOARD_TYPE_ECHOEAR)
help
因为性能不够,不建议和微信聊天界面风格同时开启

View File

@ -72,28 +72,20 @@ Application::~Application() {
void Application::CheckAssetsVersion() {
auto& board = Board::GetInstance();
auto display = board.GetDisplay();
auto assets = board.GetAssets();
if (!assets) {
ESP_LOGE(TAG, "Assets is not set for board %s", BOARD_NAME);
return;
}
auto& assets = Assets::GetInstance();
if (!assets->partition_valid()) {
ESP_LOGE(TAG, "Assets partition is not valid for board %s", BOARD_NAME);
if (!assets.partition_valid()) {
ESP_LOGW(TAG, "Assets partition is disabled for board %s", BOARD_NAME);
return;
}
Settings settings("assets", true);
// Check if there is a new assets need to be downloaded
std::string download_url = settings.GetString("download_url");
if (!download_url.empty()) {
settings.EraseKey("download_url");
}
if (download_url.empty() && !assets->checksum_valid()) {
download_url = assets->default_assets_url();
}
if (!download_url.empty()) {
settings.EraseKey("download_url");
char message[256];
snprintf(message, sizeof(message), Lang::Strings::FOUND_NEW_ASSETS, download_url.c_str());
Alert(Lang::Strings::LOADING_ASSETS, message, "cloud_arrow_down", Lang::Sounds::OGG_UPGRADE);
@ -104,7 +96,7 @@ void Application::CheckAssetsVersion() {
board.SetPowerSaveMode(false);
display->SetChatMessage("system", Lang::Strings::PLEASE_WAIT);
bool success = assets->Download(download_url, [display](int progress, size_t speed) -> void {
bool success = assets.Download(download_url, [display](int progress, size_t speed) -> void {
std::thread([display, progress, speed]() {
char buffer[32];
snprintf(buffer, sizeof(buffer), "%d%% %uKB/s", progress, speed / 1024);
@ -123,7 +115,7 @@ void Application::CheckAssetsVersion() {
}
// Apply assets
assets->Apply();
assets.Apply();
display->SetChatMessage("system", "");
display->SetEmotion("microchip_ai");
}
@ -844,10 +836,8 @@ void Application::SendMcpMessage(const std::string& payload) {
// Make sure you are using main thread to send MCP message
if (xTaskGetCurrentTaskHandle() == main_event_loop_task_handle_) {
ESP_LOGI(TAG, "Send MCP message in main thread");
protocol_->SendMcpMessage(payload);
} else {
ESP_LOGI(TAG, "Send MCP message in sub thread");
Schedule([this, payload = std::move(payload)]() {
protocol_->SendMcpMessage(payload);
});

View File

@ -21,13 +21,7 @@ struct mmap_assets_table {
};
Assets::Assets(std::string default_assets_url) {
if (default_assets_url.find("http") == 0) {
default_assets_url_ = default_assets_url;
} else {
ESP_LOGE(TAG, "The default assets url is not a http url: %s", default_assets_url.c_str());
}
Assets::Assets() {
// Initialize the partition
InitializePartition();
}

View File

@ -10,23 +10,6 @@
#include <model_path.h>
// All combinations of wakenet_model, text_font, emoji_collection can be found from the following url:
// https://github.com/78/xiaozhi-fonts/releases/tag/assets
#define ASSETS_PUHUI_COMMON_14_1 "none-font_puhui_common_14_1-none.bin"
#define ASSETS_XIAOZHI_WAKENET "wn9_nihaoxiaozhi_tts-none-none.bin"
#define ASSETS_XIAOZHI_WAKENET_SMALL "wn9s_nihaoxiaozhi-none-none.bin"
#define ASSETS_XIAOZHI_PUHUI_COMMON_14_1 "wn9_nihaoxiaozhi_tts-font_puhui_common_14_1-none.bin"
#define ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_32 "wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_32.bin"
#define ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_64.bin"
#define ASSETS_XIAOZHI_PUHUI_COMMON_20_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_20_4-emojis_64.bin"
#define ASSETS_XIAOZHI_PUHUI_COMMON_30_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_30_4-emojis_64.bin"
#define ASSETS_XIAOZHI_S_PUHUI_COMMON_14_1 "wn9s_nihaoxiaozhi-font_puhui_common_14_1-none.bin"
#define ASSETS_XIAOZHI_S_PUHUI_COMMON_16_4_EMOJI_32 "wn9s_nihaoxiaozhi-font_puhui_common_16_4-emojis_32.bin"
#define ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_32 "wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_32.bin"
#define ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_64 "wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_64.bin"
#define ASSETS_XIAOZHI_S_PUHUI_COMMON_30_4_EMOJI_64 "wn9s_nihaoxiaozhi-font_puhui_common_30_4-emojis_64.bin"
struct Asset {
size_t size;
size_t offset;
@ -34,23 +17,27 @@ struct Asset {
class Assets {
public:
Assets(std::string default_assets_url);
static Assets& GetInstance() {
static Assets instance;
return instance;
}
~Assets();
bool Download(std::string url, std::function<void(int progress, size_t speed)> progress_callback);
bool Apply();
bool GetAssetData(const std::string& name, void*& ptr, size_t& size);
inline bool partition_valid() const { return partition_valid_; }
inline bool checksum_valid() const { return checksum_valid_; }
inline std::string default_assets_url() const { return default_assets_url_; }
private:
Assets();
Assets(const Assets&) = delete;
Assets& operator=(const Assets&) = delete;
bool InitializePartition();
uint32_t CalculateChecksum(const char* data, uint32_t length);
bool GetAssetData(const std::string& name, void*& ptr, size_t& size);
const esp_partition_t* partition_ = nullptr;
esp_partition_mmap_handle_t mmap_handle_ = 0;

View File

@ -176,12 +176,3 @@ std::string Board::GetSystemInfoJson() {
json += R"(})";
return json;
}
Assets* Board::GetAssets() {
#ifdef DEFAULT_ASSETS
static Assets assets(DEFAULT_ASSETS);
return &assets;
#else
return nullptr;
#endif
}

View File

@ -52,7 +52,6 @@ public:
virtual void SetPowerSaveMode(bool enabled) = 0;
virtual std::string GetBoardJson() = 0;
virtual std::string GetDeviceStatusJson() = 0;
virtual Assets* GetAssets();
};
#define DECLARE_BOARD(BOARD_CLASS_NAME) \

View File

@ -41,6 +41,9 @@ void WifiBoard::EnterWifiConfigMode() {
wifi_ap.SetSsidPrefix("Xiaozhi");
wifi_ap.Start();
// 等待 1 秒显示开发板信息
vTaskDelay(pdMS_TO_TICKS(1000));
// 显示 WiFi 配置 AP 的 SSID 和 Web 服务器 URL
std::string hint = Lang::Strings::CONNECT_TO_HOTSPOT;
hint += wifi_ap.GetSsid();

View File

@ -517,12 +517,13 @@ private:
void InitializeSpi()
{
const spi_bus_config_t bus_config = TAIJIPI_ST77916_PANEL_BUS_QSPI_CONFIG(QSPI_PIN_NUM_LCD_PCLK,
QSPI_PIN_NUM_LCD_DATA0,
QSPI_PIN_NUM_LCD_DATA1,
QSPI_PIN_NUM_LCD_DATA2,
QSPI_PIN_NUM_LCD_DATA3,
QSPI_LCD_H_RES * 80 * sizeof(uint16_t));
spi_bus_config_t bus_config = TAIJIPI_ST77916_PANEL_BUS_QSPI_CONFIG(QSPI_PIN_NUM_LCD_PCLK,
QSPI_PIN_NUM_LCD_DATA0,
QSPI_PIN_NUM_LCD_DATA1,
QSPI_PIN_NUM_LCD_DATA2,
QSPI_PIN_NUM_LCD_DATA3,
QSPI_LCD_H_RES * 80 * sizeof(uint16_t));
// bus_config.isr_cpu_id = ESP_INTR_CPU_AFFINITY_1;
ESP_ERROR_CHECK(spi_bus_initialize(QSPI_LCD_HOST, &bus_config, SPI_DMA_CH_AUTO));
}

View File

@ -27,10 +27,6 @@ idf.py menuconfig
### 基本配置
- `Xiaozhi Assistant``Board Type` → 选择 `EchoEar`
### 分区表配置
- `Partition Table``Partition Table` → 选择 `Custom partition table CSV`
- `Partition Table``Custom partition CSV file` → 输入 `partitions/v1/16m_echoear.csv`
### UI风格选择
EchoEar 支持两种不同的UI显示风格通过修改代码中的宏定义来选择

View File

@ -4,7 +4,6 @@
{
"name": "echoear",
"sdkconfig_append": [
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions/v1/16m_echoear.csv\""
]
}
]

View File

@ -10,16 +10,37 @@
#include <freertos/task.h>
#include <sys/time.h>
#include <time.h>
#include <model_path.h>
#include "display/lcd_display.h"
#include "mmap_generate_emoji_normal.h"
#include "config.h"
#include "gfx.h"
#include "application.h"
namespace anim {
static const char* TAG = "emoji";
// Asset name mapping from the old constants to file names
static const std::unordered_map<std::string, std::string> asset_name_map = {
{"angry_one", "angry_one.aaf"},
{"dizzy_one", "dizzy_one.aaf"},
{"enjoy_one", "enjoy_one.aaf"},
{"happy_one", "happy_one.aaf"},
{"idle_one", "idle_one.aaf"},
{"listen", "listen.aaf"},
{"sad_one", "sad_one.aaf"},
{"shocked_one", "shocked_one.aaf"},
{"thinking_one", "thinking_one.aaf"},
{"icon_battery", "icon_Battery.bin"},
{"icon_wifi_failed", "icon_WiFi_failed.bin"},
{"icon_mic", "icon_mic.bin"},
{"icon_speaker_zzz", "icon_speaker_zzz.bin"},
{"icon_wifi", "icon_wifi.bin"},
{"srmodels", "srmodels.bin"},
{"kaiti", "KaiTi.ttf"}
};
// UI element management
static gfx_obj_t* obj_label_tips = nullptr;
static gfx_obj_t* obj_label_time = nullptr;
@ -29,7 +50,7 @@ static gfx_obj_t* obj_img_icon = nullptr;
static gfx_image_dsc_t icon_img_dsc;
// Track current icon to determine when to show time
static int current_icon_type = MMAP_EMOJI_NORMAL_ICON_BATTERY_BIN;
static std::string current_icon_type = "icon_battery";
enum class UIDisplayMode : uint8_t {
SHOW_ANIM_TOP = 1, // Show obj_anim_mic
@ -60,7 +81,7 @@ static void SetUIDisplayMode(UIDisplayMode mode)
static void clock_tm_callback(void* user_data)
{
// Only display time when battery icon is shown
if (current_icon_type == MMAP_EMOJI_NORMAL_ICON_BATTERY_BIN) {
if (current_icon_type == "icon_battery") {
time_t now;
struct tm timeinfo;
time(&now);
@ -77,17 +98,6 @@ static void clock_tm_callback(void* user_data)
}
}
static void InitializeAssets(mmap_assets_handle_t* assets_handle)
{
const mmap_assets_config_t assets_cfg = {
.partition_label = "assets_A",
.max_files = MMAP_EMOJI_NORMAL_FILES,
.checksum = MMAP_EMOJI_NORMAL_CHECKSUM,
.flags = {.mmap_enable = true, .full_check = true}
};
mmap_assets_new(&assets_cfg, assets_handle);
}
static void InitializeGraphics(esp_lcd_panel_handle_t panel, gfx_handle_t* engine_handle)
{
@ -111,19 +121,24 @@ static void InitializeGraphics(esp_lcd_panel_handle_t panel, gfx_handle_t* engin
};
gfx_cfg.task.task_stack_caps = MALLOC_CAP_DEFAULT;
gfx_cfg.task.task_affinity = 0;
gfx_cfg.task.task_priority = 5;
gfx_cfg.task.task_affinity = 1;
gfx_cfg.task.task_priority = 1;
gfx_cfg.task.task_stack = 20 * 1024;
*engine_handle = gfx_emote_init(&gfx_cfg);
}
static void InitializeEyeAnimation(gfx_handle_t engine_handle, mmap_assets_handle_t assets_handle)
static void InitializeEyeAnimation(gfx_handle_t engine_handle)
{
obj_anim_eye = gfx_anim_create(engine_handle);
const void* anim_data = mmap_assets_get_mem(assets_handle, MMAP_EMOJI_NORMAL_IDLE_ONE_AAF);
size_t anim_size = mmap_assets_get_size(assets_handle, MMAP_EMOJI_NORMAL_IDLE_ONE_AAF);
void* anim_data = nullptr;
size_t anim_size = 0;
auto& assets = Assets::GetInstance();
if (!assets.GetAssetData(asset_name_map.at("idle_one"), anim_data, anim_size)) {
ESP_LOGE(TAG, "Failed to get idle_one animation data");
return;
}
gfx_anim_set_src(obj_anim_eye, anim_data, anim_size);
@ -133,13 +148,21 @@ static void InitializeEyeAnimation(gfx_handle_t engine_handle, mmap_assets_handl
gfx_anim_start(obj_anim_eye);
}
static void InitializeFont(gfx_handle_t engine_handle, mmap_assets_handle_t assets_handle)
static void InitializeFont(gfx_handle_t engine_handle)
{
gfx_font_t font;
void* font_data = nullptr;
size_t font_size = 0;
auto& assets = Assets::GetInstance();
if (!assets.GetAssetData(asset_name_map.at("kaiti"), font_data, font_size)) {
ESP_LOGE(TAG, "Failed to get kaiti font data");
return;
}
gfx_label_cfg_t font_cfg = {
.name = "DejaVuSans.ttf",
.mem = mmap_assets_get_mem(assets_handle, MMAP_EMOJI_NORMAL_KAITI_TTF),
.mem_size = static_cast<size_t>(mmap_assets_get_size(assets_handle, MMAP_EMOJI_NORMAL_KAITI_TTF)),
.mem = font_data,
.mem_size = font_size,
};
gfx_label_new_font(engine_handle, &font_cfg, &font);
@ -170,24 +193,30 @@ static void InitializeLabels(gfx_handle_t engine_handle)
gfx_label_set_text_align(obj_label_time, GFX_TEXT_ALIGN_CENTER);
}
static void InitializeMicAnimation(gfx_handle_t engine_handle, mmap_assets_handle_t assets_handle)
static void InitializeMicAnimation(gfx_handle_t engine_handle)
{
obj_anim_mic = gfx_anim_create(engine_handle);
gfx_obj_align(obj_anim_mic, GFX_ALIGN_TOP_MID, 0, 25);
const void* anim_data = mmap_assets_get_mem(assets_handle, MMAP_EMOJI_NORMAL_LISTEN_AAF);
size_t anim_size = mmap_assets_get_size(assets_handle, MMAP_EMOJI_NORMAL_LISTEN_AAF);
void* anim_data = nullptr;
size_t anim_size = 0;
auto& assets = Assets::GetInstance();
if (!assets.GetAssetData(asset_name_map.at("listen"), anim_data, anim_size)) {
ESP_LOGE(TAG, "Failed to get listen animation data");
return;
}
gfx_anim_set_src(obj_anim_mic, anim_data, anim_size);
gfx_anim_start(obj_anim_mic);
gfx_obj_set_visible(obj_anim_mic, false);
}
static void InitializeIcon(gfx_handle_t engine_handle, mmap_assets_handle_t assets_handle)
static void InitializeIcon(gfx_handle_t engine_handle)
{
obj_img_icon = gfx_img_create(engine_handle);
gfx_obj_align(obj_img_icon, GFX_ALIGN_TOP_MID, -100, 38);
SetupImageDescriptor(assets_handle, &icon_img_dsc, MMAP_EMOJI_NORMAL_ICON_WIFI_FAILED_BIN);
SetupImageDescriptor(&icon_img_dsc, "icon_wifi_failed");
gfx_img_set_src(obj_img_icon, static_cast<void*>(&icon_img_dsc));
}
@ -199,12 +228,17 @@ static void RegisterCallbacks(esp_lcd_panel_io_handle_t panel_io, gfx_handle_t e
esp_lcd_panel_io_register_event_callbacks(panel_io, &cbs, engine_handle);
}
void SetupImageDescriptor(mmap_assets_handle_t assets_handle,
gfx_image_dsc_t* img_dsc,
int asset_id)
void SetupImageDescriptor(gfx_image_dsc_t* img_dsc, const std::string& asset_name)
{
const void* img_data = mmap_assets_get_mem(assets_handle, asset_id);
size_t img_size = mmap_assets_get_size(assets_handle, asset_id);
auto& assets = Assets::GetInstance();
std::string filename = asset_name_map.at(asset_name);
void* img_data = nullptr;
size_t img_size = 0;
if (!assets.GetAssetData(filename, img_data, img_size)) {
ESP_LOGE(TAG, "Failed to get asset data for %s", asset_name.c_str());
return;
}
std::memcpy(&img_dsc->header, img_data, sizeof(gfx_image_header_t));
img_dsc->data = static_cast<const uint8_t*>(img_data) + sizeof(gfx_image_header_t);
@ -215,20 +249,19 @@ EmoteEngine::EmoteEngine(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t
{
ESP_LOGI(TAG, "Create EmoteEngine, panel: %p, panel_io: %p", panel, panel_io);
InitializeAssets(&assets_handle_);
InitializeGraphics(panel, &engine_handle_);
gfx_emote_lock(engine_handle_);
gfx_emote_set_bg_color(engine_handle_, GFX_COLOR_HEX(0x000000));
// Initialize all UI components
InitializeEyeAnimation(engine_handle_, assets_handle_);
InitializeFont(engine_handle_, assets_handle_);
InitializeEyeAnimation(engine_handle_);
InitializeFont(engine_handle_);
InitializeLabels(engine_handle_);
InitializeMicAnimation(engine_handle_, assets_handle_);
InitializeIcon(engine_handle_, assets_handle_);
InitializeMicAnimation(engine_handle_);
InitializeIcon(engine_handle_);
current_icon_type = MMAP_EMOJI_NORMAL_ICON_WIFI_FAILED_BIN;
current_icon_type = "icon_wifi_failed";
SetUIDisplayMode(UIDisplayMode::SHOW_TIPS);
gfx_timer_create(engine_handle_, clock_tm_callback, 1000, obj_label_tips);
@ -244,21 +277,23 @@ EmoteEngine::~EmoteEngine()
gfx_emote_deinit(engine_handle_);
engine_handle_ = nullptr;
}
if (assets_handle_) {
mmap_assets_del(assets_handle_);
assets_handle_ = nullptr;
}
}
void EmoteEngine::setEyes(int aaf, bool repeat, int fps)
void EmoteEngine::setEyes(const std::string& asset_name, bool repeat, int fps)
{
if (!engine_handle_) {
return;
}
const void* src_data = mmap_assets_get_mem(assets_handle_, aaf);
size_t src_len = mmap_assets_get_size(assets_handle_, aaf);
auto& assets = Assets::GetInstance();
std::string filename = asset_name_map.at(asset_name);
void* src_data = nullptr;
size_t src_len = 0;
if (!assets.GetAssetData(filename, src_data, src_len)) {
ESP_LOGE(TAG, "Failed to get asset data for %s", asset_name.c_str());
return;
}
Lock();
gfx_anim_set_src(obj_anim_eye, src_data, src_len);
@ -286,16 +321,16 @@ void EmoteEngine::Unlock()
}
}
void EmoteEngine::SetIcon(int asset_id)
void EmoteEngine::SetIcon(const std::string& asset_name)
{
if (!engine_handle_) {
return;
}
Lock();
SetupImageDescriptor(assets_handle_, &icon_img_dsc, asset_id);
SetupImageDescriptor(&icon_img_dsc, asset_name);
gfx_img_set_src(obj_img_icon, static_cast<void*>(&icon_img_dsc));
current_icon_type = asset_id;
current_icon_type = asset_name;
Unlock();
}
@ -303,6 +338,7 @@ bool EmoteEngine::OnFlushIoReady(esp_lcd_panel_io_handle_t panel_io,
esp_lcd_panel_io_event_data_t* edata,
void* user_ctx)
{
gfx_emote_flush_ready((gfx_handle_t)user_ctx, true);
return true;
}
@ -313,7 +349,6 @@ void EmoteEngine::OnFlush(gfx_handle_t handle, int x_start, int y_start,
if (panel) {
esp_lcd_panel_draw_bitmap(panel, x_start, y_start, x_end, y_end, color_data);
}
gfx_emote_flush_ready(handle, true);
}
// EmoteDisplay implementation
@ -330,36 +365,36 @@ void EmoteDisplay::SetEmotion(const char* emotion)
return;
}
using EmotionParam = std::tuple<int, bool, int>;
using EmotionParam = std::tuple<std::string, bool, int>;
static const std::unordered_map<std::string, EmotionParam> emotion_map = {
{"happy", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"laughing", {MMAP_EMOJI_NORMAL_ENJOY_ONE_AAF, true, 20}},
{"funny", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"loving", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"embarrassed", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"confident", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"delicious", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"sad", {MMAP_EMOJI_NORMAL_SAD_ONE_AAF, true, 20}},
{"crying", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"sleepy", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"silly", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"angry", {MMAP_EMOJI_NORMAL_ANGRY_ONE_AAF, true, 20}},
{"surprised", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"shocked", {MMAP_EMOJI_NORMAL_SHOCKED_ONE_AAF, true, 20}},
{"thinking", {MMAP_EMOJI_NORMAL_THINKING_ONE_AAF, true, 20}},
{"winking", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"relaxed", {MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20}},
{"confused", {MMAP_EMOJI_NORMAL_DIZZY_ONE_AAF, true, 20}},
{"neutral", {MMAP_EMOJI_NORMAL_IDLE_ONE_AAF, false, 20}},
{"idle", {MMAP_EMOJI_NORMAL_IDLE_ONE_AAF, false, 20}},
{"happy", {"happy_one", true, 20}},
{"laughing", {"enjoy_one", true, 20}},
{"funny", {"happy_one", true, 20}},
{"loving", {"happy_one", true, 20}},
{"embarrassed", {"happy_one", true, 20}},
{"confident", {"happy_one", true, 20}},
{"delicious", {"happy_one", true, 20}},
{"sad", {"sad_one", true, 20}},
{"crying", {"happy_one", true, 20}},
{"sleepy", {"happy_one", true, 20}},
{"silly", {"happy_one", true, 20}},
{"angry", {"angry_one", true, 20}},
{"surprised", {"happy_one", true, 20}},
{"shocked", {"shocked_one", true, 20}},
{"thinking", {"thinking_one", true, 20}},
{"winking", {"happy_one", true, 20}},
{"relaxed", {"happy_one", true, 20}},
{"confused", {"dizzy_one", true, 20}},
{"neutral", {"idle_one", false, 20}},
{"idle", {"idle_one", false, 20}},
};
auto it = emotion_map.find(emotion);
if (it != emotion_map.end()) {
int aaf = std::get<0>(it->second);
std::string asset_name = std::get<0>(it->second);
bool repeat = std::get<1>(it->second);
int fps = std::get<2>(it->second);
engine_->setEyes(aaf, repeat, fps);
engine_->setEyes(asset_name, repeat, fps);
}
}
@ -381,17 +416,17 @@ void EmoteDisplay::SetStatus(const char* status)
if (std::strcmp(status, "聆听中...") == 0) {
SetUIDisplayMode(UIDisplayMode::SHOW_ANIM_TOP);
engine_->setEyes(MMAP_EMOJI_NORMAL_HAPPY_ONE_AAF, true, 20);
engine_->SetIcon(MMAP_EMOJI_NORMAL_ICON_MIC_BIN);
engine_->setEyes("happy_one", true, 20);
engine_->SetIcon("icon_mic");
} else if (std::strcmp(status, "待命") == 0) {
SetUIDisplayMode(UIDisplayMode::SHOW_TIME);
engine_->SetIcon(MMAP_EMOJI_NORMAL_ICON_BATTERY_BIN);
engine_->SetIcon("icon_battery");
} else if (std::strcmp(status, "说话中...") == 0) {
SetUIDisplayMode(UIDisplayMode::SHOW_TIPS);
engine_->SetIcon(MMAP_EMOJI_NORMAL_ICON_SPEAKER_ZZZ_BIN);
engine_->SetIcon("icon_speaker_zzz");
} else if (std::strcmp(status, "错误") == 0) {
SetUIDisplayMode(UIDisplayMode::SHOW_TIPS);
engine_->SetIcon(MMAP_EMOJI_NORMAL_ICON_WIFI_FAILED_BIN);
engine_->SetIcon("icon_wifi_failed");
}
engine_->Lock();

View File

@ -5,13 +5,13 @@
#include <functional>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
#include "mmap_generate_emoji_normal.h"
#include "gfx.h"
#include "assets.h"
namespace anim {
// Helper function for setting up image descriptors
void SetupImageDescriptor(mmap_assets_handle_t assets_handle, gfx_image_dsc_t* img_dsc, int asset_id);
void SetupImageDescriptor(gfx_image_dsc_t* img_dsc, const std::string& asset_name);
class EmoteEngine;
@ -23,14 +23,13 @@ public:
EmoteEngine(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io);
~EmoteEngine();
void setEyes(int aaf, bool repeat, int fps);
void setEyes(const std::string& asset_name, bool repeat, int fps);
void stopEyes();
void Lock();
void Unlock();
void SetIcon(int asset_id);
mmap_assets_handle_t GetAssetsHandle() const { return assets_handle_; }
void SetIcon(const std::string& asset_name);
// Callback functions (public to be accessible from static helper functions)
static bool OnFlushIoReady(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx);
@ -38,7 +37,6 @@ public:
private:
gfx_handle_t engine_handle_;
mmap_assets_handle_t assets_handle_;
};
class EmoteDisplay : public Display {

View File

@ -6,7 +6,7 @@
"sdkconfig_append": [
"CONFIG_IDF_TARGET=\"esp32c3\"",
"CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y",
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions/v1/4m_esp-hi.csv\"",
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions/v2/4m.csv\"",
"CONFIG_BOARD_TYPE_ESP_HI=y",
"CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=3",
"CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=4",

View File

@ -1,9 +1,9 @@
#include <cstring>
#include "display/lcd_display.h"
#include <esp_log.h>
#include "mmap_generate_emoji.h"
#include "emoji_display.h"
#include "assets/lang_config.h"
#include "assets.h"
#include <esp_lcd_panel_io.h>
#include <freertos/FreeRTOS.h>
@ -15,6 +15,19 @@ static const char *TAG = "emoji";
namespace anim {
// Emoji asset name mapping based on usage pattern
static const std::unordered_map<std::string, std::string> emoji_asset_name_map = {
{"connecting", "connecting.aaf"},
{"wake", "wake.aaf"},
{"asking", "asking.aaf"},
{"happy_loop", "happy_loop.aaf"},
{"sad_loop", "sad_loop.aaf"},
{"anger_loop", "anger_loop.aaf"},
{"panic_loop", "panic_loop.aaf"},
{"blink_quick", "blink_quick.aaf"},
{"scorn_loop", "scorn_loop.aaf"}
};
bool EmojiPlayer::OnFlushIoReady(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
auto* disp_drv = static_cast<anim_player_handle_t*>(user_ctx);
@ -31,14 +44,6 @@ void EmojiPlayer::OnFlush(anim_player_handle_t handle, int x_start, int y_start,
EmojiPlayer::EmojiPlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io)
{
ESP_LOGI(TAG, "Create EmojiPlayer, panel: %p, panel_io: %p", panel, panel_io);
const mmap_assets_config_t assets_cfg = {
.partition_label = "assets_A",
.max_files = MMAP_EMOJI_FILES,
.checksum = MMAP_EMOJI_CHECKSUM,
.flags = {.mmap_enable = true, .full_check = true}
};
mmap_assets_new(&assets_cfg, &assets_handle_);
anim_player_config_t player_cfg = {
.flush_cb = OnFlush,
@ -54,7 +59,7 @@ EmojiPlayer::EmojiPlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t
.on_color_trans_done = OnFlushIoReady,
};
esp_lcd_panel_io_register_event_callbacks(panel_io, &cbs, player_handle_);
StartPlayer(MMAP_EMOJI_CONNECTING_AAF, true, 15);
StartPlayer("connecting", true, 15);
}
EmojiPlayer::~EmojiPlayer()
@ -64,26 +69,25 @@ EmojiPlayer::~EmojiPlayer()
anim_player_deinit(player_handle_);
player_handle_ = nullptr;
}
if (assets_handle_) {
mmap_assets_del(assets_handle_);
assets_handle_ = NULL;
}
}
void EmojiPlayer::StartPlayer(int aaf, bool repeat, int fps)
void EmojiPlayer::StartPlayer(const std::string& asset_name, bool repeat, int fps)
{
if (player_handle_) {
uint32_t start, end;
const void *src_data;
size_t src_len;
void *src_data = nullptr;
size_t src_len = 0;
src_data = mmap_assets_get_mem(assets_handle_, aaf);
src_len = mmap_assets_get_size(assets_handle_, aaf);
auto& assets = Assets::GetInstance();
std::string filename = emoji_asset_name_map.at(asset_name);
if (!assets.GetAssetData(filename, src_data, src_len)) {
ESP_LOGE(TAG, "Failed to get asset data for %s", asset_name.c_str());
return;
}
anim_player_set_src_data(player_handle_, src_data, src_len);
anim_player_get_segment(player_handle_, &start, &end);
if(MMAP_EMOJI_WAKE_AAF == aaf){
if(asset_name == "wake"){
start = 7;
}
anim_player_set_segment(player_handle_, start, end, fps, true);
@ -114,26 +118,26 @@ void EmojiWidget::SetEmotion(const char* emotion)
return;
}
using Param = std::tuple<int, bool, int>;
using Param = std::tuple<std::string, bool, int>;
static const std::unordered_map<std::string, Param> emotion_map = {
{"happy", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"laughing", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"funny", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"loving", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"embarrassed", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"confident", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"delicious", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"sad", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
{"crying", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
{"sleepy", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
{"silly", {MMAP_EMOJI_SAD_LOOP_AAF, true, 25}},
{"angry", {MMAP_EMOJI_ANGER_LOOP_AAF, true, 25}},
{"surprised", {MMAP_EMOJI_PANIC_LOOP_AAF, true, 25}},
{"shocked", {MMAP_EMOJI_PANIC_LOOP_AAF, true, 25}},
{"thinking", {MMAP_EMOJI_HAPPY_LOOP_AAF, true, 25}},
{"winking", {MMAP_EMOJI_BLINK_QUICK_AAF, true, 5}},
{"relaxed", {MMAP_EMOJI_SCORN_LOOP_AAF, true, 25}},
{"confused", {MMAP_EMOJI_SCORN_LOOP_AAF, true, 25}},
{"happy", {"happy_loop", true, 25}},
{"laughing", {"happy_loop", true, 25}},
{"funny", {"happy_loop", true, 25}},
{"loving", {"happy_loop", true, 25}},
{"embarrassed", {"happy_loop", true, 25}},
{"confident", {"happy_loop", true, 25}},
{"delicious", {"happy_loop", true, 25}},
{"sad", {"sad_loop", true, 25}},
{"crying", {"sad_loop", true, 25}},
{"sleepy", {"sad_loop", true, 25}},
{"silly", {"sad_loop", true, 25}},
{"angry", {"anger_loop", true, 25}},
{"surprised", {"panic_loop", true, 25}},
{"shocked", {"panic_loop", true, 25}},
{"thinking", {"happy_loop", true, 25}},
{"winking", {"blink_quick", true, 5}},
{"relaxed", {"scorn_loop", true, 25}},
{"confused", {"scorn_loop", true, 25}},
};
auto it = emotion_map.find(emotion);
@ -148,9 +152,9 @@ void EmojiWidget::SetStatus(const char* status)
{
if (player_) {
if (strcmp(status, Lang::Strings::LISTENING) == 0) {
player_->StartPlayer(MMAP_EMOJI_ASKING_AAF, true, 15);
player_->StartPlayer("asking", true, 15);
} else if (strcmp(status, Lang::Strings::STANDBY) == 0) {
player_->StartPlayer(MMAP_EMOJI_WAKE_AAF, true, 15);
player_->StartPlayer("wake", true, 15);
}
}
}

View File

@ -6,7 +6,7 @@
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
#include "anim_player.h"
#include "mmap_generate_emoji.h"
#include "assets.h"
namespace anim {
@ -20,7 +20,7 @@ public:
EmojiPlayer(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io);
~EmojiPlayer();
void StartPlayer(int aaf, bool repeat, int fps);
void StartPlayer(const std::string& asset_name, bool repeat, int fps);
void StopPlayer();
private:
@ -28,7 +28,6 @@ private:
static void OnFlush(anim_player_handle_t handle, int x_start, int y_start, int x_end, int y_end, const void *color_data);
anim_player_handle_t player_handle_;
mmap_assets_handle_t assets_handle_;
};
class EmojiWidget : public Display {

View File

@ -7,5 +7,5 @@
```
Partition Table --->
Partition Table (Custom partition table CSV) --->
(partitions/v1/8m.csv) Custom partition CSV file
(partitions/v2/8m.csv) Custom partition CSV file
```

View File

@ -17,13 +17,13 @@
#define TAG "LcdDisplay"
LV_FONT_DECLARE(LVGL_TEXT_FONT);
LV_FONT_DECLARE(LVGL_ICON_FONT);
LV_FONT_DECLARE(BUILTIN_TEXT_FONT);
LV_FONT_DECLARE(BUILTIN_ICON_FONT);
LV_FONT_DECLARE(font_awesome_30_4);
void LcdDisplay::InitializeLcdThemes() {
auto text_font = std::make_shared<LvglBuiltInFont>(&LVGL_TEXT_FONT);
auto icon_font = std::make_shared<LvglBuiltInFont>(&LVGL_ICON_FONT);
auto text_font = std::make_shared<LvglBuiltInFont>(&BUILTIN_TEXT_FONT);
auto icon_font = std::make_shared<LvglBuiltInFont>(&BUILTIN_ICON_FONT);
auto large_icon_font = std::make_shared<LvglBuiltInFont>(&font_awesome_30_4);
// light theme
@ -879,6 +879,9 @@ void LcdDisplay::SetPreviewImage(std::unique_ptr<LvglImage> image) {
lv_obj_remove_flag(emoji_box_, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
preview_image_cached_.reset();
if (gif_controller_) {
gif_controller_->Start();
}
return;
}
@ -892,6 +895,9 @@ void LcdDisplay::SetPreviewImage(std::unique_ptr<LvglImage> image) {
}
// Hide emoji_box_
if (gif_controller_) {
gif_controller_->Stop();
}
lv_obj_add_flag(emoji_box_, LV_OBJ_FLAG_HIDDEN);
lv_obj_remove_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
esp_timer_stop(preview_timer_);

View File

@ -13,8 +13,8 @@
#define TAG "OledDisplay"
LV_FONT_DECLARE(LVGL_TEXT_FONT);
LV_FONT_DECLARE(LVGL_ICON_FONT);
LV_FONT_DECLARE(BUILTIN_TEXT_FONT);
LV_FONT_DECLARE(BUILTIN_ICON_FONT);
LV_FONT_DECLARE(font_awesome_30_1);
OledDisplay::OledDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
@ -23,8 +23,8 @@ OledDisplay::OledDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handl
width_ = width;
height_ = height;
auto text_font = std::make_shared<LvglBuiltInFont>(&LVGL_TEXT_FONT);
auto icon_font = std::make_shared<LvglBuiltInFont>(&LVGL_ICON_FONT);
auto text_font = std::make_shared<LvglBuiltInFont>(&BUILTIN_TEXT_FONT);
auto icon_font = std::make_shared<LvglBuiltInFont>(&BUILTIN_ICON_FONT);
auto large_icon_font = std::make_shared<LvglBuiltInFont>(&font_awesome_30_1);
auto dark_theme = new LvglTheme("dark");

View File

@ -16,7 +16,7 @@ dependencies:
78/esp-wifi-connect: ~2.5.2
78/esp-opus-encoder: ~2.4.1
78/esp-ml307: ~3.3.5
78/xiaozhi-fonts: ~1.5.2
78/xiaozhi-fonts: ~1.5.3
espressif/led_strip: ~3.0.1
espressif/esp_codec_dev: ~1.4.0
espressif/esp-sr: ~2.1.5

View File

@ -287,20 +287,18 @@ void McpServer::AddUserOnlyTools() {
#endif // HAVE_LVGL
// Assets download url
auto assets = Board::GetInstance().GetAssets();
if (assets) {
if (assets->partition_valid()) {
AddUserOnlyTool("self.assets.set_download_url", "Set the download url for the assets",
PropertyList({
Property("url", kPropertyTypeString)
}),
[assets](const PropertyList& properties) -> ReturnValue {
auto url = properties["url"].value<std::string>();
Settings settings("assets", true);
settings.SetString("download_url", url);
return true;
});
}
auto& assets = Assets::GetInstance();
if (assets.partition_valid()) {
AddUserOnlyTool("self.assets.set_download_url", "Set the download url for the assets",
PropertyList({
Property("url", kPropertyTypeString)
}),
[](const PropertyList& properties) -> ReturnValue {
auto url = properties["url"].value<std::string>();
Settings settings("assets", true);
settings.SetString("download_url", url);
return true;
});
}
}

7
partitions/v2/4m.csv Normal file
View File

@ -0,0 +1,7 @@
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x270000,
assets, data, spiffs, 0x280000, 0x180000,
1 # ESP-IDF Partition Table
2 # Name, Type, SubType, Offset, Size, Flags
3 nvs, data, nvs, 0x9000, 0x4000,
4 otadata, data, ota, 0xd000, 0x2000,
5 phy_init, data, phy, 0xf000, 0x1000,
6 factory, app, factory, 0x10000, 0x270000,
7 assets, data, spiffs, 0x280000, 0x180000,

615
scripts/build_default_assets.py Executable file
View File

@ -0,0 +1,615 @@
#!/usr/bin/env python3
"""
Build default assets based on configuration
This script reads configuration from sdkconfig and builds the appropriate assets.bin
for the current board configuration.
Usage:
./build_default_assets.py --sdkconfig <path> --builtin_text_font <font_name> \
--default_emoji_collection <collection_name> --output <output_path>
"""
import argparse
import io
import os
import shutil
import sys
import json
import struct
import math
from pathlib import Path
from datetime import datetime
# =============================================================================
# Pack model functions (from pack_model.py)
# =============================================================================
def struct_pack_string(string, max_len=None):
"""
pack string to binary data.
if max_len is None, max_len = len(string) + 1
else len(string) < max_len, the left will be padded by struct.pack('x')
"""
if max_len == None :
max_len = len(string)
else:
assert len(string) <= max_len
left_num = max_len - len(string)
out_bytes = None
for char in string:
if out_bytes == None:
out_bytes = struct.pack('b', ord(char))
else:
out_bytes += struct.pack('b', ord(char))
for i in range(left_num):
out_bytes += struct.pack('x')
return out_bytes
def read_data(filename):
"""Read binary data, like index and mndata"""
data = None
with open(filename, "rb") as f:
data = f.read()
return data
def pack_models(model_path, out_file="srmodels.bin"):
"""
Pack all models into one binary file by the following format:
{
model_num: int
model1_info: model_info_t
model2_info: model_info_t
...
model1_index,model1_data,model1_MODEL_INFO
model1_index,model1_data,model1_MODEL_INFO
...
}model_pack_t
{
model_name: char[32]
file_number: int
file1_name: char[32]
file1_start: int
file1_len: int
file2_name: char[32]
file2_start: int // data_len = info_start - data_start
file2_len: int
...
}model_info_t
"""
models = {}
file_num = 0
model_num = 0
for root, dirs, _ in os.walk(model_path):
for model_name in dirs:
models[model_name] = {}
model_dir = os.path.join(root, model_name)
model_num += 1
for _, _, files in os.walk(model_dir):
for file_name in files:
file_num += 1
file_path = os.path.join(model_dir, file_name)
models[model_name][file_name] = read_data(file_path)
model_num = len(models)
header_len = 4 + model_num*(32+4) + file_num*(32+4+4)
out_bin = struct.pack('I', model_num) # model number
data_bin = None
for key in models:
model_bin = struct_pack_string(key, 32) # + model name
model_bin += struct.pack('I', len(models[key])) # + file number in this model
for file_name in models[key]:
model_bin += struct_pack_string(file_name, 32) # + file name
if data_bin == None:
model_bin += struct.pack('I', header_len)
data_bin = models[key][file_name]
model_bin += struct.pack('I', len(models[key][file_name]))
else:
model_bin += struct.pack('I', header_len+len(data_bin))
data_bin += models[key][file_name]
model_bin += struct.pack('I', len(models[key][file_name]))
out_bin += model_bin
assert len(out_bin) == header_len
if data_bin != None:
out_bin += data_bin
out_file = os.path.join(model_path, out_file)
with open(out_file, "wb") as f:
f.write(out_bin)
# =============================================================================
# Build assets functions (from build.py)
# =============================================================================
def ensure_dir(directory):
"""Ensure directory exists, create if not"""
os.makedirs(directory, exist_ok=True)
def copy_file(src, dst):
"""Copy file"""
if os.path.exists(src):
shutil.copy2(src, dst)
print(f"Copied: {src} -> {dst}")
return True
else:
print(f"Warning: Source file does not exist: {src}")
return False
def copy_directory(src, dst):
"""Copy directory"""
if os.path.exists(src):
shutil.copytree(src, dst, dirs_exist_ok=True)
print(f"Copied directory: {src} -> {dst}")
return True
else:
print(f"Warning: Source directory does not exist: {src}")
return False
def process_wakenet_model(wakenet_model_dir, build_dir, assets_dir):
"""Process wakenet_model parameter"""
if not wakenet_model_dir:
return None
# Copy input directory to build directory
wakenet_build_dir = os.path.join(build_dir, "wakenet_model")
if os.path.exists(wakenet_build_dir):
shutil.rmtree(wakenet_build_dir)
copy_directory(wakenet_model_dir, os.path.join(wakenet_build_dir, os.path.basename(wakenet_model_dir)))
# Use pack_models function to generate srmodels.bin
srmodels_output = os.path.join(wakenet_build_dir, "srmodels.bin")
try:
pack_models(wakenet_build_dir, "srmodels.bin")
print(f"Generated: {srmodels_output}")
# Copy srmodels.bin to assets directory
copy_file(srmodels_output, os.path.join(assets_dir, "srmodels.bin"))
return "srmodels.bin"
except Exception as e:
print(f"Error: Failed to generate srmodels.bin: {e}")
return None
def process_text_font(text_font_file, assets_dir):
"""Process text_font parameter"""
if not text_font_file:
return None
# Copy input file to build/assets directory
font_filename = os.path.basename(text_font_file)
font_dst = os.path.join(assets_dir, font_filename)
if copy_file(text_font_file, font_dst):
return font_filename
return None
def process_emoji_collection(emoji_collection_dir, assets_dir):
"""Process emoji_collection parameter"""
if not emoji_collection_dir:
return []
emoji_list = []
# Copy each image from input directory to build/assets directory
for root, dirs, files in os.walk(emoji_collection_dir):
for file in files:
if file.lower().endswith(('.png', '.gif')):
# Copy file
src_file = os.path.join(root, file)
dst_file = os.path.join(assets_dir, file)
if copy_file(src_file, dst_file):
# Get filename without extension
filename_without_ext = os.path.splitext(file)[0]
# Add to emoji list
emoji_list.append({
"name": filename_without_ext,
"file": file
})
return emoji_list
def process_extra_files(extra_files_dir, assets_dir):
"""Process default_assets_extra_files parameter"""
if not extra_files_dir:
return []
if not os.path.exists(extra_files_dir):
print(f"Warning: Extra files directory not found: {extra_files_dir}")
return []
extra_files_list = []
# Copy each file from input directory to build/assets directory
for root, dirs, files in os.walk(extra_files_dir):
for file in files:
# Skip hidden files and directories
if file.startswith('.'):
continue
# Copy file
src_file = os.path.join(root, file)
dst_file = os.path.join(assets_dir, file)
if copy_file(src_file, dst_file):
extra_files_list.append(file)
if extra_files_list:
print(f"Processed {len(extra_files_list)} extra files from: {extra_files_dir}")
return extra_files_list
def generate_index_json(assets_dir, srmodels, text_font, emoji_collection, extra_files=None):
"""Generate index.json file"""
index_data = {
"version": 1
}
if srmodels:
index_data["srmodels"] = srmodels
if text_font:
index_data["text_font"] = text_font
if emoji_collection:
index_data["emoji_collection"] = emoji_collection
if extra_files:
index_data["extra_files"] = extra_files
# Write index.json
index_path = os.path.join(assets_dir, "index.json")
with open(index_path, 'w', encoding='utf-8') as f:
json.dump(index_data, f, indent=4, ensure_ascii=False)
print(f"Generated: {index_path}")
def generate_config_json(build_dir, assets_dir):
"""Generate config.json file"""
config_data = {
"include_path": os.path.join(build_dir, "include"),
"assets_path": assets_dir,
"image_file": os.path.join(build_dir, "output", "assets.bin"),
"lvgl_ver": "9.3.0",
"assets_size": "0x400000",
"support_format": ".png, .gif, .jpg, .bin, .json",
"name_length": "32",
"split_height": "0",
"support_qoi": False,
"support_spng": False,
"support_sjpg": False,
"support_sqoi": False,
"support_raw": False,
"support_raw_dither": False,
"support_raw_bgr": False
}
# Write config.json
config_path = os.path.join(build_dir, "config.json")
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(config_data, f, indent=4, ensure_ascii=False)
print(f"Generated: {config_path}")
return config_path
# =============================================================================
# Simplified SPIFFS assets generation (from spiffs_assets_gen.py)
# =============================================================================
def compute_checksum(data):
checksum = sum(data) & 0xFFFF
return checksum
def sort_key(filename):
basename, extension = os.path.splitext(filename)
return extension, basename
def pack_assets_simple(target_path, include_path, out_file, assets_path, max_name_len=32):
"""
Simplified version of pack_assets that handles basic file packing
"""
merged_data = bytearray()
file_info_list = []
skip_files = ['config.json']
# Ensure output directory exists
os.makedirs(os.path.dirname(out_file), exist_ok=True)
os.makedirs(include_path, exist_ok=True)
file_list = sorted(os.listdir(target_path), key=sort_key)
for filename in file_list:
if filename in skip_files:
continue
file_path = os.path.join(target_path, filename)
if not os.path.isfile(file_path):
continue
file_name = os.path.basename(file_path)
file_size = os.path.getsize(file_path)
file_info_list.append((file_name, len(merged_data), file_size, 0, 0))
# Add 0x5A5A prefix to merged_data
merged_data.extend(b'\x5A' * 2)
with open(file_path, 'rb') as bin_file:
bin_data = bin_file.read()
merged_data.extend(bin_data)
total_files = len(file_info_list)
mmap_table = bytearray()
for file_name, offset, file_size, width, height in file_info_list:
if len(file_name) > max_name_len:
print(f'Warning: "{file_name}" exceeds {max_name_len} bytes and will be truncated.')
fixed_name = file_name.ljust(max_name_len, '\0')[:max_name_len]
mmap_table.extend(fixed_name.encode('utf-8'))
mmap_table.extend(file_size.to_bytes(4, byteorder='little'))
mmap_table.extend(offset.to_bytes(4, byteorder='little'))
mmap_table.extend(width.to_bytes(2, byteorder='little'))
mmap_table.extend(height.to_bytes(2, byteorder='little'))
combined_data = mmap_table + merged_data
combined_checksum = compute_checksum(combined_data)
combined_data_length = len(combined_data).to_bytes(4, byteorder='little')
header_data = total_files.to_bytes(4, byteorder='little') + combined_checksum.to_bytes(4, byteorder='little')
final_data = header_data + combined_data_length + combined_data
with open(out_file, 'wb') as output_bin:
output_bin.write(final_data)
# Generate header file
current_year = datetime.now().year
asset_name = os.path.basename(assets_path)
header_file_path = os.path.join(include_path, f'mmap_generate_{asset_name}.h')
with open(header_file_path, 'w') as output_header:
output_header.write('/*\n')
output_header.write(' * SPDX-FileCopyrightText: 2022-{} Espressif Systems (Shanghai) CO LTD\n'.format(current_year))
output_header.write(' *\n')
output_header.write(' * SPDX-License-Identifier: Apache-2.0\n')
output_header.write(' */\n\n')
output_header.write('/**\n')
output_header.write(' * @file\n')
output_header.write(" * @brief This file was generated by esp_mmap_assets, don't modify it\n")
output_header.write(' */\n\n')
output_header.write('#pragma once\n\n')
output_header.write("#include \"esp_mmap_assets.h\"\n\n")
output_header.write(f'#define MMAP_{asset_name.upper()}_FILES {total_files}\n')
output_header.write(f'#define MMAP_{asset_name.upper()}_CHECKSUM 0x{combined_checksum:04X}\n\n')
output_header.write(f'enum MMAP_{asset_name.upper()}_LISTS {{\n')
for i, (file_name, _, _, _, _) in enumerate(file_info_list):
enum_name = file_name.replace('.', '_')
output_header.write(f' MMAP_{asset_name.upper()}_{enum_name.upper()} = {i}, /*!< {file_name} */\n')
output_header.write('};\n')
print(f'All files have been merged into {os.path.basename(out_file)}')
# =============================================================================
# Configuration and main functions
# =============================================================================
def read_wakenet_from_sdkconfig(sdkconfig_path):
"""
Read wakenet model from sdkconfig (based on movemodel.py logic)
Returns the wakenet model name or None if no wakenet is configured
"""
if not os.path.exists(sdkconfig_path):
print(f"Warning: sdkconfig file not found: {sdkconfig_path}")
return None
models = []
with io.open(sdkconfig_path, "r") as f:
for label in f:
label = label.strip("\n")
if 'CONFIG_SR_WN' in label and '#' not in label[0]:
if '_NONE' in label:
continue
if '=' in label:
label = label.split("=")[0]
if '_MULTI' in label:
label = label[:-6]
model_name = label.split("_SR_WN_")[-1].lower()
models.append(model_name)
# Return the first model found, or None if no models
return models[0] if models else None
def get_wakenet_model_path(model_name, esp_sr_model_path):
"""
Get the full path to the wakenet model directory
"""
if not model_name:
return None
wakenet_model_path = os.path.join(esp_sr_model_path, 'wakenet_model', model_name)
if os.path.exists(wakenet_model_path):
return wakenet_model_path
else:
print(f"Warning: Wakenet model directory not found: {wakenet_model_path}")
return None
def get_text_font_path(builtin_text_font, xiaozhi_fonts_path):
"""
Get the text font path if needed
Returns the font file path or None if no font is needed
"""
if not builtin_text_font or 'basic' not in builtin_text_font:
return None
# Convert from basic to common font name
# e.g., font_puhui_basic_16_4 -> font_puhui_common_16_4.bin
font_name = builtin_text_font.replace('basic', 'common') + '.bin'
font_path = os.path.join(xiaozhi_fonts_path, 'cbin', font_name)
if os.path.exists(font_path):
return font_path
else:
print(f"Warning: Font file not found: {font_path}")
return None
def get_emoji_collection_path(default_emoji_collection, xiaozhi_fonts_path):
"""
Get the emoji collection path if needed
Returns the emoji directory path or None if no emoji collection is needed
"""
if not default_emoji_collection:
return None
emoji_path = os.path.join(xiaozhi_fonts_path, 'png', default_emoji_collection)
if os.path.exists(emoji_path):
return emoji_path
else:
print(f"Warning: Emoji collection directory not found: {emoji_path}")
return None
def build_assets_integrated(wakenet_model_path, text_font_path, emoji_collection_path, extra_files_path, output_path):
"""
Build assets using integrated functions (no external dependencies)
"""
# Create temporary build directory
temp_build_dir = os.path.join(os.path.dirname(output_path), "temp_build")
assets_dir = os.path.join(temp_build_dir, "assets")
try:
# Clean and create directories
if os.path.exists(temp_build_dir):
shutil.rmtree(temp_build_dir)
ensure_dir(temp_build_dir)
ensure_dir(assets_dir)
print("Starting to build assets...")
# Process each component
srmodels = process_wakenet_model(wakenet_model_path, temp_build_dir, assets_dir) if wakenet_model_path else None
text_font = process_text_font(text_font_path, assets_dir) if text_font_path else None
emoji_collection = process_emoji_collection(emoji_collection_path, assets_dir) if emoji_collection_path else None
extra_files = process_extra_files(extra_files_path, assets_dir) if extra_files_path else None
# Generate index.json
generate_index_json(assets_dir, srmodels, text_font, emoji_collection, extra_files)
# Generate config.json for packing
config_path = generate_config_json(temp_build_dir, assets_dir)
# Load config and pack assets
with open(config_path, 'r') as f:
config_data = json.load(f)
# Use simplified packing function
include_path = config_data['include_path']
image_file = config_data['image_file']
pack_assets_simple(assets_dir, include_path, image_file, "assets", int(config_data['name_length']))
# Copy final assets.bin to output location
if os.path.exists(image_file):
shutil.copy2(image_file, output_path)
print(f"Successfully generated assets.bin: {output_path}")
# Show size information
total_size = os.path.getsize(output_path)
print(f"Assets file size: {total_size / 1024:.2f}K ({total_size} bytes)")
return True
else:
print(f"Error: Generated assets.bin not found: {image_file}")
return False
except Exception as e:
print(f"Error: Failed to build assets: {e}")
return False
finally:
# Clean up temporary directory
if os.path.exists(temp_build_dir):
shutil.rmtree(temp_build_dir)
def main():
parser = argparse.ArgumentParser(description='Build default assets based on configuration')
parser.add_argument('--sdkconfig', required=True, help='Path to sdkconfig file')
parser.add_argument('--builtin_text_font', help='Builtin text font name (e.g., font_puhui_basic_16_4)')
parser.add_argument('--emoji_collection', help='Default emoji collection name (e.g., emojis_32)')
parser.add_argument('--output', required=True, help='Output path for assets.bin')
parser.add_argument('--esp_sr_model_path', help='Path to ESP-SR model directory')
parser.add_argument('--xiaozhi_fonts_path', help='Path to xiaozhi-fonts component directory')
parser.add_argument('--extra_files', help='Path to extra files directory to be included in assets')
args = parser.parse_args()
# Get script directory (not needed anymore but keep for future use)
script_dir = os.path.dirname(os.path.abspath(__file__))
# Set default paths if not provided
if not args.esp_sr_model_path:
# Default ESP-SR model path relative to project root
project_root = os.path.dirname(os.path.dirname(script_dir))
args.esp_sr_model_path = os.path.join(project_root, "managed_components", "espressif__esp-sr", "model")
if not args.xiaozhi_fonts_path:
# Default xiaozhi-fonts path relative to project root
project_root = os.path.dirname(os.path.dirname(script_dir))
args.xiaozhi_fonts_path = os.path.join(project_root, "managed_components", "78__xiaozhi-fonts")
print("Building default assets...")
print(f" sdkconfig: {args.sdkconfig}")
print(f" builtin_text_font: {args.builtin_text_font}")
print(f" emoji_collection: {args.emoji_collection}")
print(f" output: {args.output}")
# Read wakenet model from sdkconfig
wakenet_model_name = read_wakenet_from_sdkconfig(args.sdkconfig)
wakenet_model_path = get_wakenet_model_path(wakenet_model_name, args.esp_sr_model_path)
# Get text font path if needed
text_font_path = get_text_font_path(args.builtin_text_font, args.xiaozhi_fonts_path)
# Get emoji collection path if needed
emoji_collection_path = get_emoji_collection_path(args.emoji_collection, args.xiaozhi_fonts_path)
# Get extra files path if provided
extra_files_path = args.extra_files
# Check if we have anything to build
if not wakenet_model_path and not text_font_path and not emoji_collection_path and not extra_files_path:
print("Warning: No assets to build (no wakenet, text font, emoji collection, or extra files)")
# Create an empty assets.bin file
os.makedirs(os.path.dirname(args.output), exist_ok=True)
with open(args.output, 'wb') as f:
pass # Create empty file
print(f"Created empty assets.bin: {args.output}")
return
# Build the assets
success = build_assets_integrated(wakenet_model_path, text_font_path, emoji_collection_path,
extra_files_path, args.output)
if not success:
sys.exit(1)
print("Build completed successfully!")
if __name__ == "__main__":
main()

View File

@ -106,7 +106,6 @@ def release(board_type, board_config, config_filename="config.json"):
with open("sdkconfig", "a") as f:
f.write("\n")
f.write("# Append by release.py\n")
f.write("CONFIG_FLASH_NONE_ASSETS=y\n")
for append in sdkconfig_append:
f.write(f"{append}\n")
# Build with macro BOARD_NAME defined to name

View File

@ -58,6 +58,7 @@ CONFIG_LV_USE_ASSERT_STYLE=y
CONFIG_LV_FONT_FMT_TXT_LARGE=y
CONFIG_LV_USE_FONT_COMPRESSED=n
CONFIG_LV_USE_FONT_PLACEHOLDER=n
CONFIG_LV_USE_LODEPNG=y
# Disable extra widgets to save flash size
CONFIG_LV_USE_ANIMIMG=n

View File

@ -23,5 +23,4 @@ CONFIG_SR_WN_WN9_NIHAOXIAOZHI_TTS=y
CONFIG_IDF_EXPERIMENTAL_FEATURES=y
# LVGL Graphics
CONFIG_LV_USE_LODEPNG=y
CONFIG_LV_USE_SNAPSHOT=y

View File

@ -26,5 +26,4 @@ CONFIG_SR_WN_WN9_NIHAOXIAOZHI_TTS=y
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096
# LVGL Graphics
CONFIG_LV_USE_LODEPNG=y
CONFIG_LV_USE_SNAPSHOT=y