@@ -26,25 +26,30 @@ def const(x):
2626 sys .implementation .name = "micropython"
2727 print ("Implementation is now: " + sys .implementation .name )
2828
29+
2930def patch_filesystem ():
3031 # New apps are downloaded to /apps and /backgrounds
3132 # It's hardcoded. We need to make sure files out of those
3233 # directories are importable.
3334 import os
35+
3436 os .mkdir ("/apps" )
3537 os .symlink ("/apps" , "/home/pyodide/apps" )
3638
3739 os .mkdir ("/backgrounds" )
3840 os .symlink ("/backgrounds" , "/home/pyodide/backgrounds" )
3941
42+
4043async def monkey_patch_http ():
4144 # requests doesn't work in pyscript without this voodoo
4245
4346 import micropip
47+
4448 await micropip .install ("pyodide-http" )
4549 await micropip .install ("requests" )
4650
4751 import pyodide_http
52+
4853 pyodide_http .patch_all ()
4954
5055 import requests
@@ -67,8 +72,10 @@ def get(url, *args, **kwargs):
6772
6873def monkey_patch_sys ():
6974 if not hasattr (sys , "print_exception" ):
75+
7076 def print_exception (e , file ):
7177 print ("Exception:" , e , file = file )
78+
7279 sys .print_exception = print_exception
7380
7481
@@ -152,7 +159,7 @@ class FakeCtx:
152159 def __init__ (self ):
153160 self .width = 240
154161 self .height = 240
155- self .scale = 3 # The number of web pixels per "display" pixel
162+ self .scale = 1 # The number of web pixels per "display" pixel
156163 self .border = 10
157164
158165 self .font_size = 8
@@ -162,7 +169,7 @@ def __init__(self):
162169 self .RIGHT = 3
163170 self .MIDDLE = 4
164171
165- self .color = "rgb(0, 255, 0)" # FIXME: find what the default color is
172+ self .color = "rgb(0, 255, 0)" # FIXME: find what the default color is
166173 self .position = (0 , 0 )
167174 self ._translate = (0 , 0 )
168175 self ._saves = []
@@ -173,9 +180,9 @@ def __init__(self):
173180
174181 self ._ctx .beginPath ()
175182 self ._ctx .arc (
176- (3 * self .width + 2 * self .border ) / 2 ,
177- (3 * self .height + 2 * self .border ) / 2 ,
178- (3 * self .width ) / 2 ,
183+ (self . scale * self .width + 2 * self .border ) / 2 ,
184+ (self . scale * self .height + 2 * self .border ) / 2 ,
185+ (self . scale * self .width ) / 2 ,
179186 0 ,
180187 2 * math .pi ,
181188 )
@@ -213,11 +220,11 @@ def restore(self):
213220 if len (self ._saves ) > 0 :
214221 return self ._saves .pop ()
215222 else :
216- # print("Warning: restore() called with no matching save()")
223+ # print("Warning: restore() called with no matching save()")
217224 return self
218225
219226 def move_to (self , x , y ):
220- # print("ctx.move_to(%s, %s)" % (x, y))
227+ # print("ctx.move_to(%s, %s)" % (x, y))
221228 new = self .clone ()
222229 new .position = (x , y )
223230 return new
@@ -261,12 +268,19 @@ def image(self, path, x, y, w, h):
261268 img .src = src
262269
263270 ctx = self ._ctx
264- ctx .drawImage (img , self ._x_to_web (x ), self ._y_to_web (y ), w * self .scale , h * self .scale )
271+ ctx .drawImage (
272+ img , self ._x_to_web (x ), self ._y_to_web (y ), w * self .scale , h * self .scale
273+ )
265274 return self
266275
267276 def linear_gradient (self , x0 , y0 , x1 , y1 ):
268277 ctx = self ._ctx
269- gradient = ctx .createLinearGradient (self ._x_to_web (x0 ), self ._y_to_web (y0 ), self ._x_to_web (x1 ), self ._y_to_web (y1 ))
278+ gradient = ctx .createLinearGradient (
279+ self ._x_to_web (x0 ),
280+ self ._y_to_web (y0 ),
281+ self ._x_to_web (x1 ),
282+ self ._y_to_web (y1 ),
283+ )
270284 self ._gradient = gradient
271285 return self
272286
@@ -285,7 +299,7 @@ def fill(self):
285299 return self
286300
287301 def text_width (self , text ):
288- # print("Not properly implemented: FakeCtx: text_width(%s)" % text)
302+ # print("Not properly implemented: FakeCtx: text_width(%s)" % text)
289303 return len (text ) * 8
290304
291305 def text (self , text ):
@@ -294,7 +308,7 @@ def text(self, text):
294308 ctx .fillStyle = self .color
295309 ctx .font = f"{ 8 * self .scale } px sans-serif"
296310 x , y = self .position
297- # print("Drawing text at", self._x_to_web(x), self._y_to_web(y), "in color", self.color)
311+ # print("Drawing text at", self._x_to_web(x), self._y_to_web(y), "in color", self.color)
298312 ctx .fillText (text , self ._x_to_web (x ), self ._y_to_web (y ))
299313 ctx .restore ()
300314
@@ -325,7 +339,7 @@ def get_ctx():
325339
326340 @staticmethod
327341 def end_frame (ctx ):
328- # print("Not implemented: FakeDisplay: end_frame()")
342+ # print("Not implemented: FakeDisplay: end_frame()")
329343 pass
330344
331345 sys .modules ["display" ] = FakeDisplay
@@ -355,6 +369,7 @@ class FakeSPI:
355369 pass
356370
357371 from types import ModuleType
372+
358373 m = ModuleType ("machine" )
359374 sys .modules [m .__name__ ] = m
360375
@@ -365,6 +380,7 @@ class FakeSPI:
365380
366381def monkey_patch_tildagon ():
367382 from types import ModuleType
383+
368384 m = ModuleType ("tildagon" )
369385 sys .modules [m .__name__ ] = m
370386
@@ -388,6 +404,7 @@ def __call__(self, *args, **kwargs):
388404
389405def monkey_patch_ePin ():
390406 from types import ModuleType
407+
391408 m = ModuleType ("egpio" )
392409 sys .modules [m .__name__ ] = m
393410
@@ -449,7 +466,7 @@ def __setitem__(self, item, value):
449466 if item > self .length :
450467 print ("FakeNeoPixel: Ignoring setitem out of range" , item )
451468 else :
452- self .rgb [item - 1 ] = value
469+ self .rgb [item - 1 ] = value
453470
454471 class FakeNeoPixelModule :
455472 NeoPixel = FakeNeoPixel
@@ -479,8 +496,8 @@ async def badge():
479496 ctx = canvas ._js .getContext ("2d" )
480497
481498 # Set the canvas size
482- width = 3 * resolution_x + 2 * border
483- height = 3 * resolution_y + 2 * border
499+ width = 1 * resolution_x + 2 * border
500+ height = 1 * resolution_y + 2 * border
484501
485502 canvas .style ["width" ] = f"{ width } px"
486503 canvas .style ["height" ] = f"{ height } px"
@@ -491,9 +508,9 @@ async def badge():
491508 ctx .fillStyle = "rgb(0 100 0)"
492509 ctx .beginPath ()
493510 ctx .arc (
494- (3 * resolution_x + 2 * border ) / 2 ,
495- (3 * resolution_y + 2 * border ) / 2 ,
496- (3 * resolution_x + 2 * border ) / 2 ,
511+ (1 * resolution_x + 2 * border ) / 2 ,
512+ (1 * resolution_y + 2 * border ) / 2 ,
513+ (1 * resolution_x + 2 * border ) / 2 ,
497514 0 ,
498515 2 * math .pi ,
499516 )
@@ -504,9 +521,9 @@ async def badge():
504521 ctx .fillStyle = "rgb(0 0 0)"
505522 ctx .beginPath ()
506523 ctx .arc (
507- (3 * resolution_x + 2 * border ) / 2 ,
508- (3 * resolution_y + 2 * border ) / 2 ,
509- (3 * resolution_x ) / 2 ,
524+ (1 * resolution_x + 2 * border ) / 2 ,
525+ (1 * resolution_y + 2 * border ) / 2 ,
526+ (1 * resolution_x ) / 2 ,
510527 0 ,
511528 2 * math .pi ,
512529 )
@@ -525,15 +542,17 @@ async def button_handler(event):
525542 from system .eventbus import eventbus
526543 from frontboards .twentyfour import BUTTONS
527544 from events .input import ButtonDownEvent , ButtonUpEvent
545+
528546 print ("Emitting ButtonDownEvent for button" , BUTTONS [event .target .id ])
529547 await eventbus .emit_async (ButtonDownEvent (button = BUTTONS [event .target .id ]))
530548
549+
531550@create_proxy
532551async def on_key_down (event ):
533-
534552 from system .eventbus import eventbus
535553 from frontboards .twentyfour import BUTTONS
536554 from events .input import ButtonDownEvent , ButtonUpEvent
555+
537556 match event .key :
538557 case "ArrowUp" :
539558 print ("Emitting ButtonDownEvent for button A" )
@@ -575,4 +594,5 @@ async def start_tildagon_os():
575594async def main ():
576595 _ = await asyncio .gather (badge ())
577596
597+
578598asyncio .ensure_future (main ())
0 commit comments