lcd_display.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "board.h"
  9. #define TAG "LcdDisplay"
  10. LV_FONT_DECLARE(font_awesome_30_4);
  11. SpiLcdDisplay::SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  12. int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy,
  13. DisplayFonts fonts)
  14. : LcdDisplay(panel_io, panel, fonts) {
  15. width_ = width;
  16. height_ = height;
  17. // draw white
  18. std::vector<uint16_t> buffer(width_, 0xFFFF);
  19. for (int y = 0; y < height_; y++) {
  20. esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  21. }
  22. // Set the display to on
  23. ESP_LOGI(TAG, "Turning display on");
  24. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  25. ESP_LOGI(TAG, "Initialize LVGL library");
  26. lv_init();
  27. ESP_LOGI(TAG, "Initialize LVGL port");
  28. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  29. port_cfg.task_priority = 1;
  30. lvgl_port_init(&port_cfg);
  31. ESP_LOGI(TAG, "Adding LCD screen");
  32. const lvgl_port_display_cfg_t display_cfg = {
  33. .io_handle = panel_io_,
  34. .panel_handle = panel_,
  35. .control_handle = nullptr,
  36. .buffer_size = static_cast<uint32_t>(width_ * 10),
  37. .double_buffer = false,
  38. .trans_size = 0,
  39. .hres = static_cast<uint32_t>(width_),
  40. .vres = static_cast<uint32_t>(height_),
  41. .monochrome = false,
  42. .rotation = {
  43. .swap_xy = swap_xy,
  44. .mirror_x = mirror_x,
  45. .mirror_y = mirror_y,
  46. },
  47. .color_format = LV_COLOR_FORMAT_RGB565,
  48. .flags = {
  49. .buff_dma = 1,
  50. .buff_spiram = 0,
  51. .sw_rotate = 0,
  52. .swap_bytes = 1,
  53. .full_refresh = 0,
  54. .direct_mode = 0,
  55. },
  56. };
  57. display_ = lvgl_port_add_disp(&display_cfg);
  58. if (display_ == nullptr) {
  59. ESP_LOGE(TAG, "Failed to add display");
  60. return;
  61. }
  62. if (offset_x != 0 || offset_y != 0) {
  63. lv_display_set_offset(display_, offset_x, offset_y);
  64. }
  65. SetupUI();
  66. }
  67. // RGB LCD实现
  68. RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  69. int width, int height, int offset_x, int offset_y,
  70. bool mirror_x, bool mirror_y, bool swap_xy,
  71. DisplayFonts fonts)
  72. : LcdDisplay(panel_io, panel, fonts) {
  73. width_ = width;
  74. height_ = height;
  75. // draw white
  76. std::vector<uint16_t> buffer(width_, 0xFFFF);
  77. for (int y = 0; y < height_; y++) {
  78. esp_lcd_panel_draw_bitmap(panel_, 0, y, width_, y + 1, buffer.data());
  79. }
  80. ESP_LOGI(TAG, "Initialize LVGL library");
  81. lv_init();
  82. ESP_LOGI(TAG, "Initialize LVGL port");
  83. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  84. port_cfg.task_priority = 1;
  85. lvgl_port_init(&port_cfg);
  86. ESP_LOGI(TAG, "Adding LCD screen");
  87. const lvgl_port_display_cfg_t display_cfg = {
  88. .io_handle = panel_io_,
  89. .panel_handle = panel_,
  90. .buffer_size = static_cast<uint32_t>(width_ * 10),
  91. .double_buffer = true,
  92. .hres = static_cast<uint32_t>(width_),
  93. .vres = static_cast<uint32_t>(height_),
  94. .rotation = {
  95. .swap_xy = swap_xy,
  96. .mirror_x = mirror_x,
  97. .mirror_y = mirror_y,
  98. },
  99. .flags = {
  100. .buff_dma = 1,
  101. .swap_bytes = 0,
  102. .full_refresh = 1,
  103. .direct_mode = 1,
  104. },
  105. };
  106. const lvgl_port_display_rgb_cfg_t rgb_cfg = {
  107. .flags = {
  108. .bb_mode = true,
  109. .avoid_tearing = true,
  110. }
  111. };
  112. display_ = lvgl_port_add_disp_rgb(&display_cfg, &rgb_cfg);
  113. if (display_ == nullptr) {
  114. ESP_LOGE(TAG, "Failed to add RGB display");
  115. return;
  116. }
  117. if (offset_x != 0 || offset_y != 0) {
  118. lv_display_set_offset(display_, offset_x, offset_y);
  119. }
  120. SetupUI();
  121. }
  122. LcdDisplay::~LcdDisplay() {
  123. // 然后再清理 LVGL 对象
  124. if (content_ != nullptr) {
  125. lv_obj_del(content_);
  126. }
  127. if (status_bar_ != nullptr) {
  128. lv_obj_del(status_bar_);
  129. }
  130. if (side_bar_ != nullptr) {
  131. lv_obj_del(side_bar_);
  132. }
  133. if (container_ != nullptr) {
  134. lv_obj_del(container_);
  135. }
  136. if (display_ != nullptr) {
  137. lv_display_delete(display_);
  138. }
  139. if (panel_ != nullptr) {
  140. esp_lcd_panel_del(panel_);
  141. }
  142. if (panel_io_ != nullptr) {
  143. esp_lcd_panel_io_del(panel_io_);
  144. }
  145. }
  146. bool LcdDisplay::Lock(int timeout_ms) {
  147. return lvgl_port_lock(timeout_ms);
  148. }
  149. void LcdDisplay::Unlock() {
  150. lvgl_port_unlock();
  151. }
  152. void LcdDisplay::SetupUI() {
  153. DisplayLockGuard lock(this);
  154. auto screen = lv_screen_active();
  155. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  156. lv_obj_set_style_text_color(screen, lv_color_black(), 0);
  157. /* Container */
  158. container_ = lv_obj_create(screen);
  159. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  160. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
  161. lv_obj_set_style_pad_all(container_, 0, 0);
  162. lv_obj_set_style_border_width(container_, 0, 0);
  163. lv_obj_set_style_pad_row(container_, 0, 0);
  164. /* Status bar */
  165. status_bar_ = lv_obj_create(container_);
  166. lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.text_font->line_height);
  167. lv_obj_set_style_radius(status_bar_, 0, 0);
  168. /* Content */
  169. content_ = lv_obj_create(container_);
  170. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  171. lv_obj_set_style_radius(content_, 0, 0);
  172. lv_obj_set_width(content_, LV_HOR_RES);
  173. lv_obj_set_flex_grow(content_, 1);
  174. lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_COLUMN); // 垂直布局(从上到下)
  175. lv_obj_set_flex_align(content_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_EVENLY); // 子对象居中对齐,等距分布
  176. emotion_label_ = lv_label_create(content_);
  177. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  178. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  179. chat_message_label_ = lv_label_create(content_);
  180. lv_label_set_text(chat_message_label_, "");
  181. lv_obj_set_width(chat_message_label_, LV_HOR_RES * 0.9); // 限制宽度为屏幕宽度的 90%
  182. lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_WRAP); // 设置为自动换行模式
  183. lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_CENTER, 0); // 设置文本居中对齐
  184. /* Status bar */
  185. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  186. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  187. lv_obj_set_style_border_width(status_bar_, 0, 0);
  188. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  189. lv_obj_set_style_pad_left(status_bar_, 2, 0);
  190. lv_obj_set_style_pad_right(status_bar_, 2, 0);
  191. network_label_ = lv_label_create(status_bar_);
  192. lv_label_set_text(network_label_, "");
  193. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  194. notification_label_ = lv_label_create(status_bar_);
  195. lv_obj_set_flex_grow(notification_label_, 1);
  196. lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
  197. lv_label_set_text(notification_label_, "");
  198. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  199. status_label_ = lv_label_create(status_bar_);
  200. lv_obj_set_flex_grow(status_label_, 1);
  201. lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  202. lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
  203. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  204. mute_label_ = lv_label_create(status_bar_);
  205. lv_label_set_text(mute_label_, "");
  206. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  207. battery_label_ = lv_label_create(status_bar_);
  208. lv_label_set_text(battery_label_, "");
  209. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  210. low_battery_popup_ = lv_obj_create(screen);
  211. lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
  212. lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
  213. lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
  214. lv_obj_set_style_bg_color(low_battery_popup_, lv_color_black(), 0);
  215. lv_obj_set_style_radius(low_battery_popup_, 10, 0);
  216. lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
  217. lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
  218. lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
  219. lv_obj_center(low_battery_label);
  220. lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
  221. }
  222. void LcdDisplay::SetEmotion(const char* emotion) {
  223. struct Emotion {
  224. const char* icon;
  225. const char* text;
  226. };
  227. static const std::vector<Emotion> emotions = {
  228. {"😶", "neutral"},
  229. {"🙂", "happy"},
  230. {"😆", "laughing"},
  231. {"😂", "funny"},
  232. {"😔", "sad"},
  233. {"😠", "angry"},
  234. {"😭", "crying"},
  235. {"😍", "loving"},
  236. {"😳", "embarrassed"},
  237. {"😯", "surprised"},
  238. {"😱", "shocked"},
  239. {"🤔", "thinking"},
  240. {"😉", "winking"},
  241. {"😎", "cool"},
  242. {"😌", "relaxed"},
  243. {"🤤", "delicious"},
  244. {"😘", "kissy"},
  245. {"😏", "confident"},
  246. {"😴", "sleepy"},
  247. {"😜", "silly"},
  248. {"🙄", "confused"}
  249. };
  250. // 查找匹配的表情
  251. std::string_view emotion_view(emotion);
  252. auto it = std::find_if(emotions.begin(), emotions.end(),
  253. [&emotion_view](const Emotion& e) { return e.text == emotion_view; });
  254. DisplayLockGuard lock(this);
  255. if (emotion_label_ == nullptr) {
  256. return;
  257. }
  258. // 如果找到匹配的表情就显示对应图标,否则显示默认的neutral表情
  259. lv_obj_set_style_text_font(emotion_label_, fonts_.emoji_font, 0);
  260. if (it != emotions.end()) {
  261. lv_label_set_text(emotion_label_, it->icon);
  262. } else {
  263. lv_label_set_text(emotion_label_, "😶");
  264. }
  265. }
  266. void LcdDisplay::SetIcon(const char* icon) {
  267. DisplayLockGuard lock(this);
  268. if (emotion_label_ == nullptr) {
  269. return;
  270. }
  271. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_4, 0);
  272. lv_label_set_text(emotion_label_, icon);
  273. }