|
| 1 | +import time |
| 2 | +import _thread |
| 3 | + |
| 4 | +from mpos.apps import Activity |
| 5 | +import mpos.ui |
| 6 | + |
| 7 | +indev_error_x = 160 |
| 8 | +indev_error_y = 120 |
| 9 | + |
| 10 | +DARKPINK = lv.color_hex(0xEC048C) |
| 11 | + |
| 12 | +class Benchmark(Activity): |
| 13 | + |
| 14 | + hor_res = 0 |
| 15 | + ver_res = 0 |
| 16 | + layer = None |
| 17 | + fps_buffer = [0] # Buffer to store FPS |
| 18 | + bird_x = 100 |
| 19 | + bird_y = 0 |
| 20 | + image_w = 296 |
| 21 | + image_h = 240 |
| 22 | + image_target_w = 160 |
| 23 | + image_target_h = 120 |
| 24 | + image_x = image_w |
| 25 | + image_y = 0 |
| 26 | + |
| 27 | + #lvgl_w = 160 |
| 28 | + #lvgl_h = 120 |
| 29 | + |
| 30 | + # Widgets: |
| 31 | + canvas = None |
| 32 | + image = None |
| 33 | + |
| 34 | + def onCreate(self): |
| 35 | + screen = lv.obj() |
| 36 | + # Make the screen focusable so it can be scrolled with the arrow keys |
| 37 | + focusgroup = lv.group_get_default() |
| 38 | + if focusgroup: |
| 39 | + focusgroup.add_obj(screen) |
| 40 | + screen.add_event_cb(self.key_cb, lv.EVENT.KEY, None) |
| 41 | + self.image = lv.image(screen) |
| 42 | + #self.load_image("data/images/screenshots/snapshot_640x480_RGB565.raw") |
| 43 | + self.load_image("data/images/screenshots/snapshot_296x240_RGB565.raw") |
| 44 | + #self.image.set_size(self.lvgl_w, self.lvgl_h) |
| 45 | + #self.gif.set_size(lvgl_w, lvgl_h) doesn't seem to do anything. get_style_transform_scale_x/y works but then it needs get_style_translate_x/y |
| 46 | + #self.image.set_scale(max(scale_factor_w,scale_factor_h)) # fills the entire screen but cuts off borders |
| 47 | + scale_factor_w = round(self.image_target_w * 256 / self.image_w) |
| 48 | + self.image.set_scale(scale_factor_w) |
| 49 | + #self.image.set_scale(128) |
| 50 | + #self.image.set_size(640, 480) |
| 51 | + self.spinner = lv.spinner(screen) |
| 52 | + self.spinner.set_size(16, 16) |
| 53 | + self.setContentView(screen) |
| 54 | + |
| 55 | + def onResume(self, screen): |
| 56 | + super().onResume(screen) |
| 57 | + lv.log_register_print_cb(self.log_callback) |
| 58 | + try: |
| 59 | + _thread.stack_size(mpos.apps.good_stack_size()) |
| 60 | + _thread.start_new_thread(self.game, ()) |
| 61 | + except Exception as e: |
| 62 | + print("Could not start thread: ", e) |
| 63 | + |
| 64 | + def onStop(self, screen): |
| 65 | + super().onStop(screen) |
| 66 | + lv.log_register_print_cb(None) |
| 67 | + |
| 68 | + |
| 69 | + def extract_dimensions_and_format(self, filename): |
| 70 | + # Split the filename by '_' |
| 71 | + parts = filename.split('_') |
| 72 | + # Get the color format (last part before '.raw') |
| 73 | + color_format = parts[-1].split('.')[0] # e.g., "RGB565" |
| 74 | + # Get the resolution (second-to-last part) |
| 75 | + resolution = parts[-2] # e.g., "240x240" |
| 76 | + # Split resolution by 'x' to get width and height |
| 77 | + width, height = map(int, resolution.split('x')) |
| 78 | + return width, height, color_format.upper() |
| 79 | + |
| 80 | + def load_image(self, name): |
| 81 | + if not name.lower().endswith(".raw"): |
| 82 | + self.image.remove_flag(lv.obj.FLAG.HIDDEN) |
| 83 | + self.image.set_src(f"M:{name}") |
| 84 | + else: |
| 85 | + f = open(name, 'rb') |
| 86 | + image_data = f.read() |
| 87 | + print(f"loaded {len(image_data)} bytes from .raw file") |
| 88 | + f.close() |
| 89 | + try: |
| 90 | + width, height, color_format = self.extract_dimensions_and_format(name) |
| 91 | + except ValueError as e: |
| 92 | + print(f"Warning: could not extract dimensions and format from raw image: {e}") |
| 93 | + return |
| 94 | + print(f"Raw image has width: {width}, Height: {height}, Color Format: {color_format}") |
| 95 | + stride = width * 2 |
| 96 | + cf = lv.COLOR_FORMAT.RGB565 |
| 97 | + if color_format != "RGB565": |
| 98 | + print(f"WARNING: unknown color format {color_format}, assuming RGB565...") |
| 99 | + self.current_image_dsc = lv.image_dsc_t({ |
| 100 | + "header": { |
| 101 | + "magic": lv.IMAGE_HEADER_MAGIC, |
| 102 | + "w": width, |
| 103 | + "h": height, |
| 104 | + "stride": stride, |
| 105 | + "cf": cf |
| 106 | + }, |
| 107 | + 'data_size': len(image_data), |
| 108 | + 'data': image_data |
| 109 | + }) |
| 110 | + self.image.set_src(self.current_image_dsc) |
| 111 | + |
| 112 | + def touch_cb(self, event): |
| 113 | + event_code=event.get_code() |
| 114 | + #print(f"lv_event_t: code={event_code}") |
| 115 | + if event_code == lv.EVENT.PRESSING: |
| 116 | + self.runall() |
| 117 | + |
| 118 | + # Custom log callback to capture FPS |
| 119 | + def log_callback(self,level, log_str): |
| 120 | + # Convert log_str to string if it's a bytes object |
| 121 | + log_str = log_str.decode() if isinstance(log_str, bytes) else log_str |
| 122 | + # Optional: Print for debugging |
| 123 | + #print(f"Level: {level}, Log: {log_str}") |
| 124 | + # Log message format: "sysmon: 25 FPS (refr_cnt: 8 | redraw_cnt: 1), ..." |
| 125 | + if "sysmon:" in log_str and "FPS" in log_str: |
| 126 | + try: |
| 127 | + # Extract FPS value (e.g., "25" from "sysmon: 25 FPS ...") |
| 128 | + fps_part = log_str.split("FPS")[0].split("sysmon:")[1].strip() |
| 129 | + fps = int(fps_part) |
| 130 | + print("Current FPS:", fps) |
| 131 | + self.fps_buffer[0] = fps |
| 132 | + except (IndexError, ValueError): |
| 133 | + pass |
| 134 | + |
| 135 | + def key_cb(self, event): |
| 136 | + key = event.get_key() |
| 137 | + #print(f"got key {key}") |
| 138 | + |
| 139 | + if key == lv.KEY.UP: |
| 140 | + self.image_target_w += 20 |
| 141 | + scale_factor_w = round(self.image_target_w * 256 / self.image_w) |
| 142 | + self.image.set_scale(scale_factor_w) |
| 143 | + #self.bird_y -= 10 |
| 144 | + elif key == lv.KEY.DOWN: |
| 145 | + self.image_target_w -= 20 |
| 146 | + scale_factor_w = round(self.image_target_w * 256 / self.image_w) |
| 147 | + self.image.set_scale(scale_factor_w) |
| 148 | + #self.bird_y += 10 |
| 149 | + elif key == lv.KEY.LEFT: |
| 150 | + self.bird_x -= 10 |
| 151 | + elif key == lv.KEY.RIGHT: |
| 152 | + self.bird_x += 10 |
| 153 | + elif key == lv.KEY.ENTER: |
| 154 | + self.bird_y -= 25 |
| 155 | + |
| 156 | + def game(self): |
| 157 | + # print("Waiting a bit before starting...") ; time.sleep(1) # wait for top bar to go away |
| 158 | + while self.has_foreground(): |
| 159 | + self.update_ui_threadsafe_if_foreground(self.spinner.set_pos, self.bird_x, self.bird_y) |
| 160 | + self.update_ui_threadsafe_if_foreground(self.image.set_x, self.image_x) |
| 161 | + time.sleep_ms(10) |
| 162 | + self.image_x -= 1 |
| 163 | + if self.image_x < (-self.image_target_w*2): |
| 164 | + self.image_x = self.image_w |
| 165 | + self.bird_y += 1 |
| 166 | + if self.bird_y > self.image_h: |
| 167 | + self.bird_y = 0 |
| 168 | + |
0 commit comments