LVGL学习之初始化和屏幕旋转

背景

手里有一块ESP32-S2-HMI-DevKit-1开发板,是乐鑫基于ESP32-S2模组开发的HMI人机交互方案开发板; 一边学习ESP32开发的过程中,也顺带学习一下LVGL图形库; 遇到的第一个问题: 官方提供的例程都是横屏显示的,我想切换为竖屏显示,就这么简单。

开发环境

    Windows 10 VS Code + esp-idf-v4.3 ESP32-S2-HMI-DevKit-1开发板

正文

1. 关于开发板的LVGL初始化

首先,开发板Demo中的main函数里调用了lvgl_init()的函数,

这个函数定义在lvlg_port.c文件中, 在其源码里, 实现了上面所列文档中的大部分步骤:

    Call lv_init() Create a draw buffer: LVGL will render the graphics here first, and send the rendered image to the display. Implement and register a function which can copy the rendered image to an area of your display. Implement and register a function which can read an input device. Call lv_timer_handler() periodically every few milliseconds in the main while(1) loop or in an operating system task. It will redraw the screen if required, handle input devices, animation etc.

2. 关于屏幕旋转

中有描述: 翻译并提炼一下:

    设置sw_rotate标志为1, 可软件实现屏幕旋转 LV_DISP_ROT_NONE, LV_DISP_ROT_90, LV_DISP_ROT_180, or LV_DISP_ROT_270 这些宏定义代表了旋转角度 程序运行时, 可通过lv_disp_set_rotation(disp, rot)函数来改变旋转角度

3. 代码实现

ESP32的LVGL初始化过程都在lvgl_init()函数中,可追溯进去了解;

而sw_rotate和旋转角度实际上是lv_disp_drv_t结构体中的一个成员, 此结构体的定义在…/lv_hal/lv_hal_disp.h文件中;

/**
* Display Driver structure to be registered by HAL
*/
typedef struct _disp_drv_t {
          
   

lv_coord_t hor_res; /**< Horizontal resolution. */
lv_coord_t ver_res; /**< Vertical resolution. */

/** Pointer to a buffer initialized with `lv_disp_buf_init()`.
* LVGL will use this buffer(s) to draw the screens contents */
lv_disp_buf_t * buffer;

#if LV_ANTIALIAS
uint32_t antialiasing : 1; /**< 1: antialiasing is enabled on this display. */
#endif
uint32_t rotated : 2;
uint32_t sw_rotate : 1; /**< 1: use software rotation (slower) */
......
...
.
} lv_disp_drv_t;

在lvgl_init()函数中,可以找到lv_disp_drv_t结构体定义的一个变量disp_drv,修改disp_drv这两个参数的值即可实现旋转;

...
/*Create a display*/
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.buffer = &disp_buf;
disp_drv.flush_cb = lvgl_flush_cb;
disp_drv.sw_rotate = 1;   // add for rotation
disp_drv.rotated = LV_DISP_ROT_90;   // add for rotation
lv_disp_drv_register(&disp_drv);
...

4. 关于运行时旋转屏幕

可以在运行过程中,某个条件触发时, 调用lv_disp_set_rotation(NULL, LV_DISP_ROT_NONE);函数即可旋转到第二个参数的角度。


参考

经验分享 程序员 微信小程序 职场和发展