DeviceScript - TypeScript for micros

Story

Next we start an interval every second. If you are familiar with JavaScript, this is done using the setInterval global function. Notice also that the lambda (arrow) function passed to setInterval is marked as async. This allows us to use the await keyboard in TypeScript.

setInterval(async () => {

}, 1000)

We read the state of the lightbulb by accessing the intensity register, which maps to brightness [0..1] for the lightbulb. Interacting with a Jacdac service is always an asynchronous operation (the service might be on a different device connected with a cable, or it might require an I2C message, etc...) so it has to be awaited using await. This allow other fibers to run while we are waiting for the read to return.

setInterval(async () => {
...
const brightness = await led.intensity.read()
...
}, 1000)

When developing in Visual Studio Code, you can use simulators, for the Pico and sensors, and the debugger with breakpoints, variables views, etc... The screenshot below shows the program halted in the debugger with a pico simulator and a light bulb simulator.

Based on the value of brightness, we toggle the value and write the new value.

const brightness = await led.intensity.read()
const newbrightness = brightness > 0 ? 0 : 1
await led.intensity.write(newbrightness)

The full annotated source code is here. We'll get into each part separately.

import { pins } from "@dsboard/pico"
import { startLightBulb } from "@devicescript/servers"

// start a lightbulb server on pin GP1
// and store client in `led` variable
const led = startLightBulb({
pin: pins.P1,
})

// start interval timer every 1000ms
setInterval(async () => {
// read current brightness
const brightness = await led.intensity.read()
// toggle on/off
const newbrightness = brightness > 0 ? 0 : 1
// apply new brightness
await led.intensity.write(newbrightness)
}, 1000)

Working with hardware

So far, we've been working and debugging using simulators. It's time to move on to hardware and try things out on the metal. Grab a Raspberry Pico, an LED and a resistor to assemble the hardware (remember that GP1 is labelled 2 on the silk, though you'll notice GP1 on the bottom of the board).

Now that we have the LED in place and the Pico connected to the computer we can start by flashing the DeviceScript runtime on the Pico. This is one-time operation, you will only have to update the DeviceScript compiled bytecode in the future.

Once the firmware is flashed, hit Connect -> Serial again and you'll see your device in the Devices tree. The device tree gives you a live glimpse about the state of the services and also allows you to drill in into each of them.

Use the same "Run/Debug" menu item on the file menu to work with the Raspberry Pi Pico as you did with the simulators. That's it, you just programmed your first blinky app!

Better tooling, better productivity

At the time of writing this article, it turns out I had plugged the LED backwards and nothing worked of course. The debug this issue I was able to use several tools provided by DeviceScript: console output, live inspection through the Devices tree, debugger. I was able to assert that my code was correct and the wiring was probably wrong very quickly.

Next Steps

The developer documentation for DeviceScript contain more example of usage. We are looking for feedback from the community about DeviceScript in the project Discussion page. Thank you for reading?

No comments:

Post a Comment