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 <terrence.huang@tenclass.com>
This commit is contained in:
Xiaoxia
2025-09-04 15:41:28 +08:00
committed by GitHub
parent 3a3dfc003e
commit 83f6f8c703
196 changed files with 3918 additions and 4902 deletions

View File

@ -47,10 +47,20 @@ Display::~Display() {
if (network_label_ != nullptr) {
lv_obj_del(network_label_);
}
if (notification_label_ != nullptr) {
lv_obj_del(notification_label_);
}
if (status_label_ != nullptr) {
lv_obj_del(status_label_);
}
if (mute_label_ != nullptr) {
lv_obj_del(mute_label_);
}
if (battery_label_ != nullptr) {
lv_obj_del(battery_label_);
}
if (emotion_label_ != nullptr) {
lv_obj_del(emotion_label_);
}
if( low_battery_popup_ != nullptr ) {
@ -197,23 +207,23 @@ void Display::UpdateStatusBar(bool update_all) {
void Display::SetEmotion(const char* emotion) {
const char* utf8 = font_awesome_get_utf8(emotion);
if (utf8 != nullptr) {
SetIcon(utf8);
} else {
SetIcon(FONT_AWESOME_NEUTRAL);
}
}
void Display::SetIcon(const char* icon) {
DisplayLockGuard lock(this);
if (emotion_label_ == nullptr) {
return;
}
lv_label_set_text(emotion_label_, icon);
if (utf8 != nullptr) {
lv_label_set_text(emotion_label_, utf8);
} else {
lv_label_set_text(emotion_label_, FONT_AWESOME_NEUTRAL);
}
}
void Display::SetPreviewImage(const lv_img_dsc_t* image) {
// Do nothing
// Do nothing but free the image
if (image != nullptr) {
heap_caps_free((void*)image->data);
heap_caps_free((void*)image);
}
}
void Display::SetChatMessage(const char* role, const char* content) {
@ -238,4 +248,16 @@ void Display::SetPowerSaveMode(bool on) {
SetChatMessage("system", "");
SetEmotion("neutral");
}
}
}
void Display::UpdateStyle(const DisplayStyle& style) {
DisplayLockGuard lock(this);
if (style.text_font != nullptr) {
lv_obj_set_style_text_font(lv_screen_active(), style.text_font, 0);
style_.text_font = style.text_font;
}
if (style.emoji_collection != nullptr) {
delete style_.emoji_collection;
style_.emoji_collection = style.emoji_collection;
}
}

View File

@ -1,6 +1,8 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include "emoji_collection.h"
#include <lvgl.h>
#include <esp_timer.h>
#include <esp_log.h>
@ -9,10 +11,11 @@
#include <string>
#include <chrono>
struct DisplayFonts {
const lv_font_t* text_font = nullptr;
const lv_font_t* icon_font = nullptr;
const lv_font_t* emoji_font = nullptr;
struct DisplayStyle {
const lv_font_t* text_font;
const lv_font_t* icon_font;
EmojiCollection* emoji_collection;
};
class Display {
@ -25,11 +28,11 @@ public:
virtual void ShowNotification(const std::string &notification, int duration_ms = 3000);
virtual void SetEmotion(const char* emotion);
virtual void SetChatMessage(const char* role, const char* content);
virtual void SetIcon(const char* icon);
virtual void SetPreviewImage(const lv_img_dsc_t* image);
virtual void SetTheme(const std::string& theme_name);
virtual std::string GetTheme() { return current_theme_name_; }
virtual void UpdateStatusBar(bool update_all = false);
virtual void UpdateStyle(const DisplayStyle& style);
virtual void SetPowerSaveMode(bool on);
inline int width() const { return width_; }
@ -56,6 +59,7 @@ protected:
const char* network_icon_ = nullptr;
bool muted_ = false;
std::string current_theme_name_;
DisplayStyle style_;
std::chrono::system_clock::time_point last_status_update_time_;
esp_timer_handle_t notification_timer_ = nullptr;

View File

@ -0,0 +1,144 @@
#include "emoji_collection.h"
#include <esp_log.h>
#include <unordered_map>
#include <string>
#define TAG "EmojiCollection"
// These are declared in xiaozhi-fonts/src/font_emoji_32.c
extern const lv_image_dsc_t emoji_1f636_32; // neutral
extern const lv_image_dsc_t emoji_1f642_32; // happy
extern const lv_image_dsc_t emoji_1f606_32; // laughing
extern const lv_image_dsc_t emoji_1f602_32; // funny
extern const lv_image_dsc_t emoji_1f614_32; // sad
extern const lv_image_dsc_t emoji_1f620_32; // angry
extern const lv_image_dsc_t emoji_1f62d_32; // crying
extern const lv_image_dsc_t emoji_1f60d_32; // loving
extern const lv_image_dsc_t emoji_1f633_32; // embarrassed
extern const lv_image_dsc_t emoji_1f62f_32; // surprised
extern const lv_image_dsc_t emoji_1f631_32; // shocked
extern const lv_image_dsc_t emoji_1f914_32; // thinking
extern const lv_image_dsc_t emoji_1f609_32; // winking
extern const lv_image_dsc_t emoji_1f60e_32; // cool
extern const lv_image_dsc_t emoji_1f60c_32; // relaxed
extern const lv_image_dsc_t emoji_1f924_32; // delicious
extern const lv_image_dsc_t emoji_1f618_32; // kissy
extern const lv_image_dsc_t emoji_1f60f_32; // confident
extern const lv_image_dsc_t emoji_1f634_32; // sleepy
extern const lv_image_dsc_t emoji_1f61c_32; // silly
extern const lv_image_dsc_t emoji_1f644_32; // confused
const lv_img_dsc_t* Twemoji32::GetEmojiImage(const char* name) const {
static const std::unordered_map<std::string, const lv_img_dsc_t*> emoji_map = {
{"neutral", &emoji_1f636_32},
{"happy", &emoji_1f642_32},
{"laughing", &emoji_1f606_32},
{"funny", &emoji_1f602_32},
{"sad", &emoji_1f614_32},
{"angry", &emoji_1f620_32},
{"crying", &emoji_1f62d_32},
{"loving", &emoji_1f60d_32},
{"embarrassed", &emoji_1f633_32},
{"surprised", &emoji_1f62f_32},
{"shocked", &emoji_1f631_32},
{"thinking", &emoji_1f914_32},
{"winking", &emoji_1f609_32},
{"cool", &emoji_1f60e_32},
{"relaxed", &emoji_1f60c_32},
{"delicious", &emoji_1f924_32},
{"kissy", &emoji_1f618_32},
{"confident", &emoji_1f60f_32},
{"sleepy", &emoji_1f634_32},
{"silly", &emoji_1f61c_32},
{"confused", &emoji_1f644_32},
};
auto it = emoji_map.find(name);
if (it != emoji_map.end()) {
return it->second;
}
ESP_LOGW(TAG, "Emoji not found: %s", name);
return nullptr;
}
// These are declared in xiaozhi-fonts/src/font_emoji_64.c
extern const lv_image_dsc_t emoji_1f636_64; // neutral
extern const lv_image_dsc_t emoji_1f642_64; // happy
extern const lv_image_dsc_t emoji_1f606_64; // laughing
extern const lv_image_dsc_t emoji_1f602_64; // funny
extern const lv_image_dsc_t emoji_1f614_64; // sad
extern const lv_image_dsc_t emoji_1f620_64; // angry
extern const lv_image_dsc_t emoji_1f62d_64; // crying
extern const lv_image_dsc_t emoji_1f60d_64; // loving
extern const lv_image_dsc_t emoji_1f633_64; // embarrassed
extern const lv_image_dsc_t emoji_1f62f_64; // surprised
extern const lv_image_dsc_t emoji_1f631_64; // shocked
extern const lv_image_dsc_t emoji_1f914_64; // thinking
extern const lv_image_dsc_t emoji_1f609_64; // winking
extern const lv_image_dsc_t emoji_1f60e_64; // cool
extern const lv_image_dsc_t emoji_1f60c_64; // relaxed
extern const lv_image_dsc_t emoji_1f924_64; // delicious
extern const lv_image_dsc_t emoji_1f618_64; // kissy
extern const lv_image_dsc_t emoji_1f60f_64; // confident
extern const lv_image_dsc_t emoji_1f634_64; // sleepy
extern const lv_image_dsc_t emoji_1f61c_64; // silly
extern const lv_image_dsc_t emoji_1f644_64; // confused
const lv_img_dsc_t* Twemoji64::GetEmojiImage(const char* name) const {
static const std::unordered_map<std::string, const lv_img_dsc_t*> emoji_map = {
{"neutral", &emoji_1f636_64},
{"happy", &emoji_1f642_64},
{"laughing", &emoji_1f606_64},
{"funny", &emoji_1f602_64},
{"sad", &emoji_1f614_64},
{"angry", &emoji_1f620_64},
{"crying", &emoji_1f62d_64},
{"loving", &emoji_1f60d_64},
{"embarrassed", &emoji_1f633_64},
{"surprised", &emoji_1f62f_64},
{"shocked", &emoji_1f631_64},
{"thinking", &emoji_1f914_64},
{"winking", &emoji_1f609_64},
{"cool", &emoji_1f60e_64},
{"relaxed", &emoji_1f60c_64},
{"delicious", &emoji_1f924_64},
{"kissy", &emoji_1f618_64},
{"confident", &emoji_1f60f_64},
{"sleepy", &emoji_1f634_64},
{"silly", &emoji_1f61c_64},
{"confused", &emoji_1f644_64},
};
auto it = emoji_map.find(name);
if (it != emoji_map.end()) {
return it->second;
}
ESP_LOGW(TAG, "Emoji not found: %s", name);
return nullptr;
}
void CustomEmojiCollection::AddEmoji(const std::string& name, lv_img_dsc_t* image) {
emoji_collection_[name] = image;
}
const lv_img_dsc_t* CustomEmojiCollection::GetEmojiImage(const char* name) const {
auto it = emoji_collection_.find(name);
if (it != emoji_collection_.end()) {
return it->second;
}
ESP_LOGW(TAG, "Emoji not found: %s", name);
return nullptr;
}
CustomEmojiCollection::~CustomEmojiCollection() {
for (auto it = emoji_collection_.begin(); it != emoji_collection_.end(); ++it) {
delete it->second;
}
emoji_collection_.clear();
}

View File

@ -0,0 +1,36 @@
#ifndef EMOJI_COLLECTION_H
#define EMOJI_COLLECTION_H
#include <lvgl.h>
#include <map>
#include <string>
// Define interface for emoji collection
class EmojiCollection {
public:
virtual const lv_img_dsc_t* GetEmojiImage(const char* name) const = 0;
virtual ~EmojiCollection() = default;
};
class Twemoji32 : public EmojiCollection {
public:
virtual const lv_img_dsc_t* GetEmojiImage(const char* name) const override;
};
class Twemoji64 : public EmojiCollection {
public:
virtual const lv_img_dsc_t* GetEmojiImage(const char* name) const override;
};
class CustomEmojiCollection : public EmojiCollection {
private:
std::map<std::string, lv_img_dsc_t*> emoji_collection_;
public:
void AddEmoji(const std::string& name, lv_img_dsc_t* image);
virtual const lv_img_dsc_t* GetEmojiImage(const char* name) const override;
virtual ~CustomEmojiCollection();
};
#endif

View File

@ -31,11 +31,6 @@ void EspLogDisplay::SetEmotion(const char* emotion)
ESP_LOGW(TAG, "SetEmotion: %s", emotion);
}
void EspLogDisplay::SetIcon(const char* icon)
{
ESP_LOGW(TAG, "SetIcon: %s", icon);
}
void EspLogDisplay::SetChatMessage(const char* role, const char* content)
{
ESP_LOGW(TAG, "Role:%s", role);

View File

@ -14,8 +14,7 @@ public:
virtual void ShowNotification(const char* notification, int duration_ms = 3000);
virtual void ShowNotification(const std::string &notification, int duration_ms = 3000);
virtual void SetEmotion(const char* emotion) override;
virtual void SetChatMessage(const char* role, const char* content) override;
virtual void SetIcon(const char* icon) override;
virtual void SetChatMessage(const char* role, const char* content) override;
virtual inline void SetPreviewImage(const lv_img_dsc_t* image) override {}
virtual inline void SetTheme(const std::string& theme_name) override {}
virtual inline void UpdateStatusBar(bool update_all = false) override {}

View File

@ -67,10 +67,11 @@ const ThemeColors LIGHT_THEME = {
LV_FONT_DECLARE(font_awesome_30_4);
LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, DisplayFonts fonts, int width, int height)
: panel_io_(panel_io), panel_(panel), fonts_(fonts) {
LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, int width, int height, DisplayStyle style)
: panel_io_(panel_io), panel_(panel) {
width_ = width;
height_ = height;
style_ = style;
// Load theme from settings
Settings settings("display", false);
@ -82,12 +83,25 @@ LcdDisplay::LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_
} else if (current_theme_name_ == "light") {
current_theme_ = LIGHT_THEME;
}
// Create a timer to hide the preview image
esp_timer_create_args_t preview_timer_args = {
.callback = [](void* arg) {
LcdDisplay* display = static_cast<LcdDisplay*>(arg);
display->SetPreviewImage(nullptr);
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "preview_timer",
.skip_unhandled_events = false,
};
esp_timer_create(&preview_timer_args, &preview_timer_);
}
SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts)
: LcdDisplay(panel_io, panel, fonts, width, height) {
DisplayStyle style)
: LcdDisplay(panel_io, panel, width, height, style) {
// draw white
std::vector<uint16_t> buffer(width_, 0xFFFF);
@ -163,8 +177,8 @@ SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts)
: LcdDisplay(panel_io, panel, fonts, width, height) {
DisplayStyle style)
: LcdDisplay(panel_io, panel, width, height, style) {
// draw white
std::vector<uint16_t> buffer(width_, 0xFFFF);
@ -225,8 +239,8 @@ RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
MipiLcdDisplay::MipiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts)
: LcdDisplay(panel_io, panel, fonts, width, height) {
DisplayStyle style)
: LcdDisplay(panel_io, panel, width, height, style) {
// Set the display to on
ESP_LOGI(TAG, "Turning display on");
@ -241,16 +255,16 @@ MipiLcdDisplay::MipiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel
ESP_LOGI(TAG, "Adding LCD display");
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = panel_io,
.panel_handle = panel,
.control_handle = nullptr,
.buffer_size = static_cast<uint32_t>(width_ * 50),
.double_buffer = false,
.hres = static_cast<uint32_t>(width_),
.vres = static_cast<uint32_t>(height_),
.monochrome = false,
/* Rotation values must be same as used in esp_lcd for initial settings of the screen */
.rotation = {
.io_handle = panel_io,
.panel_handle = panel,
.control_handle = nullptr,
.buffer_size = static_cast<uint32_t>(width_ * 50),
.double_buffer = false,
.hres = static_cast<uint32_t>(width_),
.vres = static_cast<uint32_t>(height_),
.monochrome = false,
/* Rotation values must be same as used in esp_lcd for initial settings of the screen */
.rotation = {
.swap_xy = swap_xy,
.mirror_x = mirror_x,
.mirror_y = mirror_y,
@ -281,6 +295,12 @@ MipiLcdDisplay::MipiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel
}
LcdDisplay::~LcdDisplay() {
SetPreviewImage(nullptr);
if (preview_timer_ != nullptr) {
esp_timer_stop(preview_timer_);
esp_timer_delete(preview_timer_);
}
// 然后再清理 LVGL 对象
if (content_ != nullptr) {
lv_obj_del(content_);
@ -319,7 +339,7 @@ void LcdDisplay::SetupUI() {
DisplayLockGuard lock(this);
auto screen = lv_screen_active();
lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
lv_obj_set_style_text_font(screen, style_.text_font, 0);
lv_obj_set_style_text_color(screen, current_theme_.text, 0);
lv_obj_set_style_bg_color(screen, current_theme_.background, 0);
@ -374,12 +394,10 @@ void LcdDisplay::SetupUI() {
// 设置状态栏的内容垂直居中
lv_obj_set_flex_align(status_bar_, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
// 创建emotion_label_在状态栏最左侧
emotion_label_ = lv_label_create(status_bar_);
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
lv_obj_set_style_text_color(emotion_label_, current_theme_.text, 0);
lv_label_set_text(emotion_label_, FONT_AWESOME_MICROCHIP_AI);
lv_obj_set_style_margin_right(emotion_label_, 5, 0); // 添加右边距,与后面的元素分隔
network_label_ = lv_label_create(status_bar_);
lv_label_set_text(network_label_, "");
lv_obj_set_style_text_font(network_label_, style_.icon_font, 0);
lv_obj_set_style_text_color(network_label_, current_theme_.text, 0);
notification_label_ = lv_label_create(status_bar_);
lv_obj_set_flex_grow(notification_label_, 1);
@ -397,25 +415,19 @@ void LcdDisplay::SetupUI() {
mute_label_ = lv_label_create(status_bar_);
lv_label_set_text(mute_label_, "");
lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(mute_label_, style_.icon_font, 0);
lv_obj_set_style_text_color(mute_label_, current_theme_.text, 0);
network_label_ = lv_label_create(status_bar_);
lv_label_set_text(network_label_, "");
lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_color(network_label_, current_theme_.text, 0);
lv_obj_set_style_margin_left(network_label_, 5, 0); // 添加左边距,与前面的元素分隔
battery_label_ = lv_label_create(status_bar_);
lv_label_set_text(battery_label_, "");
lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(battery_label_, style_.icon_font, 0);
lv_obj_set_style_text_color(battery_label_, current_theme_.text, 0);
lv_obj_set_style_margin_left(battery_label_, 5, 0); // 添加左边距,与前面的元素分隔
low_battery_popup_ = lv_obj_create(screen);
lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, style_.text_font->line_height * 2);
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, -10);
lv_obj_set_style_bg_color(low_battery_popup_, current_theme_.low_battery, 0);
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
low_battery_label_ = lv_label_create(low_battery_popup_);
@ -423,6 +435,16 @@ void LcdDisplay::SetupUI() {
lv_obj_set_style_text_color(low_battery_label_, lv_color_white(), 0);
lv_obj_center(low_battery_label_);
lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
emoji_image_ = lv_img_create(screen);
lv_obj_align(emoji_image_, LV_ALIGN_TOP_MID, 0, style_.text_font->line_height + 10);
// Display AI logo while booting
emotion_label_ = lv_label_create(screen);
lv_obj_center(emotion_label_);
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
lv_obj_set_style_text_color(emotion_label_, current_theme_.text, 0);
lv_label_set_text(emotion_label_, FONT_AWESOME_MICROCHIP_AI);
}
#if CONFIG_IDF_TARGET_ESP32P4
#define MAX_MESSAGES 40
@ -435,9 +457,6 @@ void LcdDisplay::SetChatMessage(const char* role, const char* content) {
return;
}
//避免出现空的消息框
if(strlen(content) == 0) return;
// 检查消息数量是否超过限制
uint32_t child_count = lv_obj_get_child_cnt(content_);
if (child_count >= MAX_MESSAGES) {
@ -454,23 +473,33 @@ void LcdDisplay::SetChatMessage(const char* role, const char* content) {
}
// 折叠系统消息(如果是系统消息,检查最后一个消息是否也是系统消息)
if (strcmp(role, "system") == 0 && child_count > 0) {
// 获取最后一个消息容器
lv_obj_t* last_container = lv_obj_get_child(content_, child_count - 1);
if (last_container != nullptr && lv_obj_get_child_cnt(last_container) > 0) {
// 获取容器内的气泡
lv_obj_t* last_bubble = lv_obj_get_child(last_container, 0);
if (last_bubble != nullptr) {
// 检查气泡类型是否为系统消息
void* bubble_type_ptr = lv_obj_get_user_data(last_bubble);
if (bubble_type_ptr != nullptr && strcmp((const char*)bubble_type_ptr, "system") == 0) {
// 如果最后一个消息也是系统消息,则删除它
lv_obj_del(last_container);
if (strcmp(role, "system") == 0) {
if (child_count > 0) {
// 获取最后一个消息容器
lv_obj_t* last_container = lv_obj_get_child(content_, child_count - 1);
if (last_container != nullptr && lv_obj_get_child_cnt(last_container) > 0) {
// 获取容器内的气泡
lv_obj_t* last_bubble = lv_obj_get_child(last_container, 0);
if (last_bubble != nullptr) {
// 检查气泡类型是否为系统消息
void* bubble_type_ptr = lv_obj_get_user_data(last_bubble);
if (bubble_type_ptr != nullptr && strcmp((const char*)bubble_type_ptr, "system") == 0) {
// 如果最后一个消息也是系统消息,则删除它
lv_obj_del(last_container);
}
}
}
}
} else {
// 隐藏居中显示的 AI logo
lv_obj_add_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
}
//避免出现空的消息框
if(strlen(content) == 0) {
return;
}
// Create a message bubble
lv_obj_t* msg_bubble = lv_obj_create(content_);
lv_obj_set_style_radius(msg_bubble, 8, 0);
@ -484,7 +513,7 @@ void LcdDisplay::SetChatMessage(const char* role, const char* content) {
lv_label_set_text(msg_text, content);
// 计算文本实际宽度
lv_coord_t text_width = lv_txt_get_width(content, strlen(content), fonts_.text_font, 0);
lv_coord_t text_width = lv_txt_get_width(content, strlen(content), style_.text_font, 0);
// 计算气泡宽度
lv_coord_t max_width = LV_HOR_RES * 85 / 100 - 16; // 屏幕宽度的85%
@ -506,7 +535,6 @@ void LcdDisplay::SetChatMessage(const char* role, const char* content) {
// 设置消息文本的宽度
lv_obj_set_width(msg_text, bubble_width); // 减去padding
lv_label_set_long_mode(msg_text, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(msg_text, fonts_.text_font, 0);
// 设置气泡宽度
lv_obj_set_width(msg_bubble, bubble_width);
@ -636,41 +664,18 @@ void LcdDisplay::SetPreviewImage(const lv_img_dsc_t* img_dsc) {
// Create the image object inside the bubble
lv_obj_t* preview_image = lv_image_create(img_bubble);
// Copy the image descriptor and data to avoid source data changes
lv_img_dsc_t* copied_img_dsc = (lv_img_dsc_t*)heap_caps_malloc(sizeof(lv_img_dsc_t), MALLOC_CAP_8BIT);
if (copied_img_dsc == nullptr) {
ESP_LOGE(TAG, "Failed to allocate memory for image descriptor");
lv_obj_del(img_bubble);
return;
}
// Copy the header
copied_img_dsc->header = img_dsc->header;
copied_img_dsc->data_size = img_dsc->data_size;
// Copy the image data
uint8_t* copied_data = (uint8_t*)heap_caps_malloc(img_dsc->data_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (copied_data == nullptr) {
// Fallback to internal RAM if SPIRAM allocation fails
copied_data = (uint8_t*)heap_caps_malloc(img_dsc->data_size, MALLOC_CAP_8BIT);
}
if (copied_data == nullptr) {
ESP_LOGE(TAG, "Failed to allocate memory for image data (size: %lu bytes)", img_dsc->data_size);
heap_caps_free(copied_img_dsc);
lv_obj_del(img_bubble);
return;
}
memcpy(copied_data, img_dsc->data, img_dsc->data_size);
copied_img_dsc->data = copied_data;
// Calculate appropriate size for the image
lv_coord_t max_width = LV_HOR_RES * 70 / 100; // 70% of screen width
lv_coord_t max_height = LV_VER_RES * 50 / 100; // 50% of screen height
// Calculate zoom factor to fit within maximum dimensions
lv_coord_t img_width = copied_img_dsc->header.w;
lv_coord_t img_height = copied_img_dsc->header.h;
lv_coord_t img_width = img_dsc->header.w;
lv_coord_t img_height = img_dsc->header.h;
if (img_width == 0 || img_height == 0) {
img_width = max_width;
img_height = max_height;
ESP_LOGW(TAG, "Invalid image dimensions: %ld x %ld, using default dimensions: %ld x %ld", img_width, img_height, max_width, max_height);
}
lv_coord_t zoom_w = (max_width * 256) / img_width;
lv_coord_t zoom_h = (max_height * 256) / img_height;
@ -680,17 +685,17 @@ void LcdDisplay::SetPreviewImage(const lv_img_dsc_t* img_dsc) {
if (zoom > 256) zoom = 256;
// Set image properties
lv_image_set_src(preview_image, copied_img_dsc);
lv_image_set_src(preview_image, img_dsc);
lv_image_set_scale(preview_image, zoom);
// Add event handler to clean up copied data when image is deleted
lv_obj_add_event_cb(preview_image, [](lv_event_t* e) {
lv_img_dsc_t* copied_img_dsc = (lv_img_dsc_t*)lv_event_get_user_data(e);
if (copied_img_dsc != nullptr) {
heap_caps_free((void*)copied_img_dsc->data);
heap_caps_free(copied_img_dsc);
lv_img_dsc_t* img_dsc = (lv_img_dsc_t*)lv_event_get_user_data(e);
if (img_dsc != nullptr) {
heap_caps_free((void*)img_dsc->data);
heap_caps_free(img_dsc);
}
}, LV_EVENT_DELETE, (void*)copied_img_dsc);
}, LV_EVENT_DELETE, (void*)img_dsc);
// Calculate actual scaled image dimensions
lv_coord_t scaled_width = (img_width * zoom) / 256;
@ -718,7 +723,7 @@ void LcdDisplay::SetupUI() {
DisplayLockGuard lock(this);
auto screen = lv_screen_active();
lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
lv_obj_set_style_text_font(screen, style_.text_font, 0);
lv_obj_set_style_text_color(screen, current_theme_.text, 0);
lv_obj_set_style_bg_color(screen, current_theme_.background, 0);
@ -734,7 +739,7 @@ void LcdDisplay::SetupUI() {
/* Status bar */
status_bar_ = lv_obj_create(container_);
lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.text_font->line_height);
lv_obj_set_size(status_bar_, LV_HOR_RES, style_.text_font->line_height);
lv_obj_set_style_radius(status_bar_, 0, 0);
lv_obj_set_style_bg_color(status_bar_, current_theme_.background, 0);
lv_obj_set_style_text_color(status_bar_, current_theme_.text, 0);
@ -752,13 +757,22 @@ void LcdDisplay::SetupUI() {
lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN); // 垂直布局(从上到下)
lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY); // 子对象居中对齐,等距分布
emotion_label_ = lv_label_create(content_);
emoji_box_ = lv_obj_create(content_);
lv_obj_set_size(emoji_box_, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_bg_opa(emoji_box_, LV_OPA_TRANSP, 0);
lv_obj_set_style_pad_all(emoji_box_, 0, 0);
lv_obj_set_style_border_width(emoji_box_, 0, 0);
emotion_label_ = lv_label_create(emoji_box_);
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
lv_obj_set_style_text_color(emotion_label_, current_theme_.text, 0);
lv_label_set_text(emotion_label_, FONT_AWESOME_MICROCHIP_AI);
emoji_image_ = lv_img_create(emoji_box_);
lv_obj_center(emoji_image_);
preview_image_ = lv_image_create(content_);
lv_obj_set_size(preview_image_, width_ * 0.5, height_ * 0.5);
lv_obj_set_size(preview_image_, width_ / 2, height_ / 2);
lv_obj_align(preview_image_, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
@ -779,7 +793,7 @@ void LcdDisplay::SetupUI() {
network_label_ = lv_label_create(status_bar_);
lv_label_set_text(network_label_, "");
lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(network_label_, style_.icon_font, 0);
lv_obj_set_style_text_color(network_label_, current_theme_.text, 0);
notification_label_ = lv_label_create(status_bar_);
@ -795,20 +809,21 @@ void LcdDisplay::SetupUI() {
lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_color(status_label_, current_theme_.text, 0);
lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
mute_label_ = lv_label_create(status_bar_);
lv_label_set_text(mute_label_, "");
lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(mute_label_, style_.icon_font, 0);
lv_obj_set_style_text_color(mute_label_, current_theme_.text, 0);
battery_label_ = lv_label_create(status_bar_);
lv_label_set_text(battery_label_, "");
lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(battery_label_, style_.icon_font, 0);
lv_obj_set_style_text_color(battery_label_, current_theme_.text, 0);
low_battery_popup_ = lv_obj_create(screen);
lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, style_.text_font->line_height * 2);
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, -10);
lv_obj_set_style_bg_color(low_battery_popup_, current_theme_.low_battery, 0);
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
low_battery_label_ = lv_label_create(low_battery_popup_);
@ -821,109 +836,67 @@ void LcdDisplay::SetupUI() {
void LcdDisplay::SetPreviewImage(const lv_img_dsc_t* img_dsc) {
DisplayLockGuard lock(this);
if (preview_image_ == nullptr) {
ESP_LOGE(TAG, "Preview image is not initialized");
return;
}
auto old_src = (const lv_img_dsc_t*)lv_image_get_src(preview_image_);
if (old_src != nullptr) {
lv_image_set_src(preview_image_, nullptr);
heap_caps_free((void*)old_src->data);
heap_caps_free((void*)old_src);
}
if (img_dsc != nullptr) {
// 设置图片源并显示预览图片
lv_image_set_src(preview_image_, img_dsc);
if (img_dsc->header.w > 0) {
if (img_dsc->header.w > 0 && img_dsc->header.h > 0) {
// zoom factor 0.5
lv_image_set_scale(preview_image_, 128 * width_ / img_dsc->header.w);
}
// Hide emoji_box_
lv_obj_add_flag(emoji_box_, LV_OBJ_FLAG_HIDDEN);
lv_obj_remove_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
// 隐藏emotion_label_
if (emotion_label_ != nullptr) {
lv_obj_add_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
}
esp_timer_stop(preview_timer_);
ESP_ERROR_CHECK(esp_timer_start_once(preview_timer_, PREVIEW_IMAGE_DURATION_MS * 1000));
} else {
// 隐藏预览图片并显示emotion_label_
esp_timer_stop(preview_timer_);
lv_obj_remove_flag(emoji_box_, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
if (emotion_label_ != nullptr) {
lv_obj_remove_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
}
}
}
#endif
void LcdDisplay::SetEmotion(const char* emotion) {
struct Emotion {
const char* icon;
const char* text;
};
if (emoji_image_ == nullptr) {
return;
}
static const std::vector<Emotion> emotions = {
{"😶", "neutral"},
{"🙂", "happy"},
{"😆", "laughing"},
{"😂", "funny"},
{"😔", "sad"},
{"😠", "angry"},
{"😭", "crying"},
{"😍", "loving"},
{"😳", "embarrassed"},
{"😯", "surprised"},
{"😱", "shocked"},
{"🤔", "thinking"},
{"😉", "winking"},
{"😎", "cool"},
{"😌", "relaxed"},
{"🤤", "delicious"},
{"😘", "kissy"},
{"😏", "confident"},
{"😴", "sleepy"},
{"😜", "silly"},
{"🙄", "confused"}
};
// 查找匹配的表情
std::string_view emotion_view(emotion);
auto it = std::find_if(emotions.begin(), emotions.end(),
[&emotion_view](const Emotion& e) { return e.text == emotion_view; });
if (fonts_.emoji_font == nullptr || it == emotions.end()) {
auto img_dsc = style_.emoji_collection != nullptr ? style_.emoji_collection->GetEmojiImage(emotion) : nullptr;
if (img_dsc == nullptr) {
const char* utf8 = font_awesome_get_utf8(emotion);
if (utf8 != nullptr) {
SetIcon(utf8);
if (utf8 != nullptr && emotion_label_ != nullptr) {
DisplayLockGuard lock(this);
lv_label_set_text(emotion_label_, utf8);
lv_obj_remove_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(emoji_image_, LV_OBJ_FLAG_HIDDEN);
}
return;
}
DisplayLockGuard lock(this);
if (emotion_label_ == nullptr) {
return;
}
// 如果找到匹配的表情就显示对应图标否则显示默认的neutral表情
lv_obj_set_style_text_font(emotion_label_, fonts_.emoji_font, 0);
if (it != emotions.end()) {
lv_label_set_text(emotion_label_, it->icon);
lv_image_set_src(emoji_image_, img_dsc);
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
// Wechat message style中如果emotion是neutral则隐藏emoji_image_
if (strcmp(emotion, "neutral") == 0) {
lv_obj_add_flag(emoji_image_, LV_OBJ_FLAG_HIDDEN);
} else {
lv_label_set_text(emotion_label_, "😶");
}
#if !CONFIG_USE_WECHAT_MESSAGE_STYLE
// 显示emotion_label_隐藏preview_image_
lv_obj_remove_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
if (preview_image_ != nullptr) {
lv_obj_add_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
}
#endif
}
void LcdDisplay::SetIcon(const char* icon) {
DisplayLockGuard lock(this);
if (emotion_label_ == nullptr) {
return;
}
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
lv_label_set_text(emotion_label_, icon);
#if !CONFIG_USE_WECHAT_MESSAGE_STYLE
// 显示emotion_label_隐藏preview_image_
lv_obj_remove_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
if (preview_image_ != nullptr) {
lv_obj_add_flag(preview_image_, LV_OBJ_FLAG_HIDDEN);
lv_obj_remove_flag(emoji_image_, LV_OBJ_FLAG_HIDDEN);
}
#else
// 显示emoji_image_隐藏emotion_label_, preview_image_
lv_obj_remove_flag(emoji_image_, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
#endif
}

View File

@ -8,6 +8,9 @@
#include <font_emoji.h>
#include <atomic>
#include <memory>
#define PREVIEW_IMAGE_DURATION_MS 5000
// Theme color structure
struct ThemeColors {
@ -34,8 +37,10 @@ protected:
lv_obj_t* container_ = nullptr;
lv_obj_t* side_bar_ = nullptr;
lv_obj_t* preview_image_ = nullptr;
lv_obj_t* emoji_image_ = nullptr;
lv_obj_t* emoji_box_ = nullptr;
esp_timer_handle_t preview_timer_ = nullptr;
DisplayFonts fonts_;
ThemeColors current_theme_;
void SetupUI();
@ -44,12 +49,11 @@ protected:
protected:
// 添加protected构造函数
LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, DisplayFonts fonts, int width, int height);
LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, int width, int height, DisplayStyle style);
public:
~LcdDisplay();
virtual void SetEmotion(const char* emotion) override;
virtual void SetIcon(const char* icon) override;
virtual void SetPreviewImage(const lv_img_dsc_t* img_dsc) override;
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
virtual void SetChatMessage(const char* role, const char* content) override;
@ -59,13 +63,22 @@ public:
virtual void SetTheme(const std::string& theme_name) override;
};
// SPI LCD显示器
class SpiLcdDisplay : public LcdDisplay {
public:
SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayStyle style);
};
// RGB LCD显示器
class RgbLcdDisplay : public LcdDisplay {
public:
RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts);
DisplayStyle style);
};
// MIPI LCD显示器
@ -74,33 +87,7 @@ public:
MipiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts);
DisplayStyle style);
};
// // SPI LCD显示器
class SpiLcdDisplay : public LcdDisplay {
public:
SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts);
};
// QSPI LCD显示器
class QspiLcdDisplay : public LcdDisplay {
public:
QspiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts);
};
// MCU8080 LCD显示器
class Mcu8080LcdDisplay : public LcdDisplay {
public:
Mcu8080LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, int offset_x, int offset_y,
bool mirror_x, bool mirror_y, bool swap_xy,
DisplayFonts fonts);
};
#endif // LCD_DISPLAY_H

View File

@ -14,10 +14,11 @@
LV_FONT_DECLARE(font_awesome_30_1);
OledDisplay::OledDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
int width, int height, bool mirror_x, bool mirror_y, DisplayFonts fonts)
: panel_io_(panel_io), panel_(panel), fonts_(fonts) {
int width, int height, bool mirror_x, bool mirror_y, DisplayStyle style)
: panel_io_(panel_io), panel_(panel) {
width_ = width;
height_ = height;
style_ = style;
ESP_LOGI(TAG, "Initialize LVGL");
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
@ -120,7 +121,7 @@ void OledDisplay::SetupUI_128x64() {
DisplayLockGuard lock(this);
auto screen = lv_screen_active();
lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
lv_obj_set_style_text_font(screen, style_.text_font, 0);
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
/* Container */
@ -191,7 +192,7 @@ void OledDisplay::SetupUI_128x64() {
network_label_ = lv_label_create(status_bar_);
lv_label_set_text(network_label_, "");
lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(network_label_, style_.icon_font, 0);
notification_label_ = lv_label_create(status_bar_);
lv_obj_set_flex_grow(notification_label_, 1);
@ -206,15 +207,15 @@ void OledDisplay::SetupUI_128x64() {
mute_label_ = lv_label_create(status_bar_);
lv_label_set_text(mute_label_, "");
lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(mute_label_, style_.icon_font, 0);
battery_label_ = lv_label_create(status_bar_);
lv_label_set_text(battery_label_, "");
lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(battery_label_, style_.icon_font, 0);
low_battery_popup_ = lv_obj_create(screen);
lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, style_.text_font->line_height * 2);
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
lv_obj_set_style_bg_color(low_battery_popup_, lv_color_black(), 0);
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
@ -229,7 +230,7 @@ void OledDisplay::SetupUI_128x32() {
DisplayLockGuard lock(this);
auto screen = lv_screen_active();
lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
lv_obj_set_style_text_font(screen, style_.text_font, 0);
/* Container */
container_ = lv_obj_create(screen);
@ -282,15 +283,15 @@ void OledDisplay::SetupUI_128x32() {
mute_label_ = lv_label_create(status_bar_);
lv_label_set_text(mute_label_, "");
lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(mute_label_, style_.icon_font, 0);
network_label_ = lv_label_create(status_bar_);
lv_label_set_text(network_label_, "");
lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(network_label_, style_.icon_font, 0);
battery_label_ = lv_label_create(status_bar_);
lv_label_set_text(battery_label_, "");
lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_font(battery_label_, style_.icon_font, 0);
chat_message_label_ = lv_label_create(side_bar_);
lv_obj_set_size(chat_message_label_, width_ - 32, LV_SIZE_CONTENT);

View File

@ -6,6 +6,7 @@
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
class OledDisplay : public Display {
private:
esp_lcd_panel_io_handle_t panel_io_ = nullptr;
@ -17,8 +18,7 @@ private:
lv_obj_t* content_right_ = nullptr;
lv_obj_t* container_ = nullptr;
lv_obj_t* side_bar_ = nullptr;
DisplayFonts fonts_;
DisplayStyle style_;
virtual bool Lock(int timeout_ms = 0) override;
virtual void Unlock() override;
@ -28,7 +28,7 @@ private:
public:
OledDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, int width, int height, bool mirror_x, bool mirror_y,
DisplayFonts fonts);
DisplayStyle style);
~OledDisplay();
virtual void SetChatMessage(const char* role, const char* content) override;