文章

imx6ull移植LVGL试用 linux

开发板:imx6ull正点原子阿尔法板

交叉编译器:arm-linux-gnueabihf-gcc

LVGL版本:8.1

linux版本:5.4.0

ubuntu:22.04lts

下载LVGL库

下载lvgl8.1,下载lv_drivers8.1,下载lv_demos8.1,下载lv_port_linux8.2,全部下载好后在ubuntu中建立一个文件夹并解压这些文件:

在当前目录下创建一个lv_demo文件夹用于移植。

移植LVGL

  1. 将lvgl-release-v8.1、lv_drivers-release-v8.1、lv_demos-release-v8.1复制到lv_demo文件夹下,并依次重命名为lvgllv_driverslv_demos

  2. 分别将lvgllv_driverslv_demos 文件夹中的lv_conf_template.h、lv_drv_conf_template.h、lv_demo_conf_template.h文件复制到lv_demo文件夹下,并依次重命名为lv_conf.hlv_drv_conf.hlv_demo_conf.h

  3. 将lv_port_linux-release-v8.2文件夹下的main和Makefile文件复制到lv_demo 中。

上述步骤完成后的目录如下:

修改LVGL文件

lv_conf.h

将红框中的0改为1使能头文件:

根据自己的屏幕修改LV_COLOR_DEPTH

根据自己的内存情况修改LV_MEM_SIZE

根据自己CPU的性能修改LV_DISP_DEF_REFR_PERIODLV_INDEV_DEF_READ_PERIOD

修改定时器LV_TICK_CUSTOM如下:

修改FONT 字体:

使能LOG:

lv_drv_conf.h

将红框中的0改为1使能头文件:

使能USE_FBDEV用于显示,注意修改自己的FBDEV_PATH

使能USE_EVDEV用于触摸,注意修改自己的EVDEV_NAME

lv_demo_conf.h

使能:

使能demo:

main.c

直接上代码,这里没有用官方的例程,因为运行出错,所以简单写了一个hello world,注意代码中的屏幕大小修改:

#include "lvgl/lvgl.h"
#include "lv_demos/lv_demo.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include "stdio.h"

//注意修改为自己的屏幕大小
#define DISP_BUF_SIZE (600 * 1024)

int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 1024;//注意修改为自己的屏幕大小
    disp_drv.ver_res    = 600;//注意修改为自己的屏幕大小
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

    #if 0  //没有用鼠标注释掉
    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/
    #endif

    /*Create a Demo*/
    //lv_demo_widgets();

    lv_obj_t * btn = lv_btn_create(lv_scr_act());     // 创建一个按钮,放置在当前屏幕上
    lv_obj_align(btn, LV_ALIGN_CENTER, 0, 0);         // 将按钮居中对齐

    lv_obj_t * label = lv_label_create(btn);           // 在按钮上创建一个标签
    lv_label_set_text(label, "hello world!");              // 设置标签文本

    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}

Makefile

注释掉鼠标,添加demo路径include $(LVGL_DIR)/lv_demos/lv_demo.mk

搜索 -Wshift-negative-value并删掉参数:

编译运行结果

在lv_demo文件下,运行make 命名编译。

将编译出来的demo复制到开发板的目录下运行./demo 就可以看到效果了:

License:  CC BY 4.0