【平头哥RVB2601创意应用开发】OLED显示温湿度
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">上篇我们测试了下使用SHT20读取温湿度,目前使用了串口输出,正好板子上有一块OLED显示屏幕,正好本次就测试下使用OLED屏幕显示读取到的温湿度值。</span></span></p><pre>
<code>void oled_init()
{
oled_pinmux_init();
oled_gpio_init();
oled_initialize();
// lv_disp porting
/*Create a display buffer*/
lv_disp_buf_init(&disp_buf1, buf1, buf2, 64 * 128);
/*Create a display*/
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.buffer = &disp_buf1;
disp_drv.flush_cb = oled_flush;
disp_drv.rotated = 0;
lv_disp_drv_register(&disp_drv);
}</code></pre>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">我们本次就是用LVGL库进行操作我们的OLED显示屏幕。初始化我们的LVGL是在我们的oled_init函数中的。主要是初始化一个缓冲区给LVGL。</span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">任务函数中添加上我们的GUI任务。</span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">之后主要就是创建我们的LVGL中的label。也就是我们要显示的字符。 </span></span></p>
<pre>
<code> guider_ui.p1 = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(guider_ui.p1, LV_LABEL_LONG_BREAK);
lv_label_set_align(guider_ui.p1, LV_LABEL_ALIGN_CENTER);
lv_obj_set_pos(guider_ui.p1, 0, 34);
lv_obj_set_size(guider_ui.p1, 60, 10);
lv_label_set_text(guider_ui.p1, "HUMI:");
guider_ui.humi = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(guider_ui.humi, LV_LABEL_LONG_BREAK);
lv_label_set_align(guider_ui.humi, LV_LABEL_ALIGN_CENTER);
lv_obj_set_pos(guider_ui.humi, 64, 34);
lv_obj_set_size(guider_ui.humi, 60, 10);
lv_label_set_text(guider_ui.humi, "100%");
guider_ui.p2 = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(guider_ui.p2, LV_LABEL_LONG_BREAK);
lv_label_set_align(guider_ui.p2, LV_LABEL_ALIGN_CENTER);
lv_obj_set_pos(guider_ui.p2, 0, 4);
lv_obj_set_size(guider_ui.p2, 60, 10);
lv_label_set_text(guider_ui.p2, "TEMP:");
guider_ui.temp = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(guider_ui.temp, LV_LABEL_LONG_BREAK);
lv_label_set_align(guider_ui.temp, LV_LABEL_ALIGN_CENTER);
lv_obj_set_pos(guider_ui.temp, 64, 4);
lv_obj_set_size(guider_ui.temp, 60, 10);
lv_label_set_text(guider_ui.temp, "30C");</code></pre>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">一共是添加了4个。两个用来标识温度还是湿度的。两个是我们显示的数值。</span></span></p>
<pre>
<code>void set_label_temp_value(uint16_t value)
{
lv_label_set_text_fmt(guider_ui.temp, "%d.%d C",value/10,value%10);
}
void set_label_humi_value(uint16_t value)
{
lv_label_set_text_fmt(guider_ui.humi, "%d.%d %%",value/10,value%10);
}</code></pre>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">之后就是两个刷新label的函数,通过读取温湿度传感器之后,修改相应的label数值。</span></span></p>
<p style="text-indent:21.0pt; text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">最终效果如下:</span></span></p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p style="text-indent:21.0pt; text-align:justify"><br />
</p>
<p style="text-indent:21.0pt; text-align:justify"> </p>
<p>用LVGL库进行操作屏幕楼主有什么感受</p>
页:
[1]