Skip to content

Commit a78dbcd

Browse files
committed
lvgl: Upgrade to lvgl v9 and adjust example app
LVGL 9 has a pretty significantly different API.
1 parent ff1517b commit a78dbcd

File tree

5 files changed

+952
-377
lines changed

5 files changed

+952
-377
lines changed

examples/lvgl/lvgl_driver.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
#include <libtock-sync/display/screen.h>
22
#include <libtock/sensors/touch.h>
3+
#include <libtock/services/alarm.h>
34
#include <libtock/tock.h>
45
#include <lvgl/lvgl.h>
56

67
#include "lvgl_driver.h"
78

8-
static lv_disp_draw_buf_t disp_buf;
9-
static lv_disp_drv_t disp_drv;
10-
static lv_disp_t* display_device;
9+
#define PIXEL_SIZE 2 // 16 bit for RGB565
1110

12-
static lv_indev_drv_t indev_drv;
13-
static lv_indev_t* touch_input_device;
11+
static lv_display_t* display_device;
12+
13+
static lv_indev_t* indev;
1414

1515
static int touch_status = LIBTOCK_TOUCH_STATUS_UNSTARTED;
1616
static uint16_t touch_x = 0, touch_y = 0;
1717

18-
static int buffer_size = 0;
19-
static uint8_t* buffer;
20-
2118
/* screen driver */
22-
static void screen_lvgl_driver(lv_disp_drv_t* disp, const lv_area_t* area,
23-
__attribute__ ((unused)) lv_color_t* color_p) {
19+
static void screen_lvgl_driver(lv_display_t* disp, const lv_area_t* area,
20+
uint8_t* buffer) {
21+
void* ud = lv_display_get_user_data(disp);
22+
uint32_t buffer_size = (uint32_t)ud;
2423
int32_t x, y;
2524
x = area->x1;
2625
y = area->y1;
2726
int w = area->x2 - area->x1 + 1;
2827
int h = area->y2 - area->y1 + 1;
2928
libtocksync_screen_set_frame(x, y, w, h);
30-
libtocksync_screen_write(buffer, buffer_size, (w * h) * sizeof(lv_color_t));
29+
libtocksync_screen_write(buffer, buffer_size, (w * h) * PIXEL_SIZE);
3130

32-
lv_disp_flush_ready(disp); /* Indicate you are ready with the flushing*/
31+
lv_display_flush_ready(disp); /* Indicate you are ready with the flushing*/
3332
}
3433

3534
static void touch_event(int status, uint16_t x, uint16_t y) {
@@ -38,7 +37,7 @@ static void touch_event(int status, uint16_t x, uint16_t y) {
3837
touch_y = y;
3938
}
4039

41-
static void my_input_read(__attribute__((unused)) lv_indev_drv_t* drv, lv_indev_data_t* data) {
40+
static void indev_cb(__attribute__ ((unused)) lv_indev_t* _indev, lv_indev_data_t* data) {
4241
if (touch_status == LIBTOCK_TOUCH_STATUS_PRESSED || touch_status == LIBTOCK_TOUCH_STATUS_MOVED) {
4342
data->point.x = touch_x;
4443
data->point.y = touch_y;
@@ -48,43 +47,44 @@ static void my_input_read(__attribute__((unused)) lv_indev_drv_t* drv, lv_indev_
4847
}
4948
}
5049

50+
static uint32_t tick_cb(void) {
51+
uint32_t ticks;
52+
libtock_alarm_command_read(&ticks);
5153

54+
uint32_t ms = libtock_alarm_ticks_to_ms(ticks);
55+
return ms;
56+
}
5257

5358
int lvgl_driver_init(int buffer_lines) {
5459
uint32_t width, height;
5560
int error = libtock_screen_get_resolution(&width, &height);
5661
if (error != RETURNCODE_SUCCESS) return error;
5762

58-
buffer_size = width * buffer_lines * sizeof(lv_color_t);
59-
error = libtock_screen_buffer_init(buffer_size, &buffer);
63+
uint32_t buffer_size = width * buffer_lines * PIXEL_SIZE;
64+
uint8_t* buffer = NULL;
65+
error = libtock_screen_buffer_init(buffer_size, &buffer);
6066
if (error != RETURNCODE_SUCCESS) return error;
6167

62-
/* share the frame buffer with littlevgl */
63-
lv_color_t* buf = (lv_color_t*) buffer;
64-
6568
/* initialize littlevgl */
6669
lv_init();
67-
lv_disp_drv_init(&disp_drv);
68-
disp_drv.flush_cb = screen_lvgl_driver;
69-
disp_drv.hor_res = width;
70-
disp_drv.ver_res = height;
71-
lv_disp_draw_buf_init(&disp_buf, buf, NULL, width * buffer_lines);
72-
disp_drv.draw_buf = &disp_buf;
73-
display_device = lv_disp_drv_register(&disp_drv);
70+
71+
display_device = lv_display_create(width, height);
72+
lv_display_set_color_format(display_device, LV_COLOR_FORMAT_RGB565);
73+
lv_display_set_flush_cb(display_device, screen_lvgl_driver);
74+
lv_display_set_antialiasing(display_device, false);
75+
76+
lv_display_set_buffers(display_device, buffer, NULL, buffer_size, LV_DISPLAY_RENDER_MODE_PARTIAL);
77+
lv_display_set_user_data(display_device, (void*)buffer_size);
78+
79+
lv_tick_set_cb(tick_cb);
7480

7581
int touches;
7682
if (libtock_touch_get_number_of_touches(&touches) == RETURNCODE_SUCCESS && touches >= 1) {
7783
libtock_touch_enable_single_touch(touch_event);
78-
lv_indev_drv_init(&indev_drv);
79-
indev_drv.type = LV_INDEV_TYPE_POINTER;
80-
indev_drv.read_cb = my_input_read;
81-
touch_input_device = lv_indev_drv_register(&indev_drv);
84+
indev = lv_indev_create();
85+
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
86+
lv_indev_set_read_cb(indev, indev_cb);
8287
}
8388

8489
return RETURNCODE_SUCCESS;
8590
}
86-
87-
void lvgl_driver_event(int millis) {
88-
lv_tick_inc(millis);
89-
lv_task_handler();
90-
}

examples/lvgl/main.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,42 @@
77

88
#include "lvgl_driver.h"
99

10+
static uint32_t seconds = 0;
11+
12+
static void tick_cb(lv_timer_t* timer) {
13+
seconds++;
14+
lv_obj_t* label1 = lv_timer_get_user_data(timer);
15+
lv_label_set_text_fmt(label1, "%lu", seconds);
16+
}
17+
1018
static void event_handler(lv_event_t* e) {
11-
lv_event_code_t code = lv_event_get_code(e);
12-
unsigned int* seconds = (unsigned int*)lv_event_get_user_data(e);
19+
lv_event_code_t code = lv_event_get_code(e);
20+
uint32_t* secs = (uint32_t*)lv_event_get_user_data(e);
1321

1422
if (code == LV_EVENT_CLICKED) {
1523
LV_LOG_USER("Clicked");
16-
*seconds = 0;
24+
*secs = 0;
1725
} else if (code == LV_EVENT_VALUE_CHANGED) {
1826
LV_LOG_USER("Toggled");
1927
}
2028
}
2129

2230
int main(void) {
23-
unsigned int seconds = 0;
24-
2531
libtocksync_screen_set_brightness(100);
2632
int status = lvgl_driver_init(5);
2733
if (status == RETURNCODE_SUCCESS) {
2834
/* LittlevGL's Hello World tutorial example */
2935

30-
lv_obj_t* scr = lv_disp_get_scr_act(NULL); /*Get the current screen*/
36+
lv_obj_t* scr = lv_screen_active(); /*Get the current screen*/
37+
38+
lv_obj_t* obj1 = lv_obj_create(lv_screen_active());
39+
lv_obj_set_width(obj1, lv_pct(100));
40+
lv_obj_set_height(obj1, lv_pct(100));
3141

32-
/*Create a Label on the currently active screen*/
3342
lv_obj_t* label1 = lv_label_create(scr);
3443

35-
/*Modify the Label's text*/
36-
lv_label_set_text(label1, "Hello world!");
44+
lv_label_set_text(label1, "0");
3745

38-
/* Align the Label to the center
39-
* NULL means align on parent (which is the screen now)
40-
* 0, 0 at the end means an x, y offset after alignment*/
4146
lv_obj_align(label1, LV_ALIGN_CENTER, 0, 0);
4247

4348
lv_obj_t* btn1 = lv_btn_create(scr);
@@ -48,17 +53,13 @@ int main(void) {
4853
lv_label_set_text(label, "Reset");
4954
lv_obj_center(label);
5055

51-
/* main loop */
52-
while (1) {
53-
seconds++;
54-
if (seconds % 200 == 0) {
55-
char buffer[100];
56-
snprintf(buffer, 99, "Seconds: %d", seconds / 200);
57-
lv_label_set_text(label1, buffer);
58-
}
59-
libtocksync_alarm_delay_ms(5);
60-
lvgl_driver_event(5);
56+
lv_timer_create(tick_cb, 1000, (void*)label1);
57+
58+
for ( ;;) {
59+
uint32_t time_till_next = lv_timer_handler();
60+
libtocksync_alarm_delay_ms(time_till_next);
6161
}
62+
6263
} else {
6364
printf("lvgl init error: %s\n", tock_strrcode(status));
6465
}

lvgl/Makefile

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,12 @@ TOCK_USERLAND_BASE_DIR ?= ..
22
LIBNAME := lvgl
33
$(LIBNAME)_DIR := $(TOCK_USERLAND_BASE_DIR)/$(LIBNAME)
44

5-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/core/*.c)
6-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/draw/*.c)
7-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/*.c)
8-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/layouts/flex/*.c)
9-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/layouts/grid/*.c)
10-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/themes/basic/*.c)
11-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/themes/default/*.c)
12-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/themes/mono/*.c)
13-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/mono/*.c)
14-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/animimg/*.c)
15-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/calendar/*.c)
16-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/chart/*.c)
17-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/colorwheel/*.c)
18-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/imgbtn/*.c)
19-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/keyboard/*.c)
20-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/led/*.c)
21-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/list/*.c)
22-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/meter/*.c)
23-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/msgbox/*.c)
24-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/span/*.c)
25-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/spinbox/*.c)
26-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/spinner/*.c)
27-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/tabview/*.c)
28-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/tileview/*.c)
29-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/extra/widgets/win/*.c)
30-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/font/*.c)
31-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/gpu/*.c)
32-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/hal/*.c)
33-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/misc/*.c)
34-
$(LIBNAME)_SRCS += $(wildcard $($(LIBNAME)_DIR)/lvgl/src/widgets/*.c)
5+
$(LIBNAME)_SRCS += $(shell find $($(LIBNAME)_DIR)/lvgl/src -type f -name '*.S')
6+
$(LIBNAME)_SRCS += $(shell find $($(LIBNAME)_DIR)/lvgl/src -type f -name '*.c')
7+
$(LIBNAME)_SRCS += $(shell find $($(LIBNAME)_DIR)/lvgl/src -type f -name '*.cpp')
358

369
# Avoid failing in CI due to warnings in the library.
37-
override CPPFLAGS_$(LIBNAME) += -Wno-error
10+
override CPPFLAGS_$(LIBNAME) += -Wno-error -I$(LIBNAME)_DIR/lvgl
3811

3912
include $(TOCK_USERLAND_BASE_DIR)/TockLibrary.mk
4013

0 commit comments

Comments
 (0)