Last Edited: 2026/2/26 20:47

1
2
3
4
5
6
#pragma once
/* Series Control */
#include "SERIES.hpp"
/* Third Party */
#include "src/core/lv_obj.h"
#include "src/hal/lv_hal_disp.h"

需要引两个 LVGL 头文件支持类型的使用。

1. 类型定义

1
2
3
4
5
6
7
class LVGL_UI
{
    public:
    /* * TypeDefs */
    using flush_cb = void (*)( lv_disp_drv_t* disp_drv,
    const lv_area_t* area,    
                               lv_color_t*    color_p);

首先,flush 回调函数的逻辑是需要由用户提供的,这里设置成模版文件中 flush 的标准形式,也就是入口参数为

  • 显示驱动的实例
  • 需要渲染的区域
  • 像素数据指针
1
2
3
    using pixel = lv_color_t; // LVGL Buffer/Pixel Type
    using res   = uint16_t;
    using coord = int16_t;

设置一些常见的数据类型:

  • pixel - 缓冲区数组以及其中像素数据的类型,本质上是 lv_color_t ,受到 lv_conf.h 控制
  • res - 分辨率
  • coord - 像素坐标

2. 一些成员

1
2
    lv_disp_drv_t      disp_drv;   // LVGL Display Driver Instance
    lv_disp_draw_buf_t render_buf; // LVGL render Buffer info structure/Instance

首先就是 disp_drv,也就是 LVGL8.4 中的显示屏实例,然后是 render_buf ,根据其类型可以知道是 LVGL 用于存储缓冲区信息的结构体(而非缓冲区数组本体)。

3. 构造体

1
2
/* * Constructor */
    LVGL_UI(){}

4. 注册函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <size_t N>
    void UI_Init( res      res_h,          
                  res      res_v,          
                  flush_cb flush_cb,        
                  pixel    (&buf1)[N],      
                  pixel     *buf2 = nullptr)
    {
        /* * Hardware Initialization must be done before entry */
        static bool isInited{false};
        if(!isInited)lv_init(),isInited = true;
        lv_disp_drv_init(&disp_drv);      
        lv_disp_draw_buf_init( &render_buf,
                               buf1,        
                               buf2,        
                               N         );
        disp_drv.hor_res   = res_h;        
        disp_drv.ver_res   = res_v;      
        disp_drv.user_data = this;        
        disp_drv.flush_cb  = flush_cb;    
        disp_drv.draw_buf  = &render_buf;  
        lv_disp_drv_register(&disp_drv);
    }
};

首先,res_hres_v 是显示屏的水平与垂直像素,flush_cb 为用户所提供的 flush 回调函数逻辑。buf1buf2 为用户提供的缓冲区,默认只填写 buf1 时,使用单缓冲工作模式。

为了防止 lv_init 的反复调用,这里用一个局部变量控制。

这之后,按顺序来初始化显示实例、缓冲区,并且填写实例的各项信息,需要注意的是,由于 flush 本质上是一个静态的 C 回调函数,所以此处需要将当前对象的指针保存至 user_data 中,这之后再在 flush 中释放以提供 C++ 对象的访问。