From 31fbcb41a5bdc6688432fd6692a24cd98103ecac Mon Sep 17 00:00:00 2001 From: Ryzee119 Date: Sun, 29 Oct 2023 13:41:05 +1030 Subject: [PATCH] debug: Create debug info box --- Makefile.nxdk | 1 + src/dash_debug.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ src/dash_debug.h | 20 +++++++++++ src/lithiumx.h | 1 + 4 files changed, 114 insertions(+) create mode 100644 src/dash_debug.c create mode 100644 src/dash_debug.h diff --git a/Makefile.nxdk b/Makefile.nxdk index db443f3..c2516dc 100644 --- a/Makefile.nxdk +++ b/Makefile.nxdk @@ -20,6 +20,7 @@ SRCS += \ $(CURDIR)/src/dash_synop.c \ $(CURDIR)/src/dash_browser.c \ $(CURDIR)/src/dash_launcher.c \ + $(CURDIR)/src/dash_debug.c \ $(CURDIR)/src/main.c \ $(CURDIR)/src/lvgl_widgets/confirmbox.c \ $(CURDIR)/src/lvgl_widgets/generic_container.c \ diff --git a/src/dash_debug.c b/src/dash_debug.c new file mode 100644 index 0000000..373d250 --- /dev/null +++ b/src/dash_debug.c @@ -0,0 +1,92 @@ +#include + +static bool debug_info_visible = false; +static SDL_Thread *debug_info_thread; + +#ifdef NXDK +static void get_ram_usage(uint32_t *mem_size, uint32_t *mem_used) +{ + MM_STATISTICS MemoryStatistics; + MemoryStatistics.Length = sizeof(MM_STATISTICS); + MmQueryStatistics(&MemoryStatistics); + *mem_size = (MemoryStatistics.TotalPhysicalPages * PAGE_SIZE) / 1024U / 1024U; + *mem_used = *mem_size - ((MemoryStatistics.AvailablePages * PAGE_SIZE) / 1024U / 1024U); + return; +} +#else +static void get_ram_usage(uint32_t *mem_size, uint32_t *mem_used) +{ + *mem_size = 0; + *mem_used = 0; +} +#endif + +static void frame_count_incrememt(lv_timer_t *t) +{ + uint32_t *frame_count = t->user_data; + *frame_count = *frame_count + 1; +} + +static int debug_info_thread_f(void *param) +{ + lvgl_getlock(); + lv_obj_t *mem_usage_label = lv_label_create(lv_layer_sys()); + lv_obj_set_style_bg_opa(mem_usage_label, LV_OPA_50, 0); + lv_obj_set_align(mem_usage_label, LV_ALIGN_BOTTOM_RIGHT); + lv_obj_set_style_text_font(mem_usage_label, &lv_font_montserrat_16, LV_PART_MAIN); + lvgl_removelock(); + + uint32_t frame_count = 0, used, capacity, fps = 0, start = lv_tick_get(), ram_used, ram_total; + lv_timer_t *frame_counter = lv_timer_create(frame_count_incrememt, 0, &frame_count); + + while (1) + { + if (debug_info_visible == false) + { + break; + } + if (frame_count > 100) + { + + fps = 1000 * frame_count / lv_tick_elaps(start); + if (1000 / fps < LV_DISP_DEF_REFR_PERIOD) + fps = 1000 / LV_DISP_DEF_REFR_PERIOD; + start = lv_tick_get(); + frame_count = 0; + } + lx_mem_usage(&used, &capacity); + get_ram_usage(&ram_total, &ram_used); + lvgl_getlock(); + lv_label_set_text_fmt(mem_usage_label, "GUI:%d/%dkB\n" + "CPU: %d%%\n" + "RAM:%d/%d MB\n" + "FPS: %d", + used / 1024, capacity / 1024, 100 - lv_timer_get_idle(), ram_used, ram_total, fps); + + lv_obj_update_layout(mem_usage_label); + lv_obj_set_size(mem_usage_label, LV_SIZE_CONTENT, LV_SIZE_CONTENT); + lvgl_removelock(); + for (int i = 0; i < 50; i++) + { + SDL_Delay(10); + if (debug_info_visible == false) + break; + } + } + + lv_timer_del(frame_counter); + lv_obj_del(mem_usage_label); + return 0; +} + +void dash_debug_open() +{ + debug_info_visible = true; + debug_info_thread = SDL_CreateThread(debug_info_thread_f, "update_mem_usage", NULL); +} + +void dash_debug_close() +{ + debug_info_visible = false; + SDL_WaitThread(debug_info_thread, NULL); +} diff --git a/src/dash_debug.h b/src/dash_debug.h new file mode 100644 index 0000000..435484f --- /dev/null +++ b/src/dash_debug.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT +// SPDX-FileCopyrightText: 2022 Ryzee119 + +#ifndef _DASH_DEBUG_H +#define _DASH_DEBUG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "lithiumx.h" + +void dash_debug_open(); +void dash_debug_close(); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/lithiumx.h b/src/lithiumx.h index a12e75b..d14db2b 100644 --- a/src/lithiumx.h +++ b/src/lithiumx.h @@ -31,6 +31,7 @@ int strcasecmp(const char *s1, const char *s2); #include "dash_synop.h" #include "dash_browser.h" #include "dash_launcher.h" +#include "dash_debug.h" #include "lvgl_drivers/lv_port_disp.h" #include "lvgl_drivers/lv_port_indev.h"