Skip to main content

JavaScript

JavaScript

JavaScript is one of the core technologies used by the World Wide Web. 98% of all websites use JavaScript. Websites can now use the provided DUELink JavaScript library to access the physical world.

Here is a website demo to demonstrate how JavaScript can control actuators and read sensors: demo.duelink.com. It is covered in the Getting Started page.

tip

Note that JavaScript will be executed on one of the supported hardware options, which is tethered to DUELink modules. JavaScript does not run on the module itself!


Setup

This page assumes the user is already familiar with JavaScript and there is a development machine that is already setup to build and run JavaScript programs. We'll be running our program on a local machine using Node.js.

Install the duelink package:

npm install duelink

If using serialport, dlserialusb package is needed:

npm install dlserialusb
tip

Make sure your hardware is updated with the latest firmware. The Console can help with that!

Start a new project with a simple line of code to test that the project is running.

console.log("Hello World");

Blinky!

Our program will blink the on-board status LED on a tethered module, on for 200ms then it shuts off for 800ms, and does this 20 times. We will be using SerialUSB() here. If running on a website, use WebSerial() instead.

Note that using await is very critical because JavaScript is designed to run tasks asynchronously. DUELink is accesses hardware live, in real-time. Things must run in order and using await is required with anything DUElink!

import pkg_serialusb from 'dlserialusb';
const {SerialUSB} = pkg_serialusb

import pkg_duelink from 'duelink';
const {DUELinkController} = pkg_duelink

let duelink = new DUELinkController(new SerialUSB());
await duelink.Connect();

async function Blinky() {
// Flash the LED (on for 200ms, off for 800ms, 20 times)
await duelink.System.StatLed(200, 800, 20);
}

Blinky();
tip

The Python library is open source: DUELink Libraries Repo.


Daisylinking

In the example below, we'll use Python to control an OLED 0.96 Display, a Button, and a Buzzer module Daisylinked together.

Daisylinked Modules

tip

Make sure these modules are loaded with Drivers.

import pkg_serialusb from 'dlserialusb';
const {SerialUSB} = pkg_serialusb

import pkg_duelink from 'duelink';
const {DUELinkController} = pkg_duelink

let duelink = new DUELinkController(new SerialUSB());
await duelink.Connect();

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function main() {
// Device 1 is Display OLDE 0.96, with driver installed!
// Device 2 is button
// Device 3 is buzzer
duelink.ReadTimeout = 100; // milliseconds

await duelink.Engine.Select(1);
await duelink.Graphics.Clear(1);
await duelink.Graphics.Text("DUELink", 0, 10, 10);
await duelink.Graphics.Text("Press button on", 0, 1, 21);
await duelink.Graphics.Text("device 2 to play", 0, 1, 31);
await duelink.Graphics.Text("sound on device 3", 0, 1, 41);
await duelink.Graphics.Show();

await duelink.Engine.Select(2);
await duelink.Button.Enable(1, 0);

while (true) {
await duelink.Engine.Select(2);
if (await duelink.Button.Down(1)) {
await duelink.Engine.Select(3);
await duelink.Frequency.Write(7, 1000, 50, 0.5);
}
await sleep(100); // Sleep 100 milliseconds
}
}

// Run the main function
main();

The earlier sample were generic and run on any module. The individual module's product pages. include specific samples.