From 0ae4d0621237145793d81a21e114081318e580cf Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 20 Aug 2022 15:40:40 +0200 Subject: [PATCH] tests/pkg_lvgl_touch: randomize button position on click --- tests/pkg_lvgl_touch/Makefile | 2 ++ tests/pkg_lvgl_touch/app.config.test | 3 +++ tests/pkg_lvgl_touch/main.c | 13 ++++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/pkg_lvgl_touch/Makefile b/tests/pkg_lvgl_touch/Makefile index f2a464affd..e4e7d23f60 100644 --- a/tests/pkg_lvgl_touch/Makefile +++ b/tests/pkg_lvgl_touch/Makefile @@ -15,6 +15,8 @@ USEMODULE += lvgl_extra_theme_default # uncomment the following to enable growing button (needs more RAM) # USEMODULE += lvgl_extra_theme_default_grow +USEMODULE += random + # SDL requires more stack ifeq (native,$(BOARD)) CFLAGS += -DTHREAD_STACKSIZE_MAIN=64*1024 diff --git a/tests/pkg_lvgl_touch/app.config.test b/tests/pkg_lvgl_touch/app.config.test index 0bcef0b3a7..99bcfce639 100644 --- a/tests/pkg_lvgl_touch/app.config.test +++ b/tests/pkg_lvgl_touch/app.config.test @@ -9,6 +9,9 @@ CONFIG_MODULE_LVGL_EXTRA_THEME_DEFAULT=y # Add touch capabilities CONFIG_MODULE_LVGL_CONTRIB_TOUCH=y +# enable random positioning +CONFIG_MODULE_RANDOM=y + # Uncomment the following Kconfig symbols to configure LVGL using Kconfig. This # requires the LVGL package to be already downloaded (e.g. built once already) # LVGL specific configuration diff --git a/tests/pkg_lvgl_touch/main.c b/tests/pkg_lvgl_touch/main.c index 9ba3982e0e..ed532c649d 100644 --- a/tests/pkg_lvgl_touch/main.c +++ b/tests/pkg_lvgl_touch/main.c @@ -24,13 +24,22 @@ #include "lvgl_riot.h" #include "disp_dev.h" +#include "random.h" +static lv_obj_t *btn; +static const lv_coord_t x_size = 100; +static const lv_coord_t y_size = 50; static void btn_event_cb(lv_event_t *event) { lv_event_code_t code = lv_event_get_code(event); if (code == LV_EVENT_CLICKED) { puts("Button clicked!"); + + lv_coord_t x_pos = random_uint32_range(0, lv_obj_get_width(lv_scr_act()) - x_size); + lv_coord_t y_pos = random_uint32_range(0, lv_obj_get_height(lv_scr_act()) - y_size); + + lv_obj_set_pos(btn, x_pos, y_pos); } } @@ -40,11 +49,9 @@ int main(void) disp_dev_backlight_on(); /* Add a button to the current screen */ - lv_obj_t *btn = lv_btn_create(lv_scr_act()); + btn = lv_btn_create(lv_scr_act()); /* Set the button position and size */ - lv_coord_t x_size = 100; - lv_coord_t y_size = 50; lv_coord_t x_pos = (lv_obj_get_width(lv_scr_act()) - x_size) / 2; lv_coord_t y_pos = (lv_obj_get_height(lv_scr_act()) - y_size) / 2; lv_obj_set_pos(btn, x_pos, y_pos);