Skip to content

Added a LED toggle demo fpr RP-2040-Zero #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 15 additions & 37 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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()
`));
};

</script>
</head>
<body>
<button id="connect">connect</button>
<button id="toggle" disabled>LED toggle</button>
<pre id="output"></pre>
</body>
</html>
5 changes: 3 additions & 2 deletions mpy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
<title>PyScript - MicroPython REPL Demo</title>
<link rel="stylesheet" href="../css/index.css">
<!-- PyScript -->
<link rel="stylesheet" href="https://pyscript.net/releases/2024.4.2/core.css">
<script type="module" src="https://pyscript.net/releases/2024.4.2/core.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2024.5.1/core.css">
<script type="module" src="https://pyscript.net/releases/2024.5.1/core.js"></script>
</head>
<body>
<script type="mpy" src="index.py" config="index.toml"></script>
<button id="connect">connect</button>
<button id="toggle" disabled>LED toggle</button>
<pre id="output"></pre>
</body>
</html>
67 changes: 29 additions & 38 deletions mpy/index.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion mpy/index.toml
Original file line number Diff line number Diff line change
@@ -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"