Skip to main content

Command Line

The command line tool is compatible with container and virtual machines so you can run it in Docker, GitHub Codespaces, ...

Setting up the project

Let's get started by installing the DeviceScript command line and create an empty project

  • Open a terminal in a new folder
  • install Node.js 16+ if not already installed
  • Use the init command to setup a new project
npx --yes @devicescript/cli@latest init

You will have the following files created.

.devicescript/     reserved folder for devicescript generated files
package.json project configuration
devsconfig.json configure the DeviceScript compiler with additional flags,
also used by VSCode extension to activate.
src/ directory for DeviceScript sources
src/main.ts usual name for your entry point application
src/tsconfig.json configure the TypeScript compiler to compile DeviceScript syntax
...
  • open src/main.ts and copy the following code
src/main.ts
import * as ds from "@devicescript/core"
import { debounceTime, filter } from "@devicescript/observables"

const sensor = new ds.AirPressure()
const mouse = new ds.HidMouse()
// listen for pressure changes
sensor.reading
.pipe(
filter(pressure => pressure > 1400),
debounceTime(500)
)
.subscribe(async () => {
console.log(`click!`)
await mouse.setButton(
ds.HidMouseButton.Left,
ds.HidMouseButtonEvent.Click
)
})

Launch the build watch

Assuming src/main.ts is the root file of your application, launch a compilation task in watch mode using this command.

devs devtools src/main.ts

or, for short,

yarn watch

The command line task will also start a local web server that will send the compiled bytecode to a developer tools page similar to the one hosted in the docs.

  • open the developer tools page, typically http://localhost:8081/ (see cli output)
  • use te developer tools page similarly to the embedded docs page

Native transports

If you are working on the OS directly connected to the hardware device, you can use start a native transport (serial, usb) from the command line. However, native transport typically require native npm module that might not be compatible with your operating system. The command line tool will detect missing dependencies and prompt you to install them.

devs devtools --serial src/main.ts

Edit, deploy, debug loop

From here, your developer inner loop will be very similar to building/debugging a web site with hot reload.

  • make an edit in your source file, say src/main.ts
  • after a couple seconds, the compiler picks up the changes, produces a new bytecode and sends it to the developer tools
  • the developer tools automatically deploy the bytecode to the selected device (by default the simulator)
  • switch from VS Code to the browser and debug your new code