2025-01-05 21:20:30 +08:00
|
|
|
|
#include "lcd_display.h"
|
2024-11-06 10:06:05 +08:00
|
|
|
|
|
2025-03-05 09:37:13 +08:00
|
|
|
|
#include <vector>
|
2025-02-03 23:43:07 +08:00
|
|
|
|
#include <font_awesome_symbols.h>
|
2024-11-06 10:06:05 +08:00
|
|
|
|
#include <esp_log.h>
|
|
|
|
|
|
#include <esp_err.h>
|
2025-02-03 23:43:07 +08:00
|
|
|
|
#include <esp_lvgl_port.h>
|
2025-02-18 19:33:07 +08:00
|
|
|
|
#include "assets/lang_config.h"
|
2025-03-18 00:47:35 +08:00
|
|
|
|
#include <cstring>
|
2025-02-14 01:15:10 +08:00
|
|
|
|
|
2025-01-19 04:29:23 +08:00
|
|
|
|
#include "board.h"
|
2024-11-06 10:06:05 +08:00
|
|
|
|
|
2025-01-05 21:20:30 +08:00
|
|
|
|
#define TAG "LcdDisplay"
|
2024-11-06 10:06:05 +08:00
|
|
|
|
|
2025-01-23 13:50:22 +08:00
|
|
|
|
LV_FONT_DECLARE(font_awesome_30_4);
|
2024-11-30 19:00:05 +08:00
|
|
|
|
|
2025-02-24 11:07:28 +08:00
|
|
|
|
SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
|
2025-01-23 13:50:22 +08:00
|
|
|
|
int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
|
2025-01-24 03:00:50 +08:00
|
|
|
|
DisplayFonts fonts)
|
2025-03-05 09:37:13 +08:00
|
|
|
|
: LcdDisplay(panel_io, panel, fonts) {
|
2024-11-06 10:06:05 +08:00
|
|
|
|
width_ = width;
|
|
|
|
|
|
height_ = height;
|
|
|
|
|
|
|
|
|
|
|
|
// draw white
|
|
|
|
|
|
std::vector<uint16_t> buffer(width_, 0xFFFF);
|
|
|
|
|
|
for (int y = 0; y < height_; y++) {
|
|
|
|
|
|
esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set the display to on
|
|
|
|
|
|
ESP_LOGI(TAG, "Turning display on");
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
|
|
|
|
|
|
|
2024-11-30 19:00:05 +08:00
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL library");
|
|
|
|
|
|
lv_init();
|
2025-02-03 23:43:07 +08:00
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL port");
|
|
|
|
|
|
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
2025-03-05 09:37:13 +08:00
|
|
|
|
port_cfg.task_priority = 1;
|
2025-02-03 23:43:07 +08:00
|
|
|
|
lvgl_port_init(&port_cfg);
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Adding LCD screen");
|
|
|
|
|
|
const lvgl_port_display_cfg_t display_cfg = {
|
|
|
|
|
|
.io_handle = panel_io_,
|
|
|
|
|
|
.panel_handle = panel_,
|
|
|
|
|
|
.control_handle = nullptr,
|
|
|
|
|
|
.buffer_size = static_cast<uint32_t>(width_ * 10),
|
2025-02-08 13:56:27 +08:00
|
|
|
|
.double_buffer = false,
|
2025-02-03 23:43:07 +08:00
|
|
|
|
.trans_size = 0,
|
|
|
|
|
|
.hres = static_cast<uint32_t>(width_),
|
|
|
|
|
|
.vres = static_cast<uint32_t>(height_),
|
|
|
|
|
|
.monochrome = false,
|
|
|
|
|
|
.rotation = {
|
|
|
|
|
|
.swap_xy = swap_xy,
|
|
|
|
|
|
.mirror_x = mirror_x,
|
|
|
|
|
|
.mirror_y = mirror_y,
|
|
|
|
|
|
},
|
|
|
|
|
|
.color_format = LV_COLOR_FORMAT_RGB565,
|
|
|
|
|
|
.flags = {
|
|
|
|
|
|
.buff_dma = 1,
|
|
|
|
|
|
.buff_spiram = 0,
|
|
|
|
|
|
.sw_rotate = 0,
|
|
|
|
|
|
.swap_bytes = 1,
|
|
|
|
|
|
.full_refresh = 0,
|
|
|
|
|
|
.direct_mode = 0,
|
2024-12-03 12:33:33 +08:00
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-02-03 23:43:07 +08:00
|
|
|
|
display_ = lvgl_port_add_disp(&display_cfg);
|
|
|
|
|
|
if (display_ == nullptr) {
|
|
|
|
|
|
ESP_LOGE(TAG, "Failed to add display");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-04 14:24:03 +08:00
|
|
|
|
if (offset_x != 0 || offset_y != 0) {
|
|
|
|
|
|
lv_display_set_offset(display_, offset_x, offset_y);
|
|
|
|
|
|
}
|
2024-11-06 10:06:05 +08:00
|
|
|
|
|
2024-11-18 06:17:39 +08:00
|
|
|
|
SetupUI();
|
2024-11-06 10:06:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-24 11:07:28 +08:00
|
|
|
|
// RGB LCD实现
|
|
|
|
|
|
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)
|
2025-03-05 09:37:13 +08:00
|
|
|
|
: LcdDisplay(panel_io, panel, fonts) {
|
2025-02-24 11:07:28 +08:00
|
|
|
|
width_ = width;
|
|
|
|
|
|
height_ = height;
|
|
|
|
|
|
|
|
|
|
|
|
// draw white
|
|
|
|
|
|
std::vector<uint16_t> buffer(width_, 0xFFFF);
|
|
|
|
|
|
for (int y = 0; y < height_; y++) {
|
|
|
|
|
|
esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL library");
|
|
|
|
|
|
lv_init();
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL port");
|
|
|
|
|
|
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
2025-03-05 09:37:13 +08:00
|
|
|
|
port_cfg.task_priority = 1;
|
2025-02-24 11:07:28 +08:00
|
|
|
|
lvgl_port_init(&port_cfg);
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Adding LCD screen");
|
|
|
|
|
|
const lvgl_port_display_cfg_t display_cfg = {
|
|
|
|
|
|
.io_handle = panel_io_,
|
|
|
|
|
|
.panel_handle = panel_,
|
|
|
|
|
|
.buffer_size = static_cast<uint32_t>(width_ * 10),
|
|
|
|
|
|
.double_buffer = true,
|
|
|
|
|
|
.hres = static_cast<uint32_t>(width_),
|
|
|
|
|
|
.vres = static_cast<uint32_t>(height_),
|
|
|
|
|
|
.rotation = {
|
|
|
|
|
|
.swap_xy = swap_xy,
|
|
|
|
|
|
.mirror_x = mirror_x,
|
|
|
|
|
|
.mirror_y = mirror_y,
|
|
|
|
|
|
},
|
|
|
|
|
|
.flags = {
|
|
|
|
|
|
.buff_dma = 1,
|
|
|
|
|
|
.swap_bytes = 0,
|
|
|
|
|
|
.full_refresh = 1,
|
|
|
|
|
|
.direct_mode = 1,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const lvgl_port_display_rgb_cfg_t rgb_cfg = {
|
|
|
|
|
|
.flags = {
|
|
|
|
|
|
.bb_mode = true,
|
|
|
|
|
|
.avoid_tearing = true,
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
display_ = lvgl_port_add_disp_rgb(&display_cfg, &rgb_cfg);
|
|
|
|
|
|
if (display_ == nullptr) {
|
|
|
|
|
|
ESP_LOGE(TAG, "Failed to add RGB display");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (offset_x != 0 || offset_y != 0) {
|
|
|
|
|
|
lv_display_set_offset(display_, offset_x, offset_y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SetupUI();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-05 21:20:30 +08:00
|
|
|
|
LcdDisplay::~LcdDisplay() {
|
2025-02-03 23:43:07 +08:00
|
|
|
|
// 然后再清理 LVGL 对象
|
2024-11-18 06:17:39 +08:00
|
|
|
|
if (content_ != nullptr) {
|
|
|
|
|
|
lv_obj_del(content_);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (status_bar_ != nullptr) {
|
|
|
|
|
|
lv_obj_del(status_bar_);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (side_bar_ != nullptr) {
|
|
|
|
|
|
lv_obj_del(side_bar_);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (container_ != nullptr) {
|
|
|
|
|
|
lv_obj_del(container_);
|
|
|
|
|
|
}
|
2025-02-03 23:43:07 +08:00
|
|
|
|
if (display_ != nullptr) {
|
|
|
|
|
|
lv_display_delete(display_);
|
|
|
|
|
|
}
|
2024-11-18 06:17:39 +08:00
|
|
|
|
|
2024-11-06 10:06:05 +08:00
|
|
|
|
if (panel_ != nullptr) {
|
|
|
|
|
|
esp_lcd_panel_del(panel_);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (panel_io_ != nullptr) {
|
|
|
|
|
|
esp_lcd_panel_io_del(panel_io_);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-05 21:20:30 +08:00
|
|
|
|
bool LcdDisplay::Lock(int timeout_ms) {
|
2025-02-03 23:43:07 +08:00
|
|
|
|
return lvgl_port_lock(timeout_ms);
|
2024-11-06 10:06:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-05 21:20:30 +08:00
|
|
|
|
void LcdDisplay::Unlock() {
|
2025-02-03 23:43:07 +08:00
|
|
|
|
lvgl_port_unlock();
|
2024-11-06 10:06:05 +08:00
|
|
|
|
}
|
2024-11-18 06:17:39 +08:00
|
|
|
|
|
2025-03-18 00:47:35 +08:00
|
|
|
|
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
|
|
|
|
|
|
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_color(screen, lv_color_black(), 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Container */
|
|
|
|
|
|
container_ = lv_obj_create(screen);
|
|
|
|
|
|
lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
|
|
|
|
|
|
lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
|
|
|
|
|
|
lv_obj_set_style_pad_all(container_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_border_width(container_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_row(container_, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Status bar */
|
|
|
|
|
|
status_bar_ = lv_obj_create(container_);
|
|
|
|
|
|
lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.emoji_font->line_height);
|
|
|
|
|
|
lv_obj_set_style_radius(status_bar_, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Content - Chat area */
|
|
|
|
|
|
content_ = lv_obj_create(container_);
|
|
|
|
|
|
lv_obj_set_style_radius(content_, 0, 0);
|
|
|
|
|
|
lv_obj_set_width(content_, LV_HOR_RES);
|
|
|
|
|
|
lv_obj_set_flex_grow(content_, 1);
|
|
|
|
|
|
lv_obj_set_style_pad_all(content_, 5, 0);
|
|
|
|
|
|
lv_obj_set_style_bg_color(content_, lv_color_hex(0xE0E0E0), 0); // Light gray background like WeChat
|
|
|
|
|
|
|
|
|
|
|
|
// Enable scrolling for chat content
|
|
|
|
|
|
lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
|
|
|
|
|
|
lv_obj_set_scroll_dir(content_, LV_DIR_VER);
|
|
|
|
|
|
|
|
|
|
|
|
// Create a flex container for chat messages
|
|
|
|
|
|
lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN);
|
|
|
|
|
|
lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
|
|
|
|
|
|
lv_obj_set_style_pad_row(content_, 10, 0); // Space between messages
|
|
|
|
|
|
|
|
|
|
|
|
// We'll create chat messages dynamically in SetChatMessage
|
|
|
|
|
|
chat_message_label_ = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
/* Status bar */
|
|
|
|
|
|
lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
|
|
|
|
|
|
lv_obj_set_style_pad_all(status_bar_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_border_width(status_bar_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_column(status_bar_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_left(status_bar_, 2, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_right(status_bar_, 2, 0);
|
|
|
|
|
|
lv_obj_set_scrollbar_mode(status_bar_, LV_SCROLLBAR_MODE_OFF);
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
notification_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_obj_set_flex_grow(notification_label_, 1);
|
|
|
|
|
|
lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
|
|
|
|
|
|
lv_label_set_text(notification_label_, "");
|
|
|
|
|
|
lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
|
|
|
|
|
|
|
|
|
|
|
|
status_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_obj_set_flex_grow(status_label_, 1);
|
|
|
|
|
|
lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
|
|
|
|
|
lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 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);
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建emotion_label_在状态栏最右侧
|
|
|
|
|
|
emotion_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
|
|
|
|
|
|
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
|
|
|
|
|
|
lv_obj_set_style_margin_left(emotion_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_style_bg_color(low_battery_popup_, lv_color_black(), 0);
|
|
|
|
|
|
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
|
|
|
|
|
|
lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
|
|
|
|
|
|
lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_MESSAGES 50
|
|
|
|
|
|
void LcdDisplay::SetChatMessage(const char* role, const char* content) {
|
|
|
|
|
|
DisplayLockGuard lock(this);
|
|
|
|
|
|
if (content_ == nullptr) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//避免出现空的消息框
|
|
|
|
|
|
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);
|
|
|
|
|
|
lv_obj_set_scrollbar_mode(msg_bubble, LV_SCROLLBAR_MODE_OFF);
|
|
|
|
|
|
lv_obj_set_style_border_width(msg_bubble, 1, 0);
|
|
|
|
|
|
lv_obj_set_style_border_color(msg_bubble, lv_color_hex(0xE0E0E0), 0);
|
|
|
|
|
|
lv_obj_set_style_pad_all(msg_bubble, 8, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Create the message text
|
|
|
|
|
|
lv_obj_t* msg_text = lv_label_create(msg_bubble);
|
|
|
|
|
|
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 max_width = LV_HOR_RES * 85 / 100 - 16; // 屏幕宽度的85%
|
|
|
|
|
|
lv_coord_t min_width = 20;
|
|
|
|
|
|
lv_coord_t bubble_width;
|
|
|
|
|
|
|
|
|
|
|
|
// 确保文本宽度不小于最小宽度
|
|
|
|
|
|
if (text_width < min_width) {
|
|
|
|
|
|
text_width = min_width;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果文本宽度小于最大宽度,使用文本宽度
|
|
|
|
|
|
if (text_width < max_width) {
|
|
|
|
|
|
bubble_width = text_width;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
bubble_width = max_width;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置消息文本的宽度
|
|
|
|
|
|
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);
|
|
|
|
|
|
lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
// Set alignment and style based on message role
|
|
|
|
|
|
if (strcmp(role, "user") == 0) {
|
|
|
|
|
|
// User messages are right-aligned with green background
|
|
|
|
|
|
lv_obj_set_style_bg_color(msg_bubble, lv_color_hex(0x95EC69), 0); // WeChat green
|
|
|
|
|
|
// Set text color for contrast
|
|
|
|
|
|
lv_obj_set_style_text_color(msg_text, lv_color_black(), 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Set appropriate width for content
|
|
|
|
|
|
lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
// Add some margin
|
|
|
|
|
|
lv_obj_set_style_margin_right(msg_bubble, 10, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Don't grow
|
|
|
|
|
|
lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
|
|
|
|
|
|
} else if (strcmp(role, "assistant") == 0) {
|
|
|
|
|
|
// Assistant messages are left-aligned with white background
|
|
|
|
|
|
lv_obj_set_style_bg_color(msg_bubble, lv_color_white(), 0);
|
|
|
|
|
|
// Set text color for contrast
|
|
|
|
|
|
lv_obj_set_style_text_color(msg_text, lv_color_black(), 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Set appropriate width for content
|
|
|
|
|
|
lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
// Add some margin
|
|
|
|
|
|
lv_obj_set_style_margin_left(msg_bubble, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Don't grow
|
|
|
|
|
|
lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
|
|
|
|
|
|
} else if (strcmp(role, "system") == 0) {
|
|
|
|
|
|
// System messages are center-aligned with light gray background
|
|
|
|
|
|
lv_obj_set_style_bg_color(msg_bubble, lv_color_hex(0xE0E0E0), 0); // 浅灰色背景
|
|
|
|
|
|
// Set text color for contrast
|
|
|
|
|
|
lv_obj_set_style_text_color(msg_text, lv_color_hex(0x666666), 0); // 深灰色文字
|
|
|
|
|
|
|
|
|
|
|
|
// Set appropriate width for content
|
|
|
|
|
|
lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
// Don't grow
|
|
|
|
|
|
lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a full-width container for user messages to ensure right alignment
|
|
|
|
|
|
if (strcmp(role, "user") == 0) {
|
|
|
|
|
|
// Create a full-width container
|
|
|
|
|
|
lv_obj_t* container = lv_obj_create(content_);
|
|
|
|
|
|
lv_obj_set_width(container, LV_HOR_RES);
|
|
|
|
|
|
lv_obj_set_height(container, LV_SIZE_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
// Make container transparent and borderless
|
|
|
|
|
|
lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
|
|
|
|
|
|
lv_obj_set_style_border_width(container, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_all(container, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Move the message bubble into this container
|
|
|
|
|
|
lv_obj_set_parent(msg_bubble, container);
|
|
|
|
|
|
|
|
|
|
|
|
// Right align the bubble in the container
|
|
|
|
|
|
lv_obj_align(msg_bubble, LV_ALIGN_RIGHT_MID, -10, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Auto-scroll to this container
|
|
|
|
|
|
lv_obj_scroll_to_view_recursive(container, LV_ANIM_ON);
|
|
|
|
|
|
} else if (strcmp(role, "system") == 0) {
|
|
|
|
|
|
// 为系统消息创建全宽容器以确保居中对齐
|
|
|
|
|
|
lv_obj_t* container = lv_obj_create(content_);
|
|
|
|
|
|
lv_obj_set_width(container, LV_HOR_RES);
|
|
|
|
|
|
lv_obj_set_height(container, LV_SIZE_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
// 使容器透明且无边框
|
|
|
|
|
|
lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
|
|
|
|
|
|
lv_obj_set_style_border_width(container, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_all(container, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 将消息气泡移入此容器
|
|
|
|
|
|
lv_obj_set_parent(msg_bubble, container);
|
|
|
|
|
|
|
|
|
|
|
|
// 将气泡居中对齐在容器中
|
|
|
|
|
|
lv_obj_align(msg_bubble, LV_ALIGN_CENTER, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 自动滚动底部
|
|
|
|
|
|
lv_obj_scroll_to_view_recursive(container, LV_ANIM_ON);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// For assistant messages
|
|
|
|
|
|
// Left align assistant messages
|
|
|
|
|
|
lv_obj_align(msg_bubble, LV_ALIGN_LEFT_MID, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Auto-scroll to the message bubble
|
|
|
|
|
|
lv_obj_scroll_to_view_recursive(msg_bubble, LV_ANIM_ON);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Store reference to the latest message label
|
|
|
|
|
|
chat_message_label_ = msg_text;
|
|
|
|
|
|
|
|
|
|
|
|
// 检查消息数量是否超过限制
|
|
|
|
|
|
uint32_t msg_count = lv_obj_get_child_cnt(content_);
|
|
|
|
|
|
while (msg_count >= MAX_MESSAGES) {
|
|
|
|
|
|
// 删除最早的消息(第一个子节点)
|
|
|
|
|
|
lv_obj_t* oldest_msg = lv_obj_get_child(content_, 0);
|
|
|
|
|
|
if (oldest_msg != nullptr) {
|
|
|
|
|
|
lv_obj_del(oldest_msg);
|
|
|
|
|
|
msg_count--;
|
|
|
|
|
|
}else{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
2025-01-05 21:20:30 +08:00
|
|
|
|
void LcdDisplay::SetupUI() {
|
2024-11-18 06:17:39 +08:00
|
|
|
|
DisplayLockGuard lock(this);
|
|
|
|
|
|
|
2025-02-04 14:24:03 +08:00
|
|
|
|
auto screen = lv_screen_active();
|
2025-01-24 03:00:50 +08:00
|
|
|
|
lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Container */
|
|
|
|
|
|
container_ = lv_obj_create(screen);
|
|
|
|
|
|
lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
|
|
|
|
|
|
lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
|
|
|
|
|
|
lv_obj_set_style_pad_all(container_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_border_width(container_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_row(container_, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Status bar */
|
|
|
|
|
|
status_bar_ = lv_obj_create(container_);
|
2025-01-24 03:00:50 +08:00
|
|
|
|
lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.text_font->line_height);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
lv_obj_set_style_radius(status_bar_, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
/* Content */
|
|
|
|
|
|
content_ = lv_obj_create(container_);
|
|
|
|
|
|
lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
|
|
|
|
|
|
lv_obj_set_style_radius(content_, 0, 0);
|
|
|
|
|
|
lv_obj_set_width(content_, LV_HOR_RES);
|
|
|
|
|
|
lv_obj_set_flex_grow(content_, 1);
|
|
|
|
|
|
|
2024-12-26 12:24:53 +08:00
|
|
|
|
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); // 子对象居中对齐,等距分布
|
|
|
|
|
|
|
2024-11-18 06:17:39 +08:00
|
|
|
|
emotion_label_ = lv_label_create(content_);
|
2025-01-23 13:50:22 +08:00
|
|
|
|
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
|
2024-12-26 12:24:53 +08:00
|
|
|
|
|
|
|
|
|
|
chat_message_label_ = lv_label_create(content_);
|
|
|
|
|
|
lv_label_set_text(chat_message_label_, "");
|
2025-01-23 13:50:22 +08:00
|
|
|
|
lv_obj_set_width(chat_message_label_, LV_HOR_RES * 0.9); // 限制宽度为屏幕宽度的 90%
|
2024-12-26 12:24:53 +08:00
|
|
|
|
lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_WRAP); // 设置为自动换行模式
|
|
|
|
|
|
lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_CENTER, 0); // 设置文本居中对齐
|
2024-11-18 06:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
/* Status bar */
|
|
|
|
|
|
lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
|
|
|
|
|
|
lv_obj_set_style_pad_all(status_bar_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_border_width(status_bar_, 0, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_column(status_bar_, 0, 0);
|
2025-01-23 13:50:22 +08:00
|
|
|
|
lv_obj_set_style_pad_left(status_bar_, 2, 0);
|
|
|
|
|
|
lv_obj_set_style_pad_right(status_bar_, 2, 0);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
network_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_label_set_text(network_label_, "");
|
2025-01-24 03:00:50 +08:00
|
|
|
|
lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
notification_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_obj_set_flex_grow(notification_label_, 1);
|
|
|
|
|
|
lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
|
2025-02-19 23:54:59 +08:00
|
|
|
|
lv_label_set_text(notification_label_, "");
|
2024-11-18 06:17:39 +08:00
|
|
|
|
lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
|
|
|
|
|
|
|
|
|
|
|
|
status_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_obj_set_flex_grow(status_label_, 1);
|
2025-01-06 01:26:39 +08:00
|
|
|
|
lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
|
2025-02-19 23:54:59 +08:00
|
|
|
|
lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
mute_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_label_set_text(mute_label_, "");
|
2025-01-24 03:00:50 +08:00
|
|
|
|
lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
battery_label_ = lv_label_create(status_bar_);
|
|
|
|
|
|
lv_label_set_text(battery_label_, "");
|
2025-01-24 03:00:50 +08:00
|
|
|
|
lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
|
2025-03-06 05:22:06 +08:00
|
|
|
|
|
|
|
|
|
|
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_style_bg_color(low_battery_popup_, lv_color_black(), 0);
|
|
|
|
|
|
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
|
|
|
|
|
|
lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
|
|
|
|
|
|
lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
|
|
|
|
|
|
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);
|
2024-11-18 06:17:39 +08:00
|
|
|
|
}
|
2025-03-18 00:47:35 +08:00
|
|
|
|
#endif
|
2024-12-26 12:24:53 +08:00
|
|
|
|
|
2025-02-19 23:54:59 +08:00
|
|
|
|
void LcdDisplay::SetEmotion(const char* emotion) {
|
2025-01-19 04:29:23 +08:00
|
|
|
|
struct Emotion {
|
|
|
|
|
|
const char* icon;
|
|
|
|
|
|
const char* text;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const std::vector<Emotion> emotions = {
|
|
|
|
|
|
{"😶", "neutral"},
|
2025-02-03 23:43:07 +08:00
|
|
|
|
{"🙂", "happy"},
|
2025-01-19 04:29:23 +08:00
|
|
|
|
{"😆", "laughing"},
|
|
|
|
|
|
{"😂", "funny"},
|
|
|
|
|
|
{"😔", "sad"},
|
|
|
|
|
|
{"😠", "angry"},
|
|
|
|
|
|
{"😭", "crying"},
|
|
|
|
|
|
{"😍", "loving"},
|
|
|
|
|
|
{"😳", "embarrassed"},
|
2025-02-01 11:06:17 +08:00
|
|
|
|
{"😯", "surprised"},
|
2025-02-03 23:43:07 +08:00
|
|
|
|
{"😱", "shocked"},
|
2025-01-19 04:29:23 +08:00
|
|
|
|
{"🤔", "thinking"},
|
|
|
|
|
|
{"😉", "winking"},
|
|
|
|
|
|
{"😎", "cool"},
|
|
|
|
|
|
{"😌", "relaxed"},
|
2025-02-03 23:43:07 +08:00
|
|
|
|
{"🤤", "delicious"},
|
2025-01-19 04:29:23 +08:00
|
|
|
|
{"😘", "kissy"},
|
|
|
|
|
|
{"😏", "confident"},
|
|
|
|
|
|
{"😴", "sleepy"},
|
2025-02-03 23:43:07 +08:00
|
|
|
|
{"😜", "silly"},
|
|
|
|
|
|
{"🙄", "confused"}
|
2025-01-19 04:29:23 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 查找匹配的表情
|
2025-02-19 23:54:59 +08:00
|
|
|
|
std::string_view emotion_view(emotion);
|
2025-01-19 04:29:23 +08:00
|
|
|
|
auto it = std::find_if(emotions.begin(), emotions.end(),
|
2025-02-19 23:54:59 +08:00
|
|
|
|
[&emotion_view](const Emotion& e) { return e.text == emotion_view; });
|
2025-02-03 23:43:07 +08:00
|
|
|
|
|
|
|
|
|
|
DisplayLockGuard lock(this);
|
2025-02-04 14:24:03 +08:00
|
|
|
|
if (emotion_label_ == nullptr) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-19 04:29:23 +08:00
|
|
|
|
// 如果找到匹配的表情就显示对应图标,否则显示默认的neutral表情
|
2025-01-24 03:00:50 +08:00
|
|
|
|
lv_obj_set_style_text_font(emotion_label_, fonts_.emoji_font, 0);
|
2025-01-19 04:29:23 +08:00
|
|
|
|
if (it != emotions.end()) {
|
|
|
|
|
|
lv_label_set_text(emotion_label_, it->icon);
|
|
|
|
|
|
} else {
|
2025-02-03 23:43:07 +08:00
|
|
|
|
lv_label_set_text(emotion_label_, "😶");
|
2025-01-19 04:29:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-25 12:44:31 +08:00
|
|
|
|
void LcdDisplay::SetIcon(const char* icon) {
|
2025-02-04 14:24:03 +08:00
|
|
|
|
DisplayLockGuard lock(this);
|
2025-01-25 12:44:31 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|