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

2022-11-16 12:02:58

前言

本次筆記主要記錄博主移植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

下載完如下圖:

然後直接make:

make

轉到lv_sim_vscode_sdl/build/bin執行demo韌體即可:

./demo

結果如圖:

如果上述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

進入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

新增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