Skip to content
Snippets Groups Projects
Commit a83742a4 authored by q3k's avatar q3k
Browse files

sim: update for new badge payload

parent 2ce5451a
No related branches found
No related tags found
No related merge requests found
...@@ -61,6 +61,17 @@ class Wasm: ...@@ -61,6 +61,17 @@ class Wasm:
args = [float(a) for a in args] args = [float(a) for a in args]
return self._i.exports.ctx_apply_transform(ctx, *args) return self._i.exports.ctx_apply_transform(ctx, *args)
def ctx_text_width(self, ctx, text):
s = text.encode('utf-8')
slen = len(s) + 1
p = self.malloc(slen)
mem = self._i.exports.memory.uint8_view(p)
mem[0:slen-1] = s
mem[slen-1] = 0
res = self._i.exports.ctx_text_width(ctx, p)
self.free(p)
return res
_wasm = Wasm() _wasm = Wasm()
class Ctx: class Ctx:
...@@ -84,6 +95,7 @@ class Ctx: ...@@ -84,6 +95,7 @@ class Ctx:
self.text_align = 'start' self.text_align = 'start'
self.text_baseline = 'alphabetic' self.text_baseline = 'alphabetic'
self.font_size = 10.0
def _get_fb(self): def _get_fb(self):
return _wasm._i.exports.memory.uint8_view(self._fb) return _wasm._i.exports.memory.uint8_view(self._fb)
...@@ -92,7 +104,7 @@ class Ctx: ...@@ -92,7 +104,7 @@ class Ctx:
_wasm.ctx_parse(self._ctx, text) _wasm.ctx_parse(self._ctx, text)
def move_to(self, x, y): def move_to(self, x, y):
self._emit(f"moveTo {x} {x}") self._emit(f"moveTo {int(x)} {int(y)}")
return self return self
def rgb(self, r, g, b): def rgb(self, r, g, b):
...@@ -102,17 +114,18 @@ class Ctx: ...@@ -102,17 +114,18 @@ class Ctx:
r /= 255.0 r /= 255.0
g /= 255.0 g /= 255.0
b /= 255.0 b /= 255.0
self._emit(f"rgb {r} {g} {b}") self._emit(f"rgb {r:.3f} {g:.3f} {b:.3f}")
return self return self
def text(self, s): def text(self, s):
self._emit(f"textAlign {self.text_align}") self._emit(f"textAlign {self.text_align}")
self._emit(f"textBaseline {self.text_baseline}") self._emit(f"textBaseline {self.text_baseline}")
self._emit(f"fontSize {self.font_size}")
self._emit(f"text \"{s}\"") self._emit(f"text \"{s}\"")
return self return self
def round_rectangle(self, x, y, width, height, radius): def round_rectangle(self, x, y, width, height, radius):
self._emit(f"roundRectangle {x} {y} {width} {height} {radius}") self._emit(f"roundRectangle {int(x)} {int(y)} {int(width)} {int(height)} {radius}")
return self return self
def rectangle(self, x, y, width, height): def rectangle(self, x, y, width, height):
...@@ -124,5 +137,9 @@ class Ctx: ...@@ -124,5 +137,9 @@ class Ctx:
return self return self
def arc(self, x, y , radius, arc_from, arc_to, direction): def arc(self, x, y , radius, arc_from, arc_to, direction):
self._emit(f"arc {x} {y} {radius} {arc_from} {arc_to} {direction}") self._emit(f"arc {int(x)} {int(y)} {int(radius)} {arc_from:.4f} {arc_to:.4f} {1 if direction else 0}")
return self return self
def text_width(self, text):
self._emit(f"fontSize {self.font_size}")
return _wasm.ctx_text_width(self._ctx, text)
\ No newline at end of file
def mem_free():
return 1337
...@@ -347,15 +347,14 @@ def update_leds(): ...@@ -347,15 +347,14 @@ def update_leds():
def set_global_volume_dB(a): def set_global_volume_dB(a):
pass pass
def get_button_state(left):
def get_button(a):
_sim.process_events() _sim.process_events()
_sim.render_gui_lazy() _sim.render_gui_lazy()
state = _sim.buttons.state() state = _sim.buttons.state()
if a == 1: if left == 1:
sub = state[:3] sub = state[:3]
elif a == 0: elif left == 0:
sub = state[3:6] sub = state[3:6]
else: else:
return 0 return 0
...@@ -369,6 +368,28 @@ def get_button(a): ...@@ -369,6 +368,28 @@ def get_button(a):
return 0 return 0
menu_button_left = 0
def menu_button_get():
return get_button_state(menu_button_left)
def application_button_get():
return get_button_state(1 - menu_button_left)
def left_button_get():
return get_button_state(1)
def right_button_get():
return get_button_state(0)
def menu_button_set_left(_broken):
global menu_button_left
menu_button_left = 1
def menu_button_get_left():
return menu_button_left
def get_captouch(a): def get_captouch(a):
_sim.process_events() _sim.process_events()
_sim.render_gui_lazy() _sim.render_gui_lazy()
......
import importlib
_time = importlib.import_module('_time')
def sleep_ms(ms): def sleep_ms(ms):
import _time
_time.sleep(ms * 0.001) _time.sleep(ms * 0.001)
def ticks_ms(): def ticks_ms():
import _time
return int(_time.time() * 1000) return int(_time.time() * 1000)
def ticks_diff(a, b):
return a - b
\ No newline at end of file
...@@ -8,37 +8,46 @@ import sys ...@@ -8,37 +8,46 @@ import sys
projectpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) projectpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
class Hook: import random
""" import pygame
Hook implements a importlib.abc.Finder which overwrites resolution order to import cmath
more closely match the resolution order on the badge's Micropython import wasmer
environment. import wasmer_compiler_cranelift
"""
class UnderscoreFinder(importlib.abc.MetaPathFinder):
def __init__(self, builtin):
self.builtin = builtin
def find_spec(self, fullname, path, target=None): def find_spec(self, fullname, path, target=None):
# Attempt to load from python_payload, then python_modules then
# sim/fakes. Afterwards, the normal import resolution order kicks in.
paths = [
os.path.join(projectpath, 'python_payload', fullname+'.py'),
os.path.join(projectpath, 'python_payload', fullname),
os.path.join(projectpath, 'python_modules', fullname+'.py'),
os.path.join(projectpath, 'python_modules', fullname),
os.path.join(projectpath, 'sim', 'fakes', fullname+'.py'),
]
for p in paths:
if os.path.exists(p):
root = os.path.split(p)[:-1]
return PathFinder.find_spec(fullname, root)
# As we provide our own micropython-compatible time library, allow
# resolving the original CPython time through _time
if fullname == '_time': if fullname == '_time':
return BuiltinImporter.find_spec('time') return self.builtin.find_spec('time', path, target)
return None if fullname in ['random']:
print(fullname, path, target)
return self.builtin.find_spec(fullname, path, target)
sys.meta_path.insert(0, Hook())
sys.path_importer_cache.clear() #sys.meta_path.insert(0, Hook())
sys.path = [
os.path.join(projectpath, 'python_payload'),
os.path.join(projectpath, 'sim', 'fakes'),
]
builtin = BuiltinImporter()
pathfinder = PathFinder()
underscore = UnderscoreFinder(builtin)
sys.meta_path = [pathfinder, underscore]
# Clean up whatever might have already been imported as `time`. # Clean up whatever might have already been imported as `time`.
import time import time
print('aaaa', time)
importlib.reload(time) importlib.reload(time)
print('bbbb', time)
sys.path_importer_cache.clear()
importlib.invalidate_caches()
sys.modules['time'] = time
import main import main
\ No newline at end of file
...@@ -17,7 +17,7 @@ fi ...@@ -17,7 +17,7 @@ fi
emcc ctx.c \ emcc ctx.c \
-I ../../usermodule/uctx/uctx/ \ -I ../../usermodule/uctx/uctx/ \
-I ../../usermodule/uctx/fonts/ \ -I ../../usermodule/uctx/fonts/ \
-s EXPORTED_FUNCTIONS=_ctx_new_for_framebuffer,_ctx_parse,_ctx_apply_transform,_malloc,_free \ -s EXPORTED_FUNCTIONS=_ctx_new_for_framebuffer,_ctx_parse,_ctx_apply_transform,_ctx_text_width,_malloc,_free \
--no-entry -flto -O3 \ --no-entry -flto -O3 \
-o ctx.wasm -o ctx.wasm
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment