From b4e85b87bed534de615afb39b8b8336f813fcc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ko=C5=82ucki?= Date: Mon, 25 May 2026 23:02:38 +0200 Subject: [PATCH] UI test --- source/main.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/source/main.c b/source/main.c index 682414e..fe572b7 100644 --- a/source/main.c +++ b/source/main.c @@ -10,6 +10,7 @@ static lv_obj_t * cursor; static lv_obj_t * label; static lv_obj_t * label2; +static lv_obj_t * fps_label; long unsigned int sys_tick(void) @@ -56,17 +57,68 @@ static void cursor_timer_cb(lv_timer_t * t) } } -int main(int argc, char* argv[]) +static void switch_event_cb(lv_event_t * e) { - sys_init(); - lv_init(); - lv_ctr_disp_init(); - lv_ctr_indev_init(); - lv_tick_set_cb(sys_tick); + lv_obj_t * sw = lv_event_get_target(e); - label = lv_label_create(lv_screen_active()); + if (lv_obj_has_state(sw, LV_STATE_CHECKED)) + lv_label_set_text_fmt(label, "ON"); + else + lv_label_set_text_fmt(label, "OFF"); +} + +static void fps_tick(void) +{ + static u32 frames = 0; + static u64 last = 0; + + u64 now = osGetTime(); + + frames++; + + if (now - last >= 1000) { + + lv_label_set_text_fmt(fps_label, + "FPS: %lu", + frames); + + frames = 0; + last = now; + } +} + +static void ui_prepare(void) +{ + /* Container START */ + lv_obj_t * cont = lv_obj_create(lv_screen_active()); + + /* Container layout */ + lv_obj_set_size(cont, 220, 120); + lv_obj_center(cont); + + lv_obj_set_layout(cont, LV_LAYOUT_FLEX); + lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN); + lv_obj_set_flex_align(cont, + LV_FLEX_ALIGN_CENTER, + LV_FLEX_ALIGN_CENTER, + LV_FLEX_ALIGN_CENTER); + + /* Optional cleaner look */ + lv_obj_set_style_pad_all(cont, 16, 0); + lv_obj_set_style_pad_row(cont, 12, 0); + + /* Label */ + label = lv_label_create(cont); lv_label_set_text(label, "Hello world"); - lv_obj_center(label); + + /* Switch */ + lv_obj_t * sw = lv_switch_create(cont); + lv_obj_center(sw); + lv_obj_add_event_cb(sw, + switch_event_cb, + LV_EVENT_VALUE_CHANGED, + NULL); + /* Container END */ /* Cursor */ cursor = lv_obj_create(lv_screen_active()); @@ -79,15 +131,36 @@ int main(int argc, char* argv[]) label2 = lv_label_create(lv_screen_active()); lv_obj_align(label2, LV_ALIGN_TOP_LEFT, 4, 4); + /* FPS label */ + fps_label = lv_label_create(lv_screen_active()); + lv_obj_align(fps_label, + LV_ALIGN_BOTTOM_LEFT, + 4, + -4); + /* Main loop UI update timer */ lv_timer_create(cursor_timer_cb, 16, NULL); lv_obj_invalidate(lv_screen_active()); +} + +int main(int argc, char* argv[]) +{ + sys_init(); + lv_init(); + lv_ctr_disp_init(); + lv_ctr_indev_init(); + lv_tick_set_cb(sys_tick); + + ui_prepare(); // Main loop while (aptMainLoop()) { + u64 start = osGetTime(); lv_timer_handler(); + u64 end = osGetTime(); + lv_label_set_text_fmt(fps_label, "render: %llums", end - start); hidScanInput();