diff --git a/index.html b/index.html index 4f74ada..aa8e3fa 100644 --- a/index.html +++ b/index.html @@ -28,51 +28,29 @@ } }).then(async board => { globalThis.board = board; - - // there is an active state - console.info('Board active', board.active); - - // once initialized, the REPL welcome message can be - // fully ignored or showed - show(await board.output); - - // this works for any Python code + toggle.disabled = false; await board.write(dedent(` - import sys - - print(sys.version) - print(sys.implementation._machine) + import machine, neopixel + pixel_pin = 16 + pixel = neopixel.NeoPixel(machine.Pin(pixel_pin), 1) `)); - - const lines = (await board.output).split(/[\r\n]+/); - show(lines.slice(-3).join('\n')); - - // each `write` wait for a result - await board.write('help()'); - // where the output can be read - show(await board.output); - - await board.write('print("bye bye")'); - - // we can close it without errors - await board.close(); - - // once closed it throws on write and read - // but the result would be still there - console.info('Board active', board.active); - - // the final line/result remains though - const result = await board.result; - if (/^\S+?Error: /.test(result)) - console.warn(result); - else - console.info('last result', result); }); }; + + let active = false; + toggle.onclick = () => { + active = !active; + board.write(dedent(` + pixel[0] = (${active ? '255, 255, 255' : '0, 0, 0'}) + pixel.write() + `)); + }; + +

 
 
diff --git a/mpy/index.html b/mpy/index.html
index f155f2a..33252e7 100644
--- a/mpy/index.html
+++ b/mpy/index.html
@@ -6,12 +6,13 @@
   PyScript - MicroPython REPL Demo
   
   
-  
-  
+  
+  
 
 
   
   
+  
   

 
 
diff --git a/mpy/index.py b/mpy/index.py
index 5b6ad82..5570a04 100644
--- a/mpy/index.py
+++ b/mpy/index.py
@@ -1,48 +1,39 @@
-from pyscript import document, window
+from pyscript import document
 
-from pyscript.ffi import to_js
 from pyscript.js_modules.dedent import default as dedent
 from pyscript.js_modules.micro_repl import default as init
 
-connect, output, = document.querySelectorAll("#connect, #output")
-
-def once_closed(error):
-    connect.disabled = False
-    if (error):
-        window.console.warn(error)
-
-def show(content):
-    code = document.createElement("code")
-    code.textContent = content
-    output.append(code)
+active = False
+board = None
+connect, toggle, = document.querySelectorAll("#connect, #toggle")
+
+async def ontoggle(e):
+    global active, board
+    active = not active
+    if active:
+        R = 255
+        G = 255
+        B = 255
+    else:
+        R = 0
+        G = 0
+        B = 0
+    await board.write(dedent(f"""
+        pixel[0] = ({R},{G},{B})
+        pixel.write()
+    """))
 
 async def onclick(event):
+    global board
     connect.disabled = True
-    output.replaceChildren()
-    spike3 = await init(to_js({ "onceClosed": once_closed }))
-    print('Spike3 active', spike3.active)
-    show(await spike3.output)
-    await spike3.write('help()')
-    show(await spike3.output)
-    await spike3.write(dedent("""
-        from hub import light_matrix
-        import runloop
-
-        async def main():
-            await light_matrix.write("Hello World!")
-
-        runloop.run(main())
-    """))
-    await spike3.write(dedent("""
-        def test():
-            return 1 + 2
-
-        print(test())
+    board = await init()
+    toggle.disabled = False
+    toggle.onclick = ontoggle
+    print('Board active', board.active)
+    await board.write(dedent("""
+        import machine, neopixel
+        pixel_pin = 16
+        pixel = neopixel.NeoPixel(machine.Pin(pixel_pin), 1)
     """))
-    show(await spike3.output)
-    await spike3.close()
-    print('Spike3 active', spike3.active)
-    result = await spike3.result
-    print('last result', result)
 
 connect.onclick = onclick
diff --git a/mpy/index.toml b/mpy/index.toml
index 11c25eb..86bc35c 100644
--- a/mpy/index.toml
+++ b/mpy/index.toml
@@ -1,3 +1,3 @@
 [js_modules.main]
 "https://cdn.jsdelivr.net/npm/codedent/es.js" = "dedent"
-"https://esm.run/micro-repl" = "micro_repl"
+"../micro-repl.js" = "micro_repl"