oled_display.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "oled_display.h"
  2. #include "font_awesome_symbols.h"
  3. #include "assets/lang_config.h"
  4. #include <string>
  5. #include <algorithm>
  6. #include <esp_log.h>
  7. #include <esp_err.h>
  8. #include <esp_lvgl_port.h>
  9. #define TAG "OledDisplay"
  10. LV_FONT_DECLARE(font_awesome_30_1);
  11. OledDisplay::OledDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  12. int width, int height, bool mirror_x, bool mirror_y, DisplayFonts fonts)
  13. : panel_io_(panel_io), panel_(panel), fonts_(fonts) {
  14. width_ = width;
  15. height_ = height;
  16. ESP_LOGI(TAG, "Initialize LVGL");
  17. lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  18. port_cfg.task_priority = 1;
  19. lvgl_port_init(&port_cfg);
  20. ESP_LOGI(TAG, "Adding LCD screen");
  21. const lvgl_port_display_cfg_t display_cfg = {
  22. .io_handle = panel_io_,
  23. .panel_handle = panel_,
  24. .control_handle = nullptr,
  25. .buffer_size = static_cast<uint32_t>(width_ * height_),
  26. .double_buffer = false,
  27. .trans_size = 0,
  28. .hres = static_cast<uint32_t>(width_),
  29. .vres = static_cast<uint32_t>(height_),
  30. .monochrome = true,
  31. .rotation = {
  32. .swap_xy = false,
  33. .mirror_x = mirror_x,
  34. .mirror_y = mirror_y,
  35. },
  36. .flags = {
  37. .buff_dma = 1,
  38. .buff_spiram = 0,
  39. .sw_rotate = 0,
  40. .full_refresh = 0,
  41. .direct_mode = 0,
  42. },
  43. };
  44. display_ = lvgl_port_add_disp(&display_cfg);
  45. if (display_ == nullptr) {
  46. ESP_LOGE(TAG, "Failed to add display");
  47. return;
  48. }
  49. if (height_ == 64) {
  50. SetupUI_128x64();
  51. } else {
  52. SetupUI_128x32();
  53. }
  54. }
  55. OledDisplay::~OledDisplay() {
  56. if (content_ != nullptr) {
  57. lv_obj_del(content_);
  58. }
  59. if (status_bar_ != nullptr) {
  60. lv_obj_del(status_bar_);
  61. }
  62. if (side_bar_ != nullptr) {
  63. lv_obj_del(side_bar_);
  64. }
  65. if (container_ != nullptr) {
  66. lv_obj_del(container_);
  67. }
  68. if (panel_ != nullptr) {
  69. esp_lcd_panel_del(panel_);
  70. }
  71. if (panel_io_ != nullptr) {
  72. esp_lcd_panel_io_del(panel_io_);
  73. }
  74. lvgl_port_deinit();
  75. }
  76. bool OledDisplay::Lock(int timeout_ms) {
  77. return lvgl_port_lock(timeout_ms);
  78. }
  79. void OledDisplay::Unlock() {
  80. lvgl_port_unlock();
  81. }
  82. void OledDisplay::SetChatMessage(const char* role, const char* content) {
  83. DisplayLockGuard lock(this);
  84. if (chat_message_label_ == nullptr) {
  85. return;
  86. }
  87. // Replace all newlines with spaces
  88. std::string content_str = content;
  89. std::replace(content_str.begin(), content_str.end(), '\n', ' ');
  90. if (content_right_ == nullptr) {
  91. lv_label_set_text(chat_message_label_, content_str.c_str());
  92. } else {
  93. if (content == nullptr || content[0] == '\0') {
  94. lv_obj_add_flag(content_right_, LV_OBJ_FLAG_HIDDEN);
  95. } else {
  96. lv_label_set_text(chat_message_label_, content_str.c_str());
  97. lv_obj_clear_flag(content_right_, LV_OBJ_FLAG_HIDDEN);
  98. }
  99. }
  100. }
  101. void OledDisplay::SetupUI_128x64() {
  102. DisplayLockGuard lock(this);
  103. auto screen = lv_screen_active();
  104. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  105. lv_obj_set_style_text_color(screen, lv_color_black(), 0);
  106. /* Container */
  107. container_ = lv_obj_create(screen);
  108. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  109. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_COLUMN);
  110. lv_obj_set_style_pad_all(container_, 0, 0);
  111. lv_obj_set_style_border_width(container_, 0, 0);
  112. lv_obj_set_style_pad_row(container_, 0, 0);
  113. /* Status bar */
  114. status_bar_ = lv_obj_create(container_);
  115. lv_obj_set_size(status_bar_, LV_HOR_RES, 16);
  116. lv_obj_set_style_border_width(status_bar_, 0, 0);
  117. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  118. lv_obj_set_style_radius(status_bar_, 0, 0);
  119. /* Content */
  120. content_ = lv_obj_create(container_);
  121. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  122. lv_obj_set_style_radius(content_, 0, 0);
  123. lv_obj_set_style_pad_all(content_, 0, 0);
  124. lv_obj_set_width(content_, LV_HOR_RES);
  125. lv_obj_set_flex_grow(content_, 1);
  126. lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_ROW);
  127. lv_obj_set_style_flex_main_place(content_, LV_FLEX_ALIGN_CENTER, 0);
  128. // 创建左侧固定宽度的容器
  129. content_left_ = lv_obj_create(content_);
  130. lv_obj_set_size(content_left_, 32, LV_SIZE_CONTENT); // 固定宽度32像素
  131. lv_obj_set_style_pad_all(content_left_, 0, 0);
  132. lv_obj_set_style_border_width(content_left_, 0, 0);
  133. emotion_label_ = lv_label_create(content_left_);
  134. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0);
  135. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  136. lv_obj_center(emotion_label_);
  137. lv_obj_set_style_pad_top(emotion_label_, 8, 0);
  138. // 创建右侧可扩展的容器
  139. content_right_ = lv_obj_create(content_);
  140. lv_obj_set_size(content_right_, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
  141. lv_obj_set_style_pad_all(content_right_, 0, 0);
  142. lv_obj_set_style_border_width(content_right_, 0, 0);
  143. lv_obj_set_flex_grow(content_right_, 1);
  144. lv_obj_add_flag(content_right_, LV_OBJ_FLAG_HIDDEN);
  145. chat_message_label_ = lv_label_create(content_right_);
  146. lv_label_set_text(chat_message_label_, "");
  147. lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  148. lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_LEFT, 0);
  149. lv_obj_set_width(chat_message_label_, width_ - 32);
  150. lv_obj_set_style_pad_top(chat_message_label_, 14, 0);
  151. // 延迟一定的时间后开始滚动字幕
  152. static lv_anim_t a;
  153. lv_anim_init(&a);
  154. lv_anim_set_delay(&a, 1000);
  155. lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
  156. lv_obj_set_style_anim(chat_message_label_, &a, LV_PART_MAIN);
  157. lv_obj_set_style_anim_duration(chat_message_label_, lv_anim_speed_clamped(60, 300, 60000), LV_PART_MAIN);
  158. /* Status bar */
  159. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  160. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  161. lv_obj_set_style_border_width(status_bar_, 0, 0);
  162. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  163. network_label_ = lv_label_create(status_bar_);
  164. lv_label_set_text(network_label_, "");
  165. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  166. notification_label_ = lv_label_create(status_bar_);
  167. lv_obj_set_flex_grow(notification_label_, 1);
  168. lv_obj_set_style_text_align(notification_label_, LV_TEXT_ALIGN_CENTER, 0);
  169. lv_label_set_text(notification_label_, "");
  170. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  171. status_label_ = lv_label_create(status_bar_);
  172. lv_obj_set_flex_grow(status_label_, 1);
  173. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  174. lv_obj_set_style_text_align(status_label_, LV_TEXT_ALIGN_CENTER, 0);
  175. mute_label_ = lv_label_create(status_bar_);
  176. lv_label_set_text(mute_label_, "");
  177. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  178. battery_label_ = lv_label_create(status_bar_);
  179. lv_label_set_text(battery_label_, "");
  180. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  181. low_battery_popup_ = lv_obj_create(screen);
  182. lv_obj_set_scrollbar_mode(low_battery_popup_, LV_SCROLLBAR_MODE_OFF);
  183. lv_obj_set_size(low_battery_popup_, LV_HOR_RES * 0.9, fonts_.text_font->line_height * 2);
  184. lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
  185. lv_obj_set_style_bg_color(low_battery_popup_, lv_color_black(), 0);
  186. lv_obj_set_style_radius(low_battery_popup_, 10, 0);
  187. lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
  188. lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
  189. lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
  190. lv_obj_center(low_battery_label);
  191. lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
  192. }
  193. void OledDisplay::SetupUI_128x32() {
  194. DisplayLockGuard lock(this);
  195. auto screen = lv_screen_active();
  196. lv_obj_set_style_text_font(screen, fonts_.text_font, 0);
  197. /* Container */
  198. container_ = lv_obj_create(screen);
  199. lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
  200. lv_obj_set_flex_flow(container_, LV_FLEX_FLOW_ROW);
  201. lv_obj_set_style_pad_all(container_, 0, 0);
  202. lv_obj_set_style_border_width(container_, 0, 0);
  203. lv_obj_set_style_pad_column(container_, 0, 0);
  204. /* Emotion label on the left side */
  205. content_ = lv_obj_create(container_);
  206. lv_obj_set_size(content_, 32, 32);
  207. lv_obj_set_style_pad_all(content_, 0, 0);
  208. lv_obj_set_style_border_width(content_, 0, 0);
  209. lv_obj_set_style_radius(content_, 0, 0);
  210. emotion_label_ = lv_label_create(content_);
  211. lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0);
  212. lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
  213. lv_obj_center(emotion_label_);
  214. /* Right side */
  215. side_bar_ = lv_obj_create(container_);
  216. lv_obj_set_size(side_bar_, width_ - 32, 32);
  217. lv_obj_set_flex_flow(side_bar_, LV_FLEX_FLOW_COLUMN);
  218. lv_obj_set_style_pad_all(side_bar_, 0, 0);
  219. lv_obj_set_style_border_width(side_bar_, 0, 0);
  220. lv_obj_set_style_radius(side_bar_, 0, 0);
  221. lv_obj_set_style_pad_row(side_bar_, 0, 0);
  222. /* Status bar */
  223. status_bar_ = lv_obj_create(side_bar_);
  224. lv_obj_set_size(status_bar_, width_ - 32, 16);
  225. lv_obj_set_style_radius(status_bar_, 0, 0);
  226. lv_obj_set_flex_flow(status_bar_, LV_FLEX_FLOW_ROW);
  227. lv_obj_set_style_pad_all(status_bar_, 0, 0);
  228. lv_obj_set_style_border_width(status_bar_, 0, 0);
  229. lv_obj_set_style_pad_column(status_bar_, 0, 0);
  230. status_label_ = lv_label_create(status_bar_);
  231. lv_obj_set_flex_grow(status_label_, 1);
  232. lv_obj_set_style_pad_left(status_label_, 2, 0);
  233. lv_label_set_text(status_label_, Lang::Strings::INITIALIZING);
  234. notification_label_ = lv_label_create(status_bar_);
  235. lv_obj_set_flex_grow(notification_label_, 1);
  236. lv_obj_set_style_pad_left(notification_label_, 2, 0);
  237. lv_label_set_text(notification_label_, "");
  238. lv_obj_add_flag(notification_label_, LV_OBJ_FLAG_HIDDEN);
  239. mute_label_ = lv_label_create(status_bar_);
  240. lv_label_set_text(mute_label_, "");
  241. lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
  242. network_label_ = lv_label_create(status_bar_);
  243. lv_label_set_text(network_label_, "");
  244. lv_obj_set_style_text_font(network_label_, fonts_.icon_font, 0);
  245. battery_label_ = lv_label_create(status_bar_);
  246. lv_label_set_text(battery_label_, "");
  247. lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
  248. chat_message_label_ = lv_label_create(side_bar_);
  249. lv_obj_set_size(chat_message_label_, width_ - 32, LV_SIZE_CONTENT);
  250. lv_obj_set_style_pad_left(chat_message_label_, 2, 0);
  251. lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  252. lv_label_set_text(chat_message_label_, "");
  253. // 延迟一定的时间后开始滚动字幕
  254. static lv_anim_t a;
  255. lv_anim_init(&a);
  256. lv_anim_set_delay(&a, 1000);
  257. lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
  258. lv_obj_set_style_anim(chat_message_label_, &a, LV_PART_MAIN);
  259. lv_obj_set_style_anim_duration(chat_message_label_, lv_anim_speed_clamped(60, 300, 60000), LV_PART_MAIN);
  260. }