跳转至

旋转屏幕

功能介绍

官方文档链接

LVGL支持以90度为增量旋转显示器。

如果您选择软件旋转({sw_rotate}标志设置为 1),LVGL将为您执行旋转。您的驱动程序应该假设屏幕宽度和高度没有改变。只需像往常一样将像素刷新到显示器即可。软件旋转在您的{flush_cb}回调中不需要额外的逻辑。

在软件中执行旋转需要大量的开销,非必须不建议使用。

初始化时显示器的默认旋转可以使用{rotated}标志设置。可用的选项是{LV_DISP_ROT_NONE}、{LV_DISP_ROT_90}、{LV_DISP_ROT_180}或{LV_DISP_ROT_270}。旋转值与顺时针方向旋转物理显示器的方式有关。因此,{LV_DISP_ROT_90}表示您将硬件顺时针旋转90度,显示器逆时针旋转90度以进行补偿。

也可以在运行时使用{lv_disp_set_rotation(disp, rot)} API更改显示旋转。

示例代码

lv_port_disp.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void lv_port_disp_init(void)
{
    /*-------------------------
     * Initialize your display
     * -----------------------*/
    disp_init();

    /*-----------------------------
     * Create a buffer for drawing
     *----------------------------*/
    static lv_disp_draw_buf_t draw_buf_dsc_1;
    static lv_color_t buf_1[HOR_RES * 10];                              /*A buffer for 10 rows*/
    lv_disp_draw_buf_init(&draw_buf_dsc_1, buf_1, NULL, HOR_RES * 10);  /*Initialize the display buffer*/

    /*-----------------------------------
     * Register the display in LVGL
     *----------------------------------*/
    static lv_disp_drv_t disp_drv;  /*Descriptor of a display driver*/
    lv_disp_drv_init(&disp_drv);    /*Basic initialization*/

    /*Set up the functions to access to your display*/

    /*Set the resolution of the display*/
    disp_drv.hor_res = HOR_RES;
    disp_drv.ver_res = VER_RES;

    /*Used to copy the buffer's content to the display*/
    disp_drv.flush_cb = disp_flush;

    /*Set a display buffer*/
    disp_drv.draw_buf = &draw_buf_dsc_1;

    /*Required for Example 3)*/
    // disp_drv.full_refresh = 1;

    /*Rotate*/
    disp_drv.sw_rotate = 1;
    disp_drv.rotated = LV_DISP_ROT_90;

    /* Fill a memory array with a color if you have GPU.
     * Note that, in lv_conf.h you can enable GPUs that has built-in support in LVGL.
     * But if you have a different GPU you can use with this callback.*/
    //disp_drv.gpu_fill_cb = gpu_fill;

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}
main.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    /*!< 获取当前屏幕旋转角度 */
    lv_disp_rot_t rotation = lv_disp_get_rotation(NULL);
    if(rotation == LV_DISP_ROT_270)
        rotation = LV_DISP_ROT_NONE;
    else
        rotation++;

    if(code == LV_EVENT_CLICKED) {
        /*!< 设置当前屏幕旋转角度 */
        lv_disp_set_rotation(NULL, rotation);
    }
}

/**
 * @brief 在top_layer层创建一个按键,点击把屏幕旋转90度
 * 
 */
void lv_btn_rotate(void)
{
    lv_obj_t * label;

    lv_obj_t * btn = lv_btn_create(lv_layer_top());
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_align(btn, LV_ALIGN_CENTER, 0, 0);

    label = lv_label_create(btn);
    lv_label_set_text(label, "rotate");
    lv_obj_center(label);
}

int main(void)
{
    SystemInit();
    uart_init();
    systick_init();

    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();
    // lv_port_fs_init();

#if LV_USE_DEMO_WIDGETS
    lv_demo_widgets();
#elif LV_USE_DEMO_BENCHMARK
    lv_demo_benchmark();
#endif

    lv_btn_rotate();
    while (1 == 1)
    {
        lv_timer_handler();
        gui2show();
    }
}

演示视频

旋转屏幕演示视频