Skip to main content

Drivers

In DeviceScript, any hardware component is accessed through a service client. A driver implement one or more service servers for a given hardware peripheral.

The drivers builtin package exposes drivers for a number of peripherals.

Analog (builtin)

DeviceScript provides helper functions to mount a variety of servers out of the box in the @devicescript/servers module.

For example, the startButton function allows your script to mount a button server over a pin and returns the client for it.

import { gpio } from "@devicescript/core"
import { startButton } from "@devicescript/servers"

const buttonA = startButton({
pin: gpio(2),
})
buttonA.up.subscribe(() => {
console.log("up!")
})

Once you've added this code to your script, you can interact with pin 2 (hardware specific identifier) through the buttonA client. The variable name is automatically used as the role and instance name.

Digital IO

Drivers can be implemented using digital IO and are either built-in from C or authored in DeviceScript directly.

import { gpio, GPIOMode } from "@devicescript/core"
import "@devicescript/gpio"

const p0 = gpio(0)
await p0.setMode(GPIOMode.Output)
await p0.write(true)

I2C

Drivers can be implemented using I2C and are either built-in from C or authored in DeviceScript directly.

import { i2c } from "@devicescript/i2c"

...
await i2c.writeReg(address, register, value)

SPI

Drivers can be implemented using SPI and are either built-in from C or authored in DeviceScript directly.

import { spi } from "@devicescript/spi"

...
await spi.write(hex`abcd`)

What about missing drivers?

So the driver you need isn't here? There are a few options:

Jacdac modules

If you connect a Jacdac module, it will automatically run as a service server and you will be able to bind a role to that module.

For example, the KittenBot KeyCap button will surface as a button server when connected.

A KittenBot KeyCap button

Serial, PWM

Serial, PWM are not supported yet at the DeviceScript level, however specific PWM uses (such as servos, buzzers, light bulbs, motors) are supported through @devicescript/servers.