LVGL 快速 demo
东东 Lv4

本文主要介绍 LVGL 的快速仿真示例

首先写一个通用的 main.c 调用 LVGL

src/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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <unistd.h>
// #define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include "lvgl/lvgl.h"
#include "lvgl/examples/lv_examples.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/sdl/sdl.h"

static void hal_init(void);

int main(int argc, char **argv)
{
(void)argc; /*Unused*/
(void)argv; /*Unused*/

/*Initialize LVGL*/
lv_init();

/*Initialize the HAL (display, input devices, tick) for LVGL*/
hal_init();

// lv_example_switch_1();
// lv_example_calendar_1();
// lv_example_btnmatrix_2();
// lv_example_checkbox_1();
// lv_example_colorwheel_1();
// lv_example_chart_6();
// lv_example_table_2();
// lv_example_scroll_2();
// lv_example_textarea_1();
// lv_example_msgbox_1();
// lv_example_dropdown_2();
// lv_example_btn_1();
// lv_example_scroll_1();
// lv_example_tabview_1();
// lv_example_tabview_1();
// lv_example_flex_3();
// lv_example_label_1();

lv_demo_widgets();

while(1) {
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
lv_timer_handler();
usleep(5 * 1000);
}

return 0;
}

static void hal_init(void)
{
/* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/
sdl_init();

/*Create a display buffer*/
static lv_disp_draw_buf_t disp_buf1;
static lv_color_t buf1_1[SDL_HOR_RES * 100];
lv_disp_draw_buf_init(&disp_buf1, buf1_1, NULL, SDL_HOR_RES * 100);

/*Create a display*/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.draw_buf = &disp_buf1;
disp_drv.flush_cb = sdl_display_flush;
disp_drv.hor_res = SDL_HOR_RES;
disp_drv.ver_res = SDL_VER_RES;

lv_disp_t * disp = lv_disp_drv_register(&disp_drv);

lv_theme_t * th = lv_theme_default_init(disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK, LV_FONT_DEFAULT);
lv_disp_set_theme(disp, th);

lv_group_t * g = lv_group_create();
lv_group_set_default(g);

/* Add the mouse as input device
* Use the 'mouse' driver which reads the PC's mouse*/
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 = sdl_mouse_read;
lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);

static lv_indev_drv_t indev_drv_2;
lv_indev_drv_init(&indev_drv_2); /*Basic initialization*/
indev_drv_2.type = LV_INDEV_TYPE_KEYPAD;
indev_drv_2.read_cb = sdl_keyboard_read;
lv_indev_t *kb_indev = lv_indev_drv_register(&indev_drv_2);
lv_indev_set_group(kb_indev, g);

static lv_indev_drv_t indev_drv_3;
lv_indev_drv_init(&indev_drv_3); /*Basic initialization*/
indev_drv_3.type = LV_INDEV_TYPE_ENCODER;
indev_drv_3.read_cb = sdl_mousewheel_read;
lv_indev_t * enc_indev = lv_indev_drv_register(&indev_drv_3);
lv_indev_set_group(enc_indev, g);

/*Set a cursor for the mouse*/
LV_IMG_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
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*/
}

然后写参照官方仿真例子写 CMakeLists.txt

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
cmake_minimum_required(VERSION 3.10)

project(lvgl)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

find_package(SDL2 REQUIRED SDL2)
include_directories(
${SDL2_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS}/../
${PROJECT_SOURCE_DIR}
"src"
"lvgl"
)

file(GLOB_RECURSE SOURCES
"src/*.c"
)

file(GLOB_RECURSE INCLUDES
)

add_subdirectory(lvgl/lvgl)
add_subdirectory(lvgl/lv_drivers)

set(CMAKE_C_FLAGS "-static" )

add_executable(main lvgl/mouse_cursor_icon.c ${SOURCES} ${INCLUDES})
target_link_libraries(main PRIVATE lvgl lvgl::examples lvgl::demos lvgl::drivers
SDL2::SDL2main SDL2::SDL2-static -lserialport -lpthread)

最后写一个 Makefile 作为自动构建工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# [MyProject]

default:
ifeq ("$(wildcard lvgl)", "")
@echo "lvgl sim does not exist."
@echo "Perform download..."
@git clone https://github.com/lvgl/lv_port_pc_eclipse.git lvgl
@cd lvgl && git submodule init && git submodule update
endif

make cmake_build

cmake_build:
mkdir -p build
cd build && cmake .. -G "MSYS Makefiles" && make -j8

run:
cd build && make -j8 && start main.exe

clean:
rm build -rf 2> /dev/null || true

可以将 lvgl 下的 lv_conf.hlv_drv_conf.h 复制到 src 下而不用对 lvgl 源码进行任何修改

使用方法

复制 quicksim 文件夹并重命名, 执行 make 即可.

lv_config.h 中设置一些常用配置

  • LV_USE_MEM_MONITOR 是否显示内存统计
  • LV_USE_PERF_MONITOR 是否显示帧率

源文件来自于

 评论