Your first script
Blink the on-board LED. This is examples/blink/main.ts:
// Blink the on-board LED every 500 ms and log each toggle.
// main.js is generated from main.ts (cd js && npm run examples); edit main.ts.
const led = board.LED;
let on = false;
if (led === undefined) {
console.log("blink: board " + board.name + " has no plain-GPIO LED (board.LED is undefined)");
} else {
gpio.mode(led, "output");
setInterval(() => {
on = !on;
gpio.write(led, on ? 1 : 0);
console.log(on ? "LED on" : "LED off");
}, 500);
}board.LEDis the LED's GPIO number (25 on Pico and Pico 2),undefinedwhere the LED isn't a plain GPIO.gpio.mode/gpio.writedrive the pin;setIntervalruns the callback every 500 ms from the firmware's event loop.console.logwrites to USB serial.
Compile
MQuickJS runs ES5, so TypeScript is compiled with tsc (target: ES5), which also type-checks it against the API typings (@jsbedded/console, timers, gpio, board):
cd js && npm install && npm run examples # examples/blink/main.ts -> main.jsCopy it to the board
On the Pico family, with the firmware installed (jsbedded-pico-mquickjs.uf2 or jsbedded-pico2-mquickjs.uf2), the board shows up as a USB drive named JSBEDDED next to its USB serial port:
- Copy
examples/blink/main.jsto theJSBEDDEDdrive. - Eject the drive, so the PC finishes writing, then reset the board: unplug and plug it in again (without BOOTSEL), or press RUN where the board has one.
- Open the USB serial port and watch
LED on/LED offevery half second.
At boot the firmware runs the first of main.js, index.js, main.mjs, index.mjs it finds on the drive. The script is read once, at boot: after editing it, reset again. If a script misbehaves, dev mode starts the board without running it.
Or build it into the firmware
Boards without the drive (ESP32 for now), or an image that should always run one script, take it built in:
make firmware BOARD=pico ENGINE=mquickjs SCRIPT=examples/blink/main.jsA script on the drive still takes precedence.
Errors are printed with their position, e.g. Uncaught TypeError: … at main.js:9:9; the event loop keeps running the other timers.