Skip to content

Commit b891d4b

Browse files
author
zomint
authored
Merge pull request nulllaborg#6 from vonweller/128-32OLED
修复128*32屏幕显示文字不完全的问题 适配OLED各种屏幕
2 parents 6f70492 + 419c02f commit b891d4b

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

examples/ai_voice_display_oled/display.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ void Display::Start() {
5757
lv_obj_set_style_text_font(screen, &font_puhui_14_1, 0);
5858
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
5959

60+
// 自适应布局计算
61+
// 基于屏幕尺寸计算合适的布局参数
62+
int status_bar_height = 1;
63+
int content_left_width = 1;
64+
int emotion_top_padding = 1;
65+
int chat_message_top_padding = 1;
66+
const lv_font_t* emotion_font = &font_awesome_14_1;
67+
68+
// 根据屏幕高度自适应调整布局
69+
if (height_ <= 32) {
70+
// 小屏幕 (128x32 等)
71+
status_bar_height = height_ * 0.375; // 约37.5% (32*0.375=12)
72+
content_left_width = width_ * 0.15625; // 约15.6% (128*0.15625=20)
73+
emotion_top_padding = 2;
74+
chat_message_top_padding = 2;
75+
emotion_font = &font_awesome_14_1;
76+
} else if (height_ <= 64) {
77+
// 中等屏幕 (128x64 等)
78+
status_bar_height = height_ * 0.25; // 25% (64*0.25=16)
79+
content_left_width = width_ * 0.25; // 25% (128*0.25=32)
80+
emotion_top_padding = height_ * 0.125; // 12.5% (64*0.125=8)
81+
chat_message_top_padding = height_ * 0.21875; // 约21.9% (64*0.21875≈14)
82+
emotion_font = &font_awesome_30_1;
83+
} else {
84+
// 大屏幕 (128x128 等)
85+
status_bar_height = height_ * 0.125; // 12.5%
86+
content_left_width = width_ * 0.25; // 25%
87+
emotion_top_padding = height_ * 0.0625; // 6.25%
88+
chat_message_top_padding = height_ * 0.109375; // 约10.9%
89+
emotion_font = &font_awesome_30_1;
90+
}
91+
6092
/* Container */
6193
container_ = lv_obj_create(screen);
6294
lv_obj_set_size(container_, LV_HOR_RES, LV_VER_RES);
@@ -65,14 +97,14 @@ void Display::Start() {
6597
lv_obj_set_style_border_width(container_, 0, 0);
6698
lv_obj_set_style_pad_row(container_, 0, 0);
6799

68-
/* Status bar */
100+
/* Status bar - 自适应高度 */
69101
status_bar_ = lv_obj_create(container_);
70-
lv_obj_set_size(status_bar_, LV_HOR_RES, 16);
102+
lv_obj_set_size(status_bar_, LV_HOR_RES, status_bar_height);
71103
lv_obj_set_style_border_width(status_bar_, 0, 0);
72104
lv_obj_set_style_pad_all(status_bar_, 0, 0);
73105
lv_obj_set_style_radius(status_bar_, 0, 0);
74106

75-
/* Content */
107+
/* Content - 剩余空间自动分配 */
76108
content_ = lv_obj_create(container_);
77109
lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
78110
lv_obj_set_style_radius(content_, 0, 0);
@@ -82,17 +114,17 @@ void Display::Start() {
82114
lv_obj_set_flex_flow(content_, LV_FLEX_FLOW_ROW);
83115
lv_obj_set_style_flex_main_place(content_, LV_FLEX_ALIGN_CENTER, 0);
84116

85-
// 创建左侧固定宽度的容器
117+
// 创建左侧固定宽度的容器 - 自适应宽度
86118
content_left_ = lv_obj_create(content_);
87-
lv_obj_set_size(content_left_, 32, LV_SIZE_CONTENT); // 固定宽度32像素
119+
lv_obj_set_size(content_left_, content_left_width, LV_SIZE_CONTENT);
88120
lv_obj_set_style_pad_all(content_left_, 0, 0);
89121
lv_obj_set_style_border_width(content_left_, 0, 0);
90122

91123
emotion_label_ = lv_label_create(content_left_);
92-
lv_obj_set_style_text_font(emotion_label_, &font_awesome_30_1, 0);
124+
lv_obj_set_style_text_font(emotion_label_, emotion_font, 0); // 自适应字体,添加缺失的第三个参数
93125
lv_label_set_text(emotion_label_, FONT_AWESOME_AI_CHIP);
94126
lv_obj_center(emotion_label_);
95-
lv_obj_set_style_pad_top(emotion_label_, 8, 0);
127+
lv_obj_set_style_pad_top(emotion_label_, emotion_top_padding, 0); // 自适应上边距
96128

97129
content_right_ = lv_obj_create(content_);
98130
lv_obj_set_size(content_right_, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
@@ -105,8 +137,8 @@ void Display::Start() {
105137
lv_label_set_text(chat_message_label_, "");
106138
lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
107139
lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_LEFT, 0);
108-
lv_obj_set_width(chat_message_label_, width_ - 32);
109-
lv_obj_set_style_pad_top(chat_message_label_, 14, 0);
140+
lv_obj_set_width(chat_message_label_, width_ - content_left_width); // 自适应宽度
141+
lv_obj_set_style_pad_top(chat_message_label_, chat_message_top_padding, 0); // 自适应上边距
110142

111143
// 延迟一定的时间后开始滚动字幕
112144
static lv_anim_t a;

0 commit comments

Comments
 (0)