JD Brain RP2040 and Jacdac Modules for DeviceScript Platforms

by KittenBot in Circuits > Microcontrollers

242 Views, 0 Favorites, 0 Comments

JD Brain RP2040 and Jacdac Modules for DeviceScript Platforms

0fc1b926-bbd7-48c0-a97e-ff3271e17dd4.png

JD Brain RP2040 and Jacdac Modules for DeviceScript Platforms

What Is DeviceScript?

If you are familiar with or have used MicroPython, you would know that MicroPython is a lightweight version of the Python programming language specifically designed for embedded systems and microcontrollers.

DeviceScript, initiated by Microsoft, is a TypeScript-based language for hardware development. It is a scripting language suited for embedded devices and IoT applications, offering features such as simplicity, cross-platform support, device management, network communication, and scalability.

DeviceScript brings a TypeScript developer experience to resource-constrained devices based on microcontrollers. DeviceScript code is compiled into custom VM bytecode that can run under highly constrained conditions.

If you are a TypeScript developer looking to venture into hardware, you don't want to miss out on DeviceScript!

Hardware for DeviceScript

DeviceScript supports mainstream hardware devices.

Jacdac is a platform for quick and easy prototyping of new electronic experiences.

Brief Introduction

  • The following tutorial uses JD Brain RP2040 as the main controller and Jacdac KitA as the module for demonstration.
  • It will guide you on how to set up DeviceScript configuration.
  • And with a simple keyboard example, it will show you how to download the program to the hardware.

Software Installation

You need to download and install the software, which is an easy process.

Installing the DeviceScript Extension

Open VScode, search for "Devicescript" in the Extensions sidebar, and click on "Install" to install it.

The DeviceScript extension has been successfully installed.

Setting Up Your Project

5c2cc4f7-c212-40f3-9b8a-30292c727e92.png
31cbe0bc-b637-4166-87a7-c484280eecbe.png
dabc46dc-67d0-411c-b1ff-040cfacd327d.png

I've been stuck at this part for a long time...

From the command palette (Ctrl+Shift+P)!!!, type DeviceScript: Create New Project...

The creation is completed.

You will see the newly created "src" folder in your directory, which contains the "main.ts" file. You can start programming by editing this "main.ts" file.


Updating Firmware for the Mainboard

Before using the Brain RP2040, you need to update it with DeviceScript firmware.

Connect the Brain RP2040 to your computer using a data cable.

1、Open the webpage:MSR RP2040 Brain 124 v0.1 | DeviceScript (microsoft.github.io)

2、Download the firmware.

3、Drag and drop the firmware onto the U-disk of RPI-RP2. Once the update is complete, the mainboard will automatically restart.

Connecting the Mainboard

Go back to VSCode and select "Connect Device" -> "Serial".

Connection successful.

Connecting the Jacdac Module

You can connect the Jacdac module either through a Jacdac connection cable

or by using screws and brass pillars.

In VSCode, you should see that the Jacdac KeycapButton is online.

Getting Started With Programming

1、Open src/main.ts file.

2、Copy and paste the following code into main.ts. This code allows the BrainPad 2040 to act as an HID keyboard and send the Backspace key when a button is pressed

import { pins, board } from "@dsboard/msr124"
import { startHidKeyboard} from "@devicescript/servers"
import { Button } from "@devicescript/core"


import * as ds from "@devicescript/core"


const button = new Button()


const keyboard = startHidKeyboard({
})


button.down.subscribe(async ()=>{
    const selector=ds.HidKeyboardSelector.Backspace
    const modifier = ds.HidKeyboardModifiers.None
    const action = ds.HidKeyboardAction.Press


    await keyboard.key(selector,modifier,action)


})

3、Click on "Run" to download the program to the hardware. Also, open the simulator to see the triggered effect.

Experiment Result

When KeycapButton is pressed, the keyboard triggers a Backspace.

Modifying the Program

Customize key bindings

You can use auto-completion to select different key bindings. VSCode is awesome!

Combination keys, such as Ctrl+V.

More Tutorials

This tutorial aims to help you get started with DeviceScript quickly.

For more learning resources, you can refer to:

DeviceScript | DeviceScript (microsoft.github.io)

For TypeScript syntax documentation related to Jacdac, you can refer to:

Jacdac TypeScript - v1.33.1 (microsoft.github.io)