devicescript-hola-mundo-simulador

Hello World and Simulator in DeviceScript

  • 4 min

The first program with DeviceScript lets us verify the code on a simulated device before deploying it to the board. We’ll start by writing to the console and then control the status LED.

In the world of microcontrollers, tradition dictates that the first program is not printing text on a screen, but making an LED blink. It’s our “Hello World”. It confirms the processor is alive, we have control over time, and we can act on the physical world.

With DeviceScript, we can do both tests without connecting any external components, thanks to its simulator.

Structure of a DeviceScript Program

Open VS Code and the file src/main.ts created in the previous step. If it contains example code, delete it to start from scratch.

A basic DeviceScript program looks very similar to any modern TypeScript or JavaScript script, but with one key difference: we import functionalities from the hardware.

Write this in the editor:

import { delay } from "@devicescript/core"

console.log("Starting DeviceScript!")

setInterval(async () => {
    console.log("Hello from the loop - " + new Date().toISOString())
    await delay(1000)
}, 1000)
Copied!

The setInterval function includes async because it uses await delay. This way we suspend that task without blocking the runtime while waiting.

Run the program in the simulator

Before connecting the board, let’s test this. This is where DeviceScript shines.

Look for the DeviceScript icon in the VS Code sidebar.

In the “Actions” menu, click the Start Simulator button (or the “Play” icon in the top right corner of the editor).

Suddenly, a panel called DeviceScript Dashboard will open on the right (or in a floating window).

This panel is a virtual representation of a device and will show a simulated board.

In the VS Code terminal, within the DeviceScript tab, the following messages will appear:

> Starting DeviceScript!
> Hello from the loop - 2023-10-27T10:00:01.000Z
> Hello from the loop - 2023-10-27T10:00:02.000Z
...
Copied!

Congratulations! You have just run your first virtualized firmware.

Printing text is fine, but we want lights. In DeviceScript, hardware is controlled through Services (a Jacdac concept).

However, to make things easier at the start, DeviceScript includes functions to control the Status Light (that small LED almost all boards have built-in, or the RGB LED on ESP32-C3 boards).

Let’s modify the code:

import { delay } from "@devicescript/core"
import { setStatusLight } from "@devicescript/runtime"

console.log("Starting the blink...")

setInterval(async () => {
    // Turn on the LED in Red (Hexadecimal Format 0xRRGGBB)
    await setStatusLight(0xff0000)
    await delay(500)
    
    // Turn off the LED (Black)
    await setStatusLight(0x000000) 
    
    // Optional: Blue to make it look different
    // await setStatusLight(0x0000ff)
}, 1000)
Copied!

Save the file. If the simulator is open, it will reload automatically.

In the dashboard, the virtual board will show a small circle blinking red.

This is the great power of DeviceScript: the development cycle is immediate. You change code, save, and the simulator reflects the change in milliseconds.

Run it on the physical board

Now, let’s get real. We want to see this on our board (ESP32 or Pico W).

Connect the board to USB if it isn’t already.

Make sure it is Connected in the DeviceScript extension (it should appear in the “Devices” list).

Click the Run button (the Play icon) at the top right of the main.ts file.

First, the notification Building… will appear, followed by Deploying….

What is happening?

VS Code compiles your TypeScript to Bytecode, sends it via the USB cable to the virtual machine installed on the board, and tells it: “Execute this.”

When deployment finishes, the board’s LED should start blinking red.

The simulator can remain open at the same time. The virtual LED and the physical one should show equivalent behavior.

Real-time debugging

The console.log messages will also continue to appear in the VS Code terminal.

Data arrives from the physical board in real-time. When you disconnect the cable, the program may continue running, but the messages will stop reaching the computer.