oled_display.cc 11 KB

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