From ae7627a0ab0d9a64dde355b95a2b1456c30bacbb Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 9 Mar 2026 21:25:49 +0100 Subject: [PATCH] FPS logging etc --- c_mpos/mpong/mpong.c | 23 +++++++++++++++++++ .../com.micropythonos.mpong/assets/mpong.py | 16 ++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/c_mpos/mpong/mpong.c b/c_mpos/mpong/mpong.c index 0fe96996..600feb6f 100644 --- a/c_mpos/mpong/mpong.c +++ b/c_mpos/mpong/mpong.c @@ -15,6 +15,9 @@ int g_ball_y; int g_ball_vx; int g_ball_vy; +uint32_t g_fps_last_ms; +uint32_t g_fps_frames; + #define BRICK_ROWS 4 #define BRICK_COLS 8 uint8_t g_bricks[BRICK_ROWS][BRICK_COLS]; @@ -37,6 +40,13 @@ static mp_obj_t readfile(mp_obj_t filename_obj) { } static MP_DEFINE_CONST_FUN_OBJ_1(readfile_obj, readfile); +static uint32_t ticks_ms(void) { + mp_obj_t time_mod = mp_import_name(MP_QSTR_time, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)); + mp_obj_t ticks_fun = mp_load_attr(time_mod, MP_QSTR_ticks_ms); + mp_obj_t ticks_val = mp_call_function_n_kw(ticks_fun, 0, 0, NULL); + return (uint32_t)mp_obj_get_int(ticks_val); +} + static inline int clamp_int(int value, int min_value, int max_value) { if (value < min_value) { return min_value; @@ -111,6 +121,9 @@ static mp_obj_t init(mp_obj_t framebuffer_obj, mp_obj_t width_obj, mp_obj_t heig reset_ball(); reset_bricks(); + g_fps_last_ms = ticks_ms(); + g_fps_frames = 0; + return mp_const_none; } static MP_DEFINE_CONST_FUN_OBJ_3(init_obj, init); @@ -128,6 +141,16 @@ static mp_obj_t render(void) { // Clear to black. for (size_t i = 0; i < fill_pixels; i++) { g_framebuffer[i] = 0x0000; } // RGB565 black + g_fps_frames++; + const uint32_t now_ms = ticks_ms(); + const uint32_t elapsed_ms = now_ms - g_fps_last_ms; + if (elapsed_ms >= 1000) { + const uint32_t fps = (g_fps_frames * 1000) / elapsed_ms; + mp_printf(&mp_plat_print, "mpong fps: %lu\n", (unsigned long)fps); + g_fps_last_ms = now_ms; + g_fps_frames = 0; + } + // Update ball position. g_ball_x += g_ball_vx; g_ball_y += g_ball_vy; diff --git a/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py b/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py index d30221c1..8e6a3b7c 100644 --- a/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py +++ b/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py @@ -1,11 +1,6 @@ import lvgl as lv from mpos import Activity, DisplayMetrics, InputManager -indev_error_x = 160 -indev_error_y = 120 - -DARKPINK = lv.color_hex(0xEC048C) - import sys if sys.platform == "esp32": import mpong_xtensawin as mpong @@ -70,7 +65,7 @@ class MPong(Activity): def onResume(self, screen): lv.log_register_print_cb(self.log_callback) mpong.init(self.buffer, self.hor_res, self.ver_res) - self.refresh_timer = lv.timer_create(self.run_mpong, 1, None) + self.refresh_timer = lv.timer_create(self.run_mpong, 15, None) def onPause(self, screen): if self.refresh_timer: @@ -91,6 +86,9 @@ class MPong(Activity): self.unfocus() mpong.move_paddle(self.paddle_move_step) + # This only works with the PREV/pageup and NEXT/pagedown buttons, + # because the focus_direction handling of the arrow keys uses a trick to move focus (focus_next) + # which conflicts with the focus_next below... def unfocus(self): focusgroup = lv.group_get_default() if not focusgroup: @@ -98,11 +96,11 @@ class MPong(Activity): return focused = focusgroup.get_focused() if focused: - print(f"got focus button: {focused}") + #print(f"got focus button: {focused}") label = focused.get_child(0) - print(f"got label for button: {label.get_text()}") + #print(f"got label for button: {label.get_text()}") #focused.remove_state(lv.STATE.FOCUSED) # this doesn't seem to work to remove focus - print("checking which button is focused") + #print("checking which button is focused") if focused == self.rightbutton: #print("next is focused") focusgroup.focus_prev()