【lvgl】01-lvgl移植之在linux上跑

时间:2022-11-16 09:08:26

前言

本次笔记主要记录博主移植lvgl到linux上的过程,方便调试。
两种编译方式:

  1. 移植官方推荐的lvgl,基于linux SDL2,makefile编译方式。
  2. 另外还会分别移植lvgl v8.0 和 lvgl v8.3两个版本。都是基于SDL2多媒体框架和CMAKE编译。
  • lvgl v8.3版本后面有时间再整理出来。

原文:李柱明博客

参考:

linux安装SDL2

sudo apt-get update && sudo apt-get install -y build-essential libsdl2-dev

官方推荐方式

参考:

拉取lvgl PC 模拟子模块vscode sdl:

git clone --recursive https://github.com/lvgl/lv_sim_vscode_sdl

下载完如下图:

【lvgl】01-lvgl移植之在linux上跑

然后直接make:

make

转到lv_sim_vscode_sdl/build/bin执行demo固件即可:

./demo

结果如图:

【lvgl】01-lvgl移植之在linux上跑

如果上述clone下面这三个库时较慢,可以手动下载库文件,再复制到lv_sim_vscode_sdl目录下对应的目录中:(选同一版本)

CMake方式移植lvgl v8.0

打算搭建自己学习、调试lvgl的SDK,用CMAKE编译,所以就自己手把手构建SDK和移植lvgl。

目录框架

目录构建如下:

  • app:专门存放学习、调试的UI app程序。
  • build:用于编译。
  • lib:第三方库。
  • src:主要起始文件,如main.c。
  • ui:lvgl源码、lvgl驱动等UI库文件。
  • CMakeLists.txt:cmake编译顶层文件。
    【lvgl】01-lvgl移植之在linux上跑

拉取lvgl

进入ui文件夹,clone两个库:

git clone -b release/v8.0 https://github.com/lvgl/lvgl.git
git clone -b release/v8.0 https://github.com/lvgl/lv_drivers.git

【lvgl】01-lvgl移植之在linux上跑

【lvgl】01-lvgl移植之在linux上跑

添加lv_conf.h和lv_drv_conf.h配置文件

lv_conf.h

添加lv_conf.h:在lvgl目录下

cp lv_conf_template.h lv_conf.h

使能该文件:改成1

#if 1 /*Set it to "1" to enable content*/

lv_drv_conf.h

  1. 添加lv_drv_conf.h:在lvgl目录下
cp lv_drv_conf_template.h lv_drv_conf.h
  1. 使能该文件:改成1
#if 1 /*Set it to "1" to enable content*/
  1. 开启SDL需要的宏:

    1. USE_MONITOR宏改成1。我们用monitor来模拟。
    2. USE_MOUSE改成1。用到鼠标。
    3. USE_MOUSEWHEEL改成1。鼠标滚轮也用到。
    4. USE_KEYBOARD改成1。用到键盘。

构建main.c

  1. 可以直接拷贝lv_sim_vscode_sdl/main/src/m*

    1. 我把该目录下的两个文件都cp过来了。
  2. main.c把头文件改成如下:

/*********************
 *      INCLUDES
 *********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdlib.h>
#include <unistd.h>
#define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" issue*/
#include <SDL2/SDL.h>
#include "lvgl.h"
#include "examples/lv_examples.h"
#include "monitor.h"
#include "mouse.h"
#include "keyboard.h"
#include "mousewheel.h"

  1. main.clv_demo_widgets();demo注释掉。
  2. mouse_cursor_icon.c的头文件改成:
#include "lvgl.h"

构建自己的CMAKE

lvgl 8.0版本我打算全盘使用自己构建的cmake。

lvgl 8.3版本我打算兼容原生库的构建的cmake。

顶层目录下的cmake:

cmake_minimum_required(VERSION 3.0.0)

set(target lzm_lvgl_test_bin)

project(${target} VERSION 0.1.0)

set(src_lvgl_test
        src/main.c
        src/mouse_cursor_icon.c
)

add_executable(${target} ${src_lvgl_test})


include_directories(/usr/include)
target_link_libraries(${target} PRIVATE lzm_lvgl_lib lzm_lvgl_drv_lib)
target_link_libraries(${target} PRIVATE SDL2)

add_subdirectory(ui)

./ui:

target_compile_definitions(${target} PUBLIC "-DLV_CONF_INCLUDE_SIMPLE")
target_compile_definitions(${target} PUBLIC "-DLV_LVGL_H_INCLUDE_SIMPLE")


add_subdirectory(lvgl)
add_subdirectory(lv_drivers)

./ui/lvgl:

set(target lzm_lvgl_lib)

add_library(${target} STATIC)

target_compile_definitions(${target} PUBLIC "-DLV_CONF_INCLUDE_SIMPLE")
target_compile_definitions(${target} PUBLIC "-DLV_LVGL_H_INCLUDE_SIMPLE")


target_include_directories(${target} PUBLIC ./)
target_include_directories(${target} PUBLIC ./src)
target_include_directories(${target} PUBLIC ./src/core)
target_include_directories(${target} PUBLIC ./src/draw)
target_include_directories(${target} PUBLIC ./src/extra)
target_include_directories(${target} PUBLIC ./src/font)
target_include_directories(${target} PUBLIC ./src/gpu)
target_include_directories(${target} PUBLIC ./src/hal)
target_include_directories(${target} PUBLIC ./src/misc)
target_include_directories(${target} PUBLIC ./src/widgets)

# app include
target_include_directories(${target} PUBLIC ./examples)

# app src
file(GLOB_RECURSE src_lvgl_app examples/*.c)

file(GLOB_RECURSE src_lvgl src/*.c)

target_sources(${target} PRIVATE
    ${src_lvgl}
    ${src_lvgl_app}
)

./ui/lv_drivers:

set(target lzm_lvgl_drv_lib)

add_library(${target} STATIC)

target_link_libraries(${target} PUBLIC lzm_lvgl_lib)

target_include_directories(${target} PUBLIC ./)
target_include_directories(${target} PUBLIC ./display)
target_include_directories(${target} PUBLIC ./gtkdrv)
target_include_directories(${target} PUBLIC ./indev)
target_include_directories(${target} PUBLIC ./wayland)
target_include_directories(${target} PUBLIC ./win32drv)

file(GLOB_RECURSE src_lvgl_drv ./*.c)

target_sources(${target} PRIVATE
    ${src_lvgl_drv}
)

编译

  1. 在./build路径下进行cmake ..:结果如下,Makefile创建成功:
jamie@lzm-vm-linux:~/work/study/lvgl/lzm_lvgl_v8.0/build$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jamie/work/study/lvgl/lzm_lvgl_v8.0/build
jamie@lzm-vm-linux:~/work/study/lvgl/lzm_lvgl_v8.0/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  ui
  1. 然后直接make,遇错就改:
jamie@lzm-vm-linux:~/work/study/lvgl/lzm_lvgl_v8.0/build$ make
[ 90%] Built target lzm_lvgl_lib
Consolidate compiler generated dependencies of target lzm_lvgl_drv_lib
[ 98%] Built target lzm_lvgl_drv_lib
Consolidate compiler generated dependencies of target lzm_lvgl_test_bin
[ 98%] Building C object CMakeFiles/lzm_lvgl_test_bin.dir/src/mouse_cursor_icon.c.o
[ 99%] Linking C executable lzm_lvgl_test_bin
[100%] Built target lzm_lvgl_test_bin
jamie@lzm-vm-linux:~/work/study/lvgl/lzm_lvgl_v8.0/build$ 
  1. 执行:
jamie@lzm-vm-linux:~/work/study/lvgl/lzm_lvgl_v8.0/build$ ./lzm_lvgl_test_bin