Skip to content

Commit ae7627a

Browse files
FPS logging etc
1 parent 5de7b30 commit ae7627a

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

  • c_mpos/mpong
  • internal_filesystem/apps/com.micropythonos.mpong/assets

c_mpos/mpong/mpong.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ int g_ball_y;
1515
int g_ball_vx;
1616
int g_ball_vy;
1717

18+
uint32_t g_fps_last_ms;
19+
uint32_t g_fps_frames;
20+
1821
#define BRICK_ROWS 4
1922
#define BRICK_COLS 8
2023
uint8_t g_bricks[BRICK_ROWS][BRICK_COLS];
@@ -37,6 +40,13 @@ static mp_obj_t readfile(mp_obj_t filename_obj) {
3740
}
3841
static MP_DEFINE_CONST_FUN_OBJ_1(readfile_obj, readfile);
3942

43+
static uint32_t ticks_ms(void) {
44+
mp_obj_t time_mod = mp_import_name(MP_QSTR_time, mp_const_none, MP_OBJ_NEW_SMALL_INT(0));
45+
mp_obj_t ticks_fun = mp_load_attr(time_mod, MP_QSTR_ticks_ms);
46+
mp_obj_t ticks_val = mp_call_function_n_kw(ticks_fun, 0, 0, NULL);
47+
return (uint32_t)mp_obj_get_int(ticks_val);
48+
}
49+
4050
static inline int clamp_int(int value, int min_value, int max_value) {
4151
if (value < min_value) {
4252
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
111121
reset_ball();
112122
reset_bricks();
113123

124+
g_fps_last_ms = ticks_ms();
125+
g_fps_frames = 0;
126+
114127
return mp_const_none;
115128
}
116129
static MP_DEFINE_CONST_FUN_OBJ_3(init_obj, init);
@@ -128,6 +141,16 @@ static mp_obj_t render(void) {
128141
// Clear to black.
129142
for (size_t i = 0; i < fill_pixels; i++) { g_framebuffer[i] = 0x0000; } // RGB565 black
130143

144+
g_fps_frames++;
145+
const uint32_t now_ms = ticks_ms();
146+
const uint32_t elapsed_ms = now_ms - g_fps_last_ms;
147+
if (elapsed_ms >= 1000) {
148+
const uint32_t fps = (g_fps_frames * 1000) / elapsed_ms;
149+
mp_printf(&mp_plat_print, "mpong fps: %lu\n", (unsigned long)fps);
150+
g_fps_last_ms = now_ms;
151+
g_fps_frames = 0;
152+
}
153+
131154
// Update ball position.
132155
g_ball_x += g_ball_vx;
133156
g_ball_y += g_ball_vy;

internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import lvgl as lv
22
from mpos import Activity, DisplayMetrics, InputManager
33

4-
indev_error_x = 160
5-
indev_error_y = 120
6-
7-
DARKPINK = lv.color_hex(0xEC048C)
8-
94
import sys
105
if sys.platform == "esp32":
116
import mpong_xtensawin as mpong
@@ -70,7 +65,7 @@ def onCreate(self):
7065
def onResume(self, screen):
7166
lv.log_register_print_cb(self.log_callback)
7267
mpong.init(self.buffer, self.hor_res, self.ver_res)
73-
self.refresh_timer = lv.timer_create(self.run_mpong, 1, None)
68+
self.refresh_timer = lv.timer_create(self.run_mpong, 15, None)
7469

7570
def onPause(self, screen):
7671
if self.refresh_timer:
@@ -91,18 +86,21 @@ def move_right_unfocus(self):
9186
self.unfocus()
9287
mpong.move_paddle(self.paddle_move_step)
9388

89+
# This only works with the PREV/pageup and NEXT/pagedown buttons,
90+
# because the focus_direction handling of the arrow keys uses a trick to move focus (focus_next)
91+
# which conflicts with the focus_next below...
9492
def unfocus(self):
9593
focusgroup = lv.group_get_default()
9694
if not focusgroup:
9795
print("WARNING: imageview.py could not get default focus group")
9896
return
9997
focused = focusgroup.get_focused()
10098
if focused:
101-
print(f"got focus button: {focused}")
99+
#print(f"got focus button: {focused}")
102100
label = focused.get_child(0)
103-
print(f"got label for button: {label.get_text()}")
101+
#print(f"got label for button: {label.get_text()}")
104102
#focused.remove_state(lv.STATE.FOCUSED) # this doesn't seem to work to remove focus
105-
print("checking which button is focused")
103+
#print("checking which button is focused")
106104
if focused == self.rightbutton:
107105
#print("next is focused")
108106
focusgroup.focus_prev()

0 commit comments

Comments
 (0)