lcd_display.cc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. #include "lcd_display.h"
  2. #include <vector>
  3. #include <font_awesome_symbols.h>
  4. #include <esp_log.h>
  5. #include <esp_err.h>
  6. #include <esp_lvgl_port.h>
  7. #include "assets/lang_config.h"
  8. #include <cstring>
  9. #include "settings.h"
  10. #include "board.h"
  11. #include "esp_lcd_touch_gt911.h"
  12. #define TAG "LcdDisplay"
  13. // Color definitions for dark theme
  14. #define DARK_BACKGROUND_COLOR lv_color_hex(0x121212) // Dark background
  15. #define DARK_TEXT_COLOR lv_color_white() // White text
  16. #define DARK_CHAT_BACKGROUND_COLOR lv_color_hex(0x1E1E1E) // Slightly lighter than background
  17. #define DARK_USER_BUBBLE_COLOR lv_color_hex(0x1A6C37) // Dark green
  18. #define DARK_ASSISTANT_BUBBLE_COLOR lv_color_hex(0x333333) // Dark gray
  19. #define DARK_SYSTEM_BUBBLE_COLOR lv_color_hex(0x2A2A2A) // Medium gray
  20. #define DARK_SYSTEM_TEXT_COLOR lv_color_hex(0xAAAAAA) // Light gray text
  21. #define DARK_BORDER_COLOR lv_color_hex(0x333333) // Dark gray border
  22. #define DARK_LOW_BATTERY_COLOR lv_color_hex(0xFF0000) // Red for dark mode
  23. // Color definitions for light theme
  24. #define LIGHT_BACKGROUND_COLOR lv_color_white() // White background
  25. #define LIGHT_TEXT_COLOR lv_color_black() // Black text
  26. #define LIGHT_CHAT_BACKGROUND_COLOR lv_color_hex(0xE0E0E0) // Light gray background
  27. #define LIGHT_USER_BUBBLE_COLOR lv_color_hex(0x95EC69) // WeChat green
  28. #define LIGHT_ASSISTANT_BUBBLE_COLOR lv_color_white() // White
  29. #define LIGHT_SYSTEM_BUBBLE_COLOR lv_color_hex(0xE0E0E0) // Light gray
  30. #define LIGHT_SYSTEM_TEXT_COLOR lv_color_hex(0x666666) // Dark gray text
  31. #define LIGHT_BORDER_COLOR lv_color_hex(0xE0E0E0) // Light gray border
  32. #define LIGHT_LOW_BATTERY_COLOR lv_color_black() // Black for light mode
  33. // Theme color structure
  34. struct ThemeColors {
  35. lv_color_t background;
  36. lv_color_t text;
  37. lv_color_t chat_background;
  38. lv_color_t user_bubble;
  39. lv_color_t assistant_bubble;
  40. lv_color_t system_bubble;
  41. lv_color_t system_text;
  42. lv_color_t border;
  43. lv_color_t low_battery;
  44. };
  45. // Define dark theme colors
  46. static const ThemeColors DARK_THEME = {
  47. .background = DARK_BACKGROUND_COLOR,
  48. .text = DARK_TEXT_COLOR,
  49. .chat_background = DARK_CHAT_BACKGROUND_COLOR,
  50. .user_bubble = DARK_USER_BUBBLE_COLOR,
  51. .assistant_bubble = DARK_ASSISTANT_BUBBLE_COLOR,
  52. .system_bubble = DARK_SYSTEM_BUBBLE_COLOR,
  53. .system_text = DARK_SYSTEM_TEXT_COLOR,
  54. .border = DARK_BORDER_COLOR,
  55. .low_battery = DARK_LOW_BATTERY_COLOR
  56. };
  57. // Define light theme colors
  58. static const ThemeColors LIGHT_THEME = {
  59. .background = LIGHT_BACKGROUND_COLOR,
  60. .text = LIGHT_TEXT_COLOR,
  61. .chat_background = LIGHT_CHAT_BACKGROUND_COLOR,
  62. .user_bubble = LIGHT_USER_BUBBLE_COLOR,
  63. .assistant_bubble = LIGHT_ASSISTANT_BUBBLE_COLOR,
  64. .system_bubble = LIGHT_SYSTEM_BUBBLE_COLOR,
  65. .system_text = LIGHT_SYSTEM_TEXT_COLOR,
  66. .border = LIGHT_BORDER_COLOR,
  67. .low_battery = LIGHT_LOW_BATTERY_COLOR
  68. };
  69. // Current theme - initialize based on default config
  70. static ThemeColors current_theme = LIGHT_THEME;
  71. LV_FONT_DECLARE(font_awesome_30_4);
  72. SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  73. int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
  74. DisplayFonts fonts)
  75. : LcdDisplay(panel_io, panel, fonts) {
  76. width_ = width;
  77. height_ = height;
  78. // draw white
  79. std::vector<uint16_t> buffer(width_, 0xFFFF);
  80. for (int y = 0; y < height_; y++) {
  81. esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  82. }
  83. // Set the display to on
  84. ESP_LOGI(TAG, "Turning display on");
  85. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  86. ESP_LOGI(TAG, "Initialize LVGL library");
  87. lv_init();
  88. ESP_LOGI(TAG, "Initialize LVGL port");
  89. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  90. port_cfg.task_priority = 1;
  91. lvgl_port_init(&port_cfg);
  92. ESP_LOGI(TAG, "Adding LCD screen");
  93. const lvgl_port_display_cfg_t display_cfg = {
  94. .io_handle = panel_io_,
  95. .panel_handle = panel_,
  96. .control_handle = nullptr,
  97. .buffer_size = static_cast<uint32_t>(width_ * 10),
  98. .double_buffer = false,
  99. .trans_size = 0,
  100. .hres = static_cast<uint32_t>(width_),
  101. .vres = static_cast<uint32_t>(height_),
  102. .monochrome = false,
  103. .rotation = {
  104. .swap_xy = swap_xy,
  105. .mirror_x = mirror_x,
  106. .mirror_y = mirror_y,
  107. },
  108. .color_format = LV_COLOR_FORMAT_RGB565,
  109. .flags = {
  110. .buff_dma = 1,
  111. .buff_spiram = 0,
  112. .sw_rotate = 0,
  113. .swap_bytes = 1,
  114. .full_refresh = 0,
  115. .direct_mode = 0,
  116. },
  117. };
  118. display_ = lvgl_port_add_disp(&display_cfg);
  119. if (display_ == nullptr) {
  120. ESP_LOGE(TAG, "Failed to add display");
  121. return;
  122. }
  123. if (offset_x != 0 || offset_y != 0) {
  124. lv_display_set_offset(display_, offset_x, offset_y);
  125. }
  126. // Update the theme
  127. if (current_theme_name_ == "dark") {
  128. current_theme = DARK_THEME;
  129. } else if (current_theme_name_ == "light") {
  130. current_theme = LIGHT_THEME;
  131. }
  132. SetupUI();
  133. }
  134. // RGB LCD实现
  135. RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  136. int width, int height, int offset_x, int offset_y,
  137. bool mirror_x, bool mirror_y, bool swap_xy,
  138. DisplayFonts fonts)
  139. : LcdDisplay(panel_io, panel, fonts) {
  140. width_ = width;
  141. height_ = height;
  142. // draw white
  143. std::vector<uint16_t> buffer(width_, 0xFFFF);
  144. for (int y = 0; y < height_; y++) {
  145. esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  146. }
  147. ESP_LOGI(TAG, "Initialize LVGL library");
  148. lv_init();
  149. ESP_LOGI(TAG, "Initialize LVGL port");
  150. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  151. port_cfg.task_priority = 1;
  152. lvgl_port_init(&port_cfg);
  153. ESP_LOGI(TAG, "Adding LCD screen");
  154. const lvgl_port_display_cfg_t display_cfg = {
  155. .io_handle = panel_io_,
  156. .panel_handle = panel_,
  157. .buffer_size = static_cast<uint32_t>(width_ * 10),
  158. .double_buffer = true,
  159. .hres = static_cast<uint32_t>(width_),
  160. .vres = static_cast<uint32_t>(height_),
  161. .rotation = {
  162. .swap_xy = swap_xy,
  163. .mirror_x = mirror_x,
  164. .mirror_y = mirror_y,
  165. },
  166. .flags = {
  167. .buff_dma = 1,
  168. .swap_bytes = 0,
  169. .full_refresh = 1,
  170. .direct_mode = 1,
  171. },
  172. };
  173. const lvgl_port_display_rgb_cfg_t rgb_cfg = {
  174. .flags = {
  175. .bb_mode = true,
  176. .avoid_tearing = true,
  177. }
  178. };
  179. display_ = lvgl_port_add_disp_rgb(&display_cfg, &rgb_cfg);
  180. if (display_ == nullptr) {
  181. ESP_LOGE(TAG, "Failed to add RGB display");
  182. return;
  183. }
  184. if (offset_x != 0 || offset_y != 0) {
  185. lv_display_set_offset(display_, offset_x, offset_y);
  186. }
  187. // Update the theme
  188. if (current_theme_name_ == "dark") {
  189. current_theme = DARK_THEME;
  190. } else if (current_theme_name_ == "light") {
  191. current_theme = LIGHT_THEME;
  192. }
  193. SetupUI();
  194. }
  195. MipiLcdDisplay::MipiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,i2c_master_bus_handle_t i2c_handle,
  196. int width, int height, int offset_x, int offset_y,
  197. bool mirror_x, bool mirror_y, bool swap_xy,
  198. DisplayFonts fonts): LcdDisplay(panel_io, panel, fonts) {
  199. width_ = width;
  200. height_ = height;
  201. ESP_LOGI(TAG, "Initialize LVGL library");
  202. lv_init();
  203. ESP_LOGI(TAG, "Initialize LVGL port");
  204. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  205. lvgl_port_init(&port_cfg);
  206. const lvgl_port_display_cfg_t disp_cfg = {
  207. .io_handle = panel_io_,
  208. .panel_handle = panel_,
  209. .control_handle = NULL,
  210. .buffer_size = static_cast<uint32_t>(width * 50),
  211. .double_buffer = false,
  212. .hres = static_cast<uint32_t>(width_),
  213. .vres = static_cast<uint32_t>(height_),
  214. .monochrome = false,
  215. .rotation = {
  216. .swap_xy = false,
  217. .mirror_x = true,
  218. .mirror_y = true,
  219. },
  220. .color_format = LV_COLOR_FORMAT_RGB565,
  221. .flags = {
  222. .buff_dma = true,
  223. .buff_spiram = true,
  224. .sw_rotate = true,
  225. .swap_bytes = 0,
  226. .full_refresh = false,
  227. .direct_mode = false,
  228. }
  229. };
  230. lvgl_port_display_dsi_cfg_t dpi_cfg = {
  231. .flags = {
  232. .avoid_tearing = false,
  233. }
  234. };
  235. display_ = lvgl_port_add_disp_dsi(&disp_cfg, &dpi_cfg);
  236. if (display_ == nullptr) {
  237. ESP_LOGE(TAG, "Failed to add display");
  238. return;
  239. }
  240. if (offset_x != 0 || offset_y != 0) {
  241. lv_display_set_offset(display_, offset_x, offset_y);
  242. }
  243. if (current_theme_name_ == "dark") {
  244. current_theme = DARK_THEME;
  245. } else if (current_theme_name_ == "light") {
  246. current_theme = LIGHT_THEME;
  247. }
  248. esp_lcd_touch_handle_t tp;
  249. esp_lcd_touch_config_t tp_cfg = {
  250. .x_max = 800,
  251. .y_max = 1280,
  252. .rst_gpio_num = GPIO_NUM_NC,
  253. .int_gpio_num = GPIO_NUM_NC,
  254. .levels = {.reset = 0, .interrupt = 0},
  255. .flags = {.swap_xy = 0, .mirror_x = 0, .mirror_y = 0},
  256. };
  257. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  258. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
  259. tp_io_config.scl_speed_hz = 100000;
  260. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_handle, &tp_io_config, &tp_io_handle));
  261. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
  262. const lvgl_port_touch_cfg_t touch_cfg = {
  263. .disp = lv_display_get_default(),
  264. .handle = tp,
  265. };
  266. lvgl_port_add_touch(&touch_cfg);
  267. lv_disp_t* disp = lv_display_get_default();
  268. if (disp) {
  269. // 先设置旋转(顺时针90度)
  270. lv_disp_set_rotation(disp, LV_DISP_ROTATION_90);
  271. }
  272. SetupUI();
  273. }
  274. LcdDisplay::~LcdDisplay() {
  275. // 然后再清理 LVGL 对象
  276. if (content_ != nullptr) {
  277. lv_obj_del(content_);
  278. }
  279. if (status_bar_ != nullptr) {
  280. lv_obj_del(status_bar_);
  281. }
  282. if (side_bar_ != nullptr) {
  283. lv_obj_del(side_bar_);
  284. }
  285. if (container_ != nullptr) {
  286. lv_obj_del(container_);
  287. }
  288. if (display_ != nullptr) {
  289. lv_display_delete(display_);
  290. }
  291. if (panel_ != nullptr) {
  292. esp_lcd_panel_del(panel_);
  293. }
  294. if (panel_io_ != nullptr) {
  295. esp_lcd_panel_io_del(panel_io_);
  296. }
  297. }
  298. bool LcdDisplay::Lock(int timeout_ms) {
  299. return lvgl_port_lock(timeout_ms);
  300. }
  301. void LcdDisplay::Unlock() {
  302. lvgl_port_unlock();
  303. }
  304. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  305. void LcdDisplay::SetupUI() {
  306. DisplayLockGuard lock(this);
  307. auto screen = lv_screen_active();
  308. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  309. lv_obj_set_style_text_color(screen, current_theme.text, 0);
  310. lv_obj_set_style_bg_color(screen, current_theme.background, 0);
  311. /* Container */
  312. container_ = lv_obj_create(screen);
  313. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  314. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
  315. lv_obj_set_style_pad_all(container_, 0, 0);
  316. lv_obj_set_style_border_width(container_, 0, 0);
  317. lv_obj_set_style_pad_row(container_, 0, 0);
  318. lv_obj_set_style_bg_color(container_, current_theme.background, 0);
  319. lv_obj_set_style_border_color(container_, current_theme.border, 0);
  320. /* Status bar */
  321. status_bar_ = lv_obj_create(container_);
  322. lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.emoji_font->line_height);
  323. lv_obj_set_style_radius(status_bar_, 0, 0);
  324. lv_obj_set_style_bg_color(status_bar_, current_theme.background, 0);
  325. lv_obj_set_style_text_color(status_bar_, current_theme.text, 0);
  326. /* Content - Chat area */
  327. content_ = lv_obj_create(container_);
  328. lv_obj_set_style_radius(content_, 0, 0);
  329. lv_obj_set_width(content_, LV_HOR_RES);
  330. lv_obj_set_flex_grow(content_, 1);
  331. lv_obj_set_style_pad_all(content_, 5, 0);
  332. lv_obj_set_style_bg_color(content_, current_theme.chat_background, 0); // Background for chat area
  333. lv_obj_set_style_border_color(content_, current_theme.border, 0); // Border color for chat area
  334. // Enable scrolling for chat content
  335. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  336. lv_obj_set_scroll_dir(content_, LV_DIR_VER);
  337. // Create a flex container for chat messages
  338. lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN);
  339. lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
  340. lv_obj_set_style_pad_row(content_, 10, 0); // Space between messages
  341. // We'll create chat messages dynamically in SetChatMessage
  342. chat_message_label_ = nullptr;
  343. /* Status bar */
  344. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  345. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  346. lv_obj_set_style_border_width(status_bar_, 0, 0);
  347. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  348. lv_obj_set_style_pad_left(status_bar_, 2, 0);
  349. lv_obj_set_style_pad_right(status_bar_, 2, 0);
  350. lv_obj_set_scrollbar_mode(status_bar_, LV_SCROLLBAR_MODE_OFF);
  351. // 设置状态栏的内容垂直居中
  352. lv_obj_set_flex_align(status_bar_, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
  353. // 创建emotion_label_在状态栏最左侧
  354. emotion_label_ = lv_label_create(status_bar_);
  355. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  356. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  357. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  358. lv_obj_set_style_margin_right(emotion_label_, 5, 0); // 添加右边距,与后面的元素分隔
  359. notification_label_ = lv_label_create(status_bar_);
  360. lv_obj_set_flex_grow(notification_label_, 1);
  361. lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
  362. lv_obj_set_style_text_color(notification_label_, current_theme.text, 0);
  363. lv_label_set_text(notification_label_, "");
  364. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  365. status_label_ = lv_label_create(status_bar_);
  366. lv_obj_set_flex_grow(status_label_, 1);
  367. lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  368. lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
  369. lv_obj_set_style_text_color(status_label_, current_theme.text, 0);
  370. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  371. mute_label_ = lv_label_create(status_bar_);
  372. lv_label_set_text(mute_label_, "");
  373. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  374. lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);
  375. network_label_ = lv_label_create(status_bar_);
  376. lv_label_set_text(network_label_, "");
  377. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  378. lv_obj_set_style_text_color(network_label_, current_theme.text, 0);
  379. lv_obj_set_style_margin_left(network_label_, 5, 0); // 添加左边距,与前面的元素分隔
  380. battery_label_ = lv_label_create(status_bar_);
  381. lv_label_set_text(battery_label_, "");
  382. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  383. lv_obj_set_style_text_color(battery_label_, current_theme.text, 0);
  384. lv_obj_set_style_margin_left(battery_label_, 5, 0); // 添加左边距,与前面的元素分隔
  385. low_battery_popup_ = lv_obj_create(screen);
  386. lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
  387. lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
  388. lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
  389. lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
  390. lv_obj_set_style_radius(low_battery_popup_, 10, 0);
  391. lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
  392. lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
  393. lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
  394. lv_obj_center(low_battery_label);
  395. lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
  396. }
  397. #define MAX_MESSAGES 300
  398. void LcdDisplay::SetChatMessage(const char* role, const char* content) {
  399. DisplayLockGuard lock(this);
  400. if (content_ == nullptr) {
  401. return;
  402. }
  403. //避免出现空的消息框
  404. if(strlen(content) == 0) return;
  405. // Create a message bubble
  406. lv_obj_t* msg_bubble = lv_obj_create(content_);
  407. lv_obj_set_style_radius(msg_bubble, 8, 0);
  408. lv_obj_set_scrollbar_mode(msg_bubble, LV_SCROLLBAR_MODE_OFF);
  409. lv_obj_set_style_border_width(msg_bubble, 1, 0);
  410. lv_obj_set_style_border_color(msg_bubble, current_theme.border, 0);
  411. lv_obj_set_style_pad_all(msg_bubble, 8, 0);
  412. // Create the message text
  413. lv_obj_t* msg_text = lv_label_create(msg_bubble);
  414. lv_label_set_text(msg_text, content);
  415. // 计算文本实际宽度
  416. lv_coord_t text_width = lv_txt_get_width(content, strlen(content), fonts_.text_font, 0);
  417. // 计算气泡宽度
  418. lv_coord_t max_width = LV_HOR_RES * 85 / 100 - 16; // 屏幕宽度的85%
  419. lv_coord_t min_width = 20;
  420. lv_coord_t bubble_width;
  421. // 确保文本宽度不小于最小宽度
  422. if (text_width < min_width) {
  423. text_width = min_width;
  424. }
  425. // 如果文本宽度小于最大宽度,使用文本宽度
  426. if (text_width < max_width) {
  427. bubble_width = text_width;
  428. } else {
  429. bubble_width = max_width;
  430. }
  431. // 设置消息文本的宽度
  432. lv_obj_set_width(msg_text, bubble_width); // 减去padding
  433. lv_label_set_long_mode(msg_text, LV_LABEL_LONG_WRAP);
  434. lv_obj_set_style_text_font(msg_text, fonts_.text_font, 0);
  435. // 设置气泡宽度
  436. lv_obj_set_width(msg_bubble, bubble_width);
  437. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  438. // Set alignment and style based on message role
  439. if (strcmp(role, "user") == 0) {
  440. // User messages are right-aligned with green background
  441. lv_obj_set_style_bg_color(msg_bubble, current_theme.user_bubble, 0);
  442. // Set text color for contrast
  443. lv_obj_set_style_text_color(msg_text, current_theme.text, 0);
  444. // 设置自定义属性标记气泡类型
  445. lv_obj_set_user_data(msg_bubble, (void*)"user");
  446. // Set appropriate width for content
  447. lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
  448. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  449. // Add some margin
  450. lv_obj_set_style_margin_right(msg_bubble, 10, 0);
  451. // Don't grow
  452. lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
  453. } else if (strcmp(role, "assistant") == 0) {
  454. // Assistant messages are left-aligned with white background
  455. lv_obj_set_style_bg_color(msg_bubble, current_theme.assistant_bubble, 0);
  456. // Set text color for contrast
  457. lv_obj_set_style_text_color(msg_text, current_theme.text, 0);
  458. // 设置自定义属性标记气泡类型
  459. lv_obj_set_user_data(msg_bubble, (void*)"assistant");
  460. // Set appropriate width for content
  461. lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
  462. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  463. // Add some margin
  464. lv_obj_set_style_margin_left(msg_bubble, -4, 0);
  465. // Don't grow
  466. lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
  467. } else if (strcmp(role, "system") == 0) {
  468. // System messages are center-aligned with light gray background
  469. lv_obj_set_style_bg_color(msg_bubble, current_theme.system_bubble, 0);
  470. // Set text color for contrast
  471. lv_obj_set_style_text_color(msg_text, current_theme.system_text, 0);
  472. // 设置自定义属性标记气泡类型
  473. lv_obj_set_user_data(msg_bubble, (void*)"system");
  474. // Set appropriate width for content
  475. lv_obj_set_width(msg_bubble, LV_SIZE_CONTENT);
  476. lv_obj_set_height(msg_bubble, LV_SIZE_CONTENT);
  477. // Don't grow
  478. lv_obj_set_style_flex_grow(msg_bubble, 0, 0);
  479. }
  480. // Create a full-width container for user messages to ensure right alignment
  481. if (strcmp(role, "user") == 0) {
  482. // Create a full-width container
  483. lv_obj_t* container = lv_obj_create(content_);
  484. lv_obj_set_width(container, LV_HOR_RES);
  485. lv_obj_set_height(container, LV_SIZE_CONTENT);
  486. // Make container transparent and borderless
  487. lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
  488. lv_obj_set_style_border_width(container, 0, 0);
  489. lv_obj_set_style_pad_all(container, 0, 0);
  490. // Move the message bubble into this container
  491. lv_obj_set_parent(msg_bubble, container);
  492. // Right align the bubble in the container
  493. lv_obj_align(msg_bubble, LV_ALIGN_RIGHT_MID, -10, 0);
  494. // Auto-scroll to this container
  495. lv_obj_scroll_to_view_recursive(container, LV_ANIM_ON);
  496. } else if (strcmp(role, "system") == 0) {
  497. // 为系统消息创建全宽容器以确保居中对齐
  498. lv_obj_t* container = lv_obj_create(content_);
  499. lv_obj_set_width(container, LV_HOR_RES);
  500. lv_obj_set_height(container, LV_SIZE_CONTENT);
  501. // 使容器透明且无边框
  502. lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
  503. lv_obj_set_style_border_width(container, 0, 0);
  504. lv_obj_set_style_pad_all(container, 0, 0);
  505. // 将消息气泡移入此容器
  506. lv_obj_set_parent(msg_bubble, container);
  507. // 将气泡居中对齐在容器中
  508. lv_obj_align(msg_bubble, LV_ALIGN_CENTER, 0, 0);
  509. // 自动滚动底部
  510. lv_obj_scroll_to_view_recursive(container, LV_ANIM_ON);
  511. } else {
  512. // For assistant messages
  513. // Left align assistant messages
  514. lv_obj_align(msg_bubble, LV_ALIGN_LEFT_MID, 0, 0);
  515. // Auto-scroll to the message bubble
  516. lv_obj_scroll_to_view_recursive(msg_bubble, LV_ANIM_ON);
  517. }
  518. // Store reference to the latest message label
  519. chat_message_label_ = msg_text;
  520. // 检查消息数量是否超过限制
  521. uint32_t msg_count = lv_obj_get_child_cnt(content_);
  522. while (msg_count >= MAX_MESSAGES) {
  523. // 删除最早的消息(第一个子节点)
  524. lv_obj_t* oldest_msg = lv_obj_get_child(content_, 0);
  525. if (oldest_msg != nullptr) {
  526. lv_obj_del(oldest_msg);
  527. msg_count--;
  528. // 删除最早的消息会导致所有气泡整体往上移
  529. // 所以需要重新滚动到当前消息气泡位置
  530. lv_obj_scroll_to_view_recursive(msg_bubble, LV_ANIM_ON);
  531. }else{
  532. break;
  533. }
  534. }
  535. }
  536. #else
  537. void LcdDisplay::SetupUI() {
  538. DisplayLockGuard lock(this);
  539. auto screen = lv_screen_active();
  540. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  541. lv_obj_set_style_text_color(screen, current_theme.text, 0);
  542. lv_obj_set_style_bg_color(screen, current_theme.background, 0);
  543. /* Container */
  544. container_ = lv_obj_create(screen);
  545. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  546. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
  547. lv_obj_set_style_pad_all(container_, 0, 0);
  548. lv_obj_set_style_border_width(container_, 0, 0);
  549. lv_obj_set_style_pad_row(container_, 0, 0);
  550. lv_obj_set_style_bg_color(container_, current_theme.background, 0);
  551. lv_obj_set_style_border_color(container_, current_theme.border, 0);
  552. /* Status bar */
  553. status_bar_ = lv_obj_create(container_);
  554. lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.text_font->line_height);
  555. lv_obj_set_style_radius(status_bar_, 0, 0);
  556. lv_obj_set_style_bg_color(status_bar_, current_theme.background, 0);
  557. lv_obj_set_style_text_color(status_bar_, current_theme.text, 0);
  558. /* Content */
  559. content_ = lv_obj_create(container_);
  560. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  561. lv_obj_set_style_radius(content_, 0, 0);
  562. lv_obj_set_width(content_, LV_HOR_RES);
  563. lv_obj_set_flex_grow(content_, 1);
  564. lv_obj_set_style_pad_all(content_, 5, 0);
  565. lv_obj_set_style_bg_color(content_, current_theme.chat_background, 0);
  566. lv_obj_set_style_border_color(content_, current_theme.border, 0); // Border color for content
  567. lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN); // 垂直布局(从上到下)
  568. lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY); // 子对象居中对齐,等距分布
  569. emotion_label_ = lv_label_create(content_);
  570. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  571. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  572. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  573. chat_message_label_ = lv_label_create(content_);
  574. lv_label_set_text(chat_message_label_, "");
  575. lv_obj_set_width(chat_message_label_, LV_HOR_RES * 0.9); // 限制宽度为屏幕宽度的 90%
  576. lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_WRAP); // 设置为自动换行模式
  577. lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_CENTER, 0); // 设置文本居中对齐
  578. lv_obj_set_style_text_color(chat_message_label_, current_theme.text, 0);
  579. /* Status bar */
  580. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  581. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  582. lv_obj_set_style_border_width(status_bar_, 0, 0);
  583. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  584. lv_obj_set_style_pad_left(status_bar_, 2, 0);
  585. lv_obj_set_style_pad_right(status_bar_, 2, 0);
  586. network_label_ = lv_label_create(status_bar_);
  587. lv_label_set_text(network_label_, "");
  588. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  589. lv_obj_set_style_text_color(network_label_, current_theme.text, 0);
  590. notification_label_ = lv_label_create(status_bar_);
  591. lv_obj_set_flex_grow(notification_label_, 1);
  592. lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
  593. lv_obj_set_style_text_color(notification_label_, current_theme.text, 0);
  594. lv_label_set_text(notification_label_, "");
  595. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  596. status_label_ = lv_label_create(status_bar_);
  597. lv_obj_set_flex_grow(status_label_, 1);
  598. lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  599. lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
  600. lv_obj_set_style_text_color(status_label_, current_theme.text, 0);
  601. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  602. mute_label_ = lv_label_create(status_bar_);
  603. lv_label_set_text(mute_label_, "");
  604. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  605. lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);
  606. battery_label_ = lv_label_create(status_bar_);
  607. lv_label_set_text(battery_label_, "");
  608. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  609. lv_obj_set_style_text_color(battery_label_, current_theme.text, 0);
  610. low_battery_popup_ = lv_obj_create(screen);
  611. lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
  612. lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
  613. lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
  614. lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
  615. lv_obj_set_style_radius(low_battery_popup_, 10, 0);
  616. lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
  617. lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
  618. lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
  619. lv_obj_center(low_battery_label);
  620. lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
  621. }
  622. #endif
  623. void LcdDisplay::SetEmotion(const char* emotion) {
  624. struct Emotion {
  625. const char* icon;
  626. const char* text;
  627. };
  628. static const std::vector<Emotion> emotions = {
  629. {"😶", "neutral"},
  630. {"🙂", "happy"},
  631. {"😆", "laughing"},
  632. {"😂", "funny"},
  633. {"😔", "sad"},
  634. {"😠", "angry"},
  635. {"😭", "crying"},
  636. {"😍", "loving"},
  637. {"😳", "embarrassed"},
  638. {"😯", "surprised"},
  639. {"😱", "shocked"},
  640. {"🤔", "thinking"},
  641. {"😉", "winking"},
  642. {"😎", "cool"},
  643. {"😌", "relaxed"},
  644. {"🤤", "delicious"},
  645. {"😘", "kissy"},
  646. {"😏", "confident"},
  647. {"😴", "sleepy"},
  648. {"😜", "silly"},
  649. {"🙄", "confused"}
  650. };
  651. // 查找匹配的表情
  652. std::string_view emotion_view(emotion);
  653. auto it = std::find_if(emotions.begin(), emotions.end(),
  654. [&emotion_view](const Emotion& e) { return e.text == emotion_view; });
  655. DisplayLockGuard lock(this);
  656. if (emotion_label_ == nullptr) {
  657. return;
  658. }
  659. // 如果找到匹配的表情就显示对应图标,否则显示默认的neutral表情
  660. lv_obj_set_style_text_font(emotion_label_, fonts_.emoji_font, 0);
  661. if (it != emotions.end()) {
  662. lv_label_set_text(emotion_label_, it->icon);
  663. } else {
  664. lv_label_set_text(emotion_label_, "😶");
  665. }
  666. }
  667. void LcdDisplay::SetIcon(const char* icon) {
  668. DisplayLockGuard lock(this);
  669. if (emotion_label_ == nullptr) {
  670. return;
  671. }
  672. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  673. lv_label_set_text(emotion_label_, icon);
  674. }
  675. void LcdDisplay::SetTheme(const std::string& theme_name) {
  676. DisplayLockGuard lock(this);
  677. if (theme_name == "dark" || theme_name == "DARK") {
  678. current_theme = DARK_THEME;
  679. } else if (theme_name == "light" || theme_name == "LIGHT") {
  680. current_theme = LIGHT_THEME;
  681. } else {
  682. // Invalid theme name, return false
  683. ESP_LOGE(TAG, "Invalid theme name: %s", theme_name.c_str());
  684. return;
  685. }
  686. // Get the active screen
  687. lv_obj_t* screen = lv_screen_active();
  688. // Update the screen colors
  689. lv_obj_set_style_bg_color(screen, current_theme.background, 0);
  690. lv_obj_set_style_text_color(screen, current_theme.text, 0);
  691. // Update container colors
  692. if (container_ != nullptr) {
  693. lv_obj_set_style_bg_color(container_, current_theme.background, 0);
  694. lv_obj_set_style_border_color(container_, current_theme.border, 0);
  695. }
  696. // Update status bar colors
  697. if (status_bar_ != nullptr) {
  698. lv_obj_set_style_bg_color(status_bar_, current_theme.background, 0);
  699. lv_obj_set_style_text_color(status_bar_, current_theme.text, 0);
  700. // Update status bar elements
  701. if (network_label_ != nullptr) {
  702. lv_obj_set_style_text_color(network_label_, current_theme.text, 0);
  703. }
  704. if (status_label_ != nullptr) {
  705. lv_obj_set_style_text_color(status_label_, current_theme.text, 0);
  706. }
  707. if (notification_label_ != nullptr) {
  708. lv_obj_set_style_text_color(notification_label_, current_theme.text, 0);
  709. }
  710. if (mute_label_ != nullptr) {
  711. lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);
  712. }
  713. if (battery_label_ != nullptr) {
  714. lv_obj_set_style_text_color(battery_label_, current_theme.text, 0);
  715. }
  716. if (emotion_label_ != nullptr) {
  717. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  718. }
  719. }
  720. // Update content area colors
  721. if (content_ != nullptr) {
  722. lv_obj_set_style_bg_color(content_, current_theme.chat_background, 0);
  723. lv_obj_set_style_border_color(content_, current_theme.border, 0);
  724. // If we have the chat message style, update all message bubbles
  725. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  726. // Iterate through all children of content (message containers or bubbles)
  727. uint32_t child_count = lv_obj_get_child_cnt(content_);
  728. for (uint32_t i = 0; i < child_count; i++) {
  729. lv_obj_t* obj = lv_obj_get_child(content_, i);
  730. if (obj == nullptr) continue;
  731. lv_obj_t* bubble = nullptr;
  732. // 检查这个对象是容器还是气泡
  733. // 如果是容器(用户或系统消息),则获取其子对象作为气泡
  734. // 如果是气泡(助手消息),则直接使用
  735. if (lv_obj_get_child_cnt(obj) > 0) {
  736. // 可能是容器,检查它是否为用户或系统消息容器
  737. // 用户和系统消息容器是透明的
  738. lv_opa_t bg_opa = lv_obj_get_style_bg_opa(obj, 0);
  739. if (bg_opa == LV_OPA_TRANSP) {
  740. // 这是用户或系统消息的容器
  741. bubble = lv_obj_get_child(obj, 0);
  742. } else {
  743. // 这可能是助手消息的气泡自身
  744. bubble = obj;
  745. }
  746. } else {
  747. // 没有子元素,可能是其他UI元素,跳过
  748. continue;
  749. }
  750. if (bubble == nullptr) continue;
  751. // 使用保存的用户数据来识别气泡类型
  752. void* bubble_type_ptr = lv_obj_get_user_data(bubble);
  753. if (bubble_type_ptr != nullptr) {
  754. const char* bubble_type = static_cast<const char*>(bubble_type_ptr);
  755. // 根据气泡类型应用正确的颜色
  756. if (strcmp(bubble_type, "user") == 0) {
  757. lv_obj_set_style_bg_color(bubble, current_theme.user_bubble, 0);
  758. } else if (strcmp(bubble_type, "assistant") == 0) {
  759. lv_obj_set_style_bg_color(bubble, current_theme.assistant_bubble, 0);
  760. } else if (strcmp(bubble_type, "system") == 0) {
  761. lv_obj_set_style_bg_color(bubble, current_theme.system_bubble, 0);
  762. }
  763. // Update border color
  764. lv_obj_set_style_border_color(bubble, current_theme.border, 0);
  765. // Update text color for the message
  766. if (lv_obj_get_child_cnt(bubble) > 0) {
  767. lv_obj_t* text = lv_obj_get_child(bubble, 0);
  768. if (text != nullptr) {
  769. // 根据气泡类型设置文本颜色
  770. if (strcmp(bubble_type, "system") == 0) {
  771. lv_obj_set_style_text_color(text, current_theme.system_text, 0);
  772. } else {
  773. lv_obj_set_style_text_color(text, current_theme.text, 0);
  774. }
  775. }
  776. }
  777. } else {
  778. // 如果没有标记,回退到之前的逻辑(颜色比较)
  779. // ...保留原有的回退逻辑...
  780. lv_color_t bg_color = lv_obj_get_style_bg_color(bubble, 0);
  781. // 改进bubble类型检测逻辑,不仅使用颜色比较
  782. bool is_user_bubble = false;
  783. bool is_assistant_bubble = false;
  784. bool is_system_bubble = false;
  785. // 检查用户bubble
  786. if (lv_color_eq(bg_color, DARK_USER_BUBBLE_COLOR) ||
  787. lv_color_eq(bg_color, LIGHT_USER_BUBBLE_COLOR) ||
  788. lv_color_eq(bg_color, current_theme.user_bubble)) {
  789. is_user_bubble = true;
  790. }
  791. // 检查系统bubble
  792. else if (lv_color_eq(bg_color, DARK_SYSTEM_BUBBLE_COLOR) ||
  793. lv_color_eq(bg_color, LIGHT_SYSTEM_BUBBLE_COLOR) ||
  794. lv_color_eq(bg_color, current_theme.system_bubble)) {
  795. is_system_bubble = true;
  796. }
  797. // 剩余的都当作助手bubble处理
  798. else {
  799. is_assistant_bubble = true;
  800. }
  801. // 根据bubble类型应用正确的颜色
  802. if (is_user_bubble) {
  803. lv_obj_set_style_bg_color(bubble, current_theme.user_bubble, 0);
  804. } else if (is_assistant_bubble) {
  805. lv_obj_set_style_bg_color(bubble, current_theme.assistant_bubble, 0);
  806. } else if (is_system_bubble) {
  807. lv_obj_set_style_bg_color(bubble, current_theme.system_bubble, 0);
  808. }
  809. // Update border color
  810. lv_obj_set_style_border_color(bubble, current_theme.border, 0);
  811. // Update text color for the message
  812. if (lv_obj_get_child_cnt(bubble) > 0) {
  813. lv_obj_t* text = lv_obj_get_child(bubble, 0);
  814. if (text != nullptr) {
  815. // 回退到颜色检测逻辑
  816. if (lv_color_eq(bg_color, current_theme.system_bubble) ||
  817. lv_color_eq(bg_color, DARK_SYSTEM_BUBBLE_COLOR) ||
  818. lv_color_eq(bg_color, LIGHT_SYSTEM_BUBBLE_COLOR)) {
  819. lv_obj_set_style_text_color(text, current_theme.system_text, 0);
  820. } else {
  821. lv_obj_set_style_text_color(text, current_theme.text, 0);
  822. }
  823. }
  824. }
  825. }
  826. }
  827. #else
  828. // Simple UI mode - just update the main chat message
  829. if (chat_message_label_ != nullptr) {
  830. lv_obj_set_style_text_color(chat_message_label_, current_theme.text, 0);
  831. }
  832. if (emotion_label_ != nullptr) {
  833. lv_obj_set_style_text_color(emotion_label_, current_theme.text, 0);
  834. }
  835. #endif
  836. }
  837. // Update low battery popup
  838. if (low_battery_popup_ != nullptr) {
  839. lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
  840. }
  841. // No errors occurred. Save theme to settings
  842. Display::SetTheme(theme_name);
  843. }