diff --git a/c_mpos/mpong/Makefile b/c_mpos/mpong/Makefile new file mode 100644 index 00000000..e69de29b diff --git a/c_mpos/mpong/Makefile_amd64 b/c_mpos/mpong/Makefile_amd64 new file mode 100644 index 00000000..89e8fbc0 --- /dev/null +++ b/c_mpos/mpong/Makefile_amd64 @@ -0,0 +1,13 @@ +MPY_DIR = ../../lvgl_micropython/lib/micropython/ + +# Name of module +MOD = mpong + +# Source files (.c or .py) +SRC = mpong.c + +# Architecture to build for (x86, x64, armv6m, armv7m, xtensa, xtensawin, rv32imc, rv64imc) +ARCH = x64 + +# Include to get the rules for compiling and linking the module +include $(MPY_DIR)/py/dynruntime.mk diff --git a/c_mpos/mpong/Makefile_esp32 b/c_mpos/mpong/Makefile_esp32 new file mode 100644 index 00000000..557a76b3 --- /dev/null +++ b/c_mpos/mpong/Makefile_esp32 @@ -0,0 +1,13 @@ +MPY_DIR = ../../lvgl_micropython/lib/micropython/ + +# Name of module +MOD = mpong + +# Source files (.c or .py) +SRC = mpong.c + +# Architecture to build for (x86, x64, armv6m, armv7m, xtensa, xtensawin, rv32imc, rv64imc) +ARCH = xtensawin + +# Include to get the rules for compiling and linking the module +include $(MPY_DIR)/py/dynruntime.mk diff --git a/c_mpos/mpong/build_amd64.sh b/c_mpos/mpong/build_amd64.sh new file mode 100755 index 00000000..26d77aaa --- /dev/null +++ b/c_mpos/mpong/build_amd64.sh @@ -0,0 +1,11 @@ +mydir=$(readlink -f "$0") +mydir=$(dirname "$mydir") + +cd "$mydir" +rm -rf build + +make -f Makefile_amd64 = fill_pixels) { + break; + } + pixels[idx] = 0x0000; // RGB565 black + } + } + + // Advance the line for the next call, wrapping at the bottom. + g_line_y++; + if (g_line_y >= height) { + g_line_y = 0; + } + + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_0(render_obj, render); + +// move_paddle(delta): print delta for debugging. +static mp_obj_t move_paddle(mp_obj_t delta_obj) { + int delta = mp_obj_get_int(delta_obj); + mp_printf(&mp_plat_print, "delta: %d\n", delta); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(move_paddle_obj, move_paddle); + +// This is the entry point and is called when the module is imported +mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) { + // This must be first, it sets up the globals dict and other things + MP_DYNRUNTIME_INIT_ENTRY + + // Make the function available in the module's namespace + mp_store_global(MP_QSTR_init, MP_OBJ_FROM_PTR(&init_obj)); + mp_store_global(MP_QSTR_render, MP_OBJ_FROM_PTR(&render_obj)); + mp_store_global(MP_QSTR_move_paddle, MP_OBJ_FROM_PTR(&move_paddle_obj)); + mp_store_global(MP_QSTR_readfile, MP_OBJ_FROM_PTR(&readfile_obj)); + + // This must be last, it restores the globals dict + MP_DYNRUNTIME_INIT_EXIT +} diff --git a/internal_filesystem/apps/com.micropythonos.mpong/META-INF/MANIFEST.JSON b/internal_filesystem/apps/com.micropythonos.mpong/META-INF/MANIFEST.JSON new file mode 100644 index 00000000..55463a99 --- /dev/null +++ b/internal_filesystem/apps/com.micropythonos.mpong/META-INF/MANIFEST.JSON @@ -0,0 +1,24 @@ +{ +"name": "mPong", +"publisher": "MicroPythonOS", +"short_description": "PingPong game", +"long_description": "Demonstrates native machinecode in .mpy files on both AMD64 and ESP32", +"icon_url": "https://apps.micropythonos.com/apps/com.micropythonos.mpong/icons/com.micropythonos.mpong_0.1.0_64x64.png", +"download_url": "https://apps.micropythonos.com/apps/com.micropythonos.mpong/mpks/com.micropythonos.mpong_0.1.0.mpk", +"fullname": "com.micropythonos.mpong", +"version": "0.1.0", +"category": "games", +"activities": [ + { + "entrypoint": "assets/mpong.py", + "classname": "MPong", + "intent_filters": [ + { + "action": "main", + "category": "launcher" + } + ] + } + ] +} + diff --git a/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py b/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py new file mode 100644 index 00000000..2e61fc5d --- /dev/null +++ b/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py @@ -0,0 +1,62 @@ +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_esp32 as mpong +else: + import mpong_amd64 as mpong + +class MPong(Activity): + + hor_res = 0 + ver_res = 0 + layer = None + buffer = None + + # Widgets: + screen = None + canvas = None + + def onCreate(self): + self.screen = lv.obj() + self.canvas = lv.canvas(self.screen) + d = lv.display_get_default() + self.hor_res = d.get_horizontal_resolution() + self.ver_res = d.get_vertical_resolution() + self.canvas.set_size(self.hor_res, self.ver_res) + #self.canvas.set_style_bg_color(lv.color_white(), lv.PART.MAIN) + self.buffer = bytearray(self.hor_res * self.ver_res * 2) + self.canvas.set_buffer(self.buffer, self.hor_res, self.ver_res, lv.COLOR_FORMAT.NATIVE) + #self.canvas.fill_bg(lv.color_white(), lv.OPA.COVER) + self.canvas.add_flag(lv.obj.FLAG.CLICKABLE) + self.canvas.add_event_cb(self.touch_cb, lv.EVENT.ALL, None) + self.layer = lv.layer_t() + self.canvas.init_layer(self.layer) + self.setContentView(self.screen) + + def onResume(self, screen): + mpong.init(self.buffer, self.hor_res, self.ver_res) + self.refresh_timer = lv.timer_create(self.run_mpong, 10, None) + + def onPause(self, screen): + print("stopping it!") + if self.refresh_timer: + self.refresh_timer.delete() + + def run_mpong(self, timer=None): + mpong.render() + self.canvas.invalidate() # force redraw + + def touch_cb(self, event): + event_code=event.get_code() + if event_code not in [19,23,25,26,27,28,29,30,49]: + if event_code == lv.EVENT.PRESSING: # this is probably enough + x, y = InputManager.pointer_xy() + mpong.move_paddle(-10) + return diff --git a/internal_filesystem/apps/com.micropythonos.mpong/res/mipmap-mdpi/icon_64x64.png b/internal_filesystem/apps/com.micropythonos.mpong/res/mipmap-mdpi/icon_64x64.png new file mode 100644 index 00000000..30c6a2d7 Binary files /dev/null and b/internal_filesystem/apps/com.micropythonos.mpong/res/mipmap-mdpi/icon_64x64.png differ