Skip to content

Commit 4445b11

Browse files
committed
Added a LED toggle demo fpr RP-2040-Zero
1 parent 0143c87 commit 4445b11

File tree

4 files changed

+49
-78
lines changed

4 files changed

+49
-78
lines changed

index.html

+15-37
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,29 @@
2828
}
2929
}).then(async board => {
3030
globalThis.board = board;
31-
32-
// there is an active state
33-
console.info('Board active', board.active);
34-
35-
// once initialized, the REPL welcome message can be
36-
// fully ignored or showed
37-
show(await board.output);
38-
39-
// this works for any Python code
31+
toggle.disabled = false;
4032
await board.write(dedent(`
41-
import sys
42-
43-
print(sys.version)
44-
print(sys.implementation._machine)
33+
import machine, neopixel
34+
pixel_pin = 16
35+
pixel = neopixel.NeoPixel(machine.Pin(pixel_pin), 1)
4536
`));
46-
47-
const lines = (await board.output).split(/[\r\n]+/);
48-
show(lines.slice(-3).join('\n'));
49-
50-
// each `write` wait for a result
51-
await board.write('help()');
52-
// where the output can be read
53-
show(await board.output);
54-
55-
await board.write('print("bye bye")');
56-
57-
// we can close it without errors
58-
await board.close();
59-
60-
// once closed it throws on write and read
61-
// but the result would be still there
62-
console.info('Board active', board.active);
63-
64-
// the final line/result remains though
65-
const result = await board.result;
66-
if (/^\S+?Error: /.test(result))
67-
console.warn(result);
68-
else
69-
console.info('last result', result);
7037
});
7138
};
39+
40+
let active = false;
41+
toggle.onclick = () => {
42+
active = !active;
43+
board.write(dedent(`
44+
pixel[0] = (${active ? '255, 255, 255' : '0, 0, 0'})
45+
pixel.write()
46+
`));
47+
};
48+
7249
</script>
7350
</head>
7451
<body>
7552
<button id="connect">connect</button>
53+
<button id="toggle" disabled>LED toggle</button>
7654
<pre id="output"></pre>
7755
</body>
7856
</html>

mpy/index.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
<title>PyScript - MicroPython REPL Demo</title>
77
<link rel="stylesheet" href="../css/index.css">
88
<!-- PyScript -->
9-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.4.2/core.css">
10-
<script type="module" src="https://pyscript.net/releases/2024.4.2/core.js"></script>
9+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.5.1/core.css">
10+
<script type="module" src="https://pyscript.net/releases/2024.5.1/core.js"></script>
1111
</head>
1212
<body>
1313
<script type="mpy" src="index.py" config="index.toml"></script>
1414
<button id="connect">connect</button>
15+
<button id="toggle" disabled>LED toggle</button>
1516
<pre id="output"></pre>
1617
</body>
1718
</html>

mpy/index.py

+30-38
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
1-
from pyscript import document, window
1+
from pyscript import document
22

3-
from pyscript.ffi import to_js
43
from pyscript.js_modules.dedent import default as dedent
54
from pyscript.js_modules.micro_repl import default as init
65

7-
connect, output, = document.querySelectorAll("#connect, #output")
8-
9-
def once_closed(error):
10-
connect.disabled = False
11-
if (error):
12-
window.console.warn(error)
13-
14-
def show(content):
15-
code = document.createElement("code")
16-
code.textContent = content
17-
output.append(code)
6+
active = False
7+
board = None
8+
connect, toggle, = document.querySelectorAll("#connect, #toggle")
9+
10+
async def ontoggle(e):
11+
global active, board
12+
active = not active
13+
if active:
14+
R = 255
15+
G = 255
16+
B = 255
17+
else:
18+
R = 0
19+
G = 0
20+
B = 0
21+
await board.write(dedent(f"""
22+
pixel[0] = ({R},{G},{B})
23+
pixel.write()
24+
"""))
25+
print(await board.output)
1826

1927
async def onclick(event):
28+
global board
2029
connect.disabled = True
21-
output.replaceChildren()
22-
spike3 = await init(to_js({ "onceClosed": once_closed }))
23-
print('Spike3 active', spike3.active)
24-
show(await spike3.output)
25-
await spike3.write('help()')
26-
show(await spike3.output)
27-
await spike3.write(dedent("""
28-
from hub import light_matrix
29-
import runloop
30-
31-
async def main():
32-
await light_matrix.write("Hello World!")
33-
34-
runloop.run(main())
35-
"""))
36-
await spike3.write(dedent("""
37-
def test():
38-
return 1 + 2
39-
40-
print(test())
30+
board = await init()
31+
toggle.disabled = False
32+
toggle.onclick = ontoggle
33+
print('Board active', board.active)
34+
await board.write(dedent("""
35+
import machine, neopixel
36+
pixel_pin = 16
37+
pixel = neopixel.NeoPixel(machine.Pin(pixel_pin), 1)
4138
"""))
42-
show(await spike3.output)
43-
await spike3.close()
44-
print('Spike3 active', spike3.active)
45-
result = await spike3.result
46-
print('last result', result)
4739

4840
connect.onclick = onclick

mpy/index.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[js_modules.main]
22
"https://cdn.jsdelivr.net/npm/codedent/es.js" = "dedent"
3-
"https://esm.run/micro-repl" = "micro_repl"
3+
"../micro-repl.js" = "micro_repl"

0 commit comments

Comments
 (0)