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.

Use this demo website to see how DUELink modules work with JavaScript inside a website. This page shows how DUELink modules work with JavaScript on a local machine.
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
For the module, visit loader.duelink.com to make sure your hardware has the latest DUElink Official firmware and related module drivers.
We assume that you are already familiar with JavaScript and your development machine is already setup to build and run JavaScript programs using Node.js.
Start a new project with a simple line of code to test that the project is running.
console.log("Hello World");
We now need to install the duelink package using npm install duelink. We also need the serialport dlserialusb package using npm install dlserialusb.
Blinky!
Our first 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();
The JavaScript library only supports serial ports, which can be either UART or USB. The SerialUSB() function scans for a USB-connected DUELink module. The returned value can be used to start DUElinkController(). If a serial port is desired then use the actual port number, for example SerialUSB("COM6"). Using serial is useful with wireless Bluetooth connections.
Daisylinking
DUELink modules can be Daisylinked together. The duelink.Engine.Select() function is used to select the module to be listening to incoming commands.
In the example below, we'll access an OLED Display, a Button, and a Buzzer.

Visit loader.duelink.com to "discover" modules and load the latest firmware and 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();
Code Samples
Individual product pages include specific code samples that utilize the module's Driver Script.
See Tethered Samples under individual module pages.