The Javascript DUE library allows for the use of full standard Javascript to access physical-computing.
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. No changes are needed there but we are using Microsoft Visual Studio Code as a personal preference.
Just clone this repo, and open duelink.html with your fav browser for the sample app using DUE JS Library
If this is the first time you use your device, start by visiting the Hardware page and load your device with the appropriate firmware. The Console is also a great place to start.
Start a new project with a simple line of code to test out the project is running
print("Hello DUE!")
We now need to copy the DUE Javascript library JS LIB, check duelink.js, it contains driver for DUE. The library is also available on the downloads page if needed.
The DUE Javascript library requires serial port access.
Our first program will blink the on-board LED 20 times, where it comes on for 200ms and then it is off for 800ms.
Init device and choose com port with this method
this.comPort = new SerialInterface();
comPort.Connect();
init the driver and blink an led.
if (!this.dev)
this.dev = new DUELinkController(this.comPort);
# Flash the LED 20 times (on for 200ms and off for 800ms)
dev.Led.Set(200,800,20);
console.log("Bye DUE!");
The provided API mirrors DUE Script's Core library. Referencing those APIs is a good place to learn about the available functionality and available arguments.
Javascript API
DUE Script Equivalent
Description
Analog.Read()
Reads analog pin
Analog.Write()
Reads analog pin
Button.Enable()
Sets up a button to be used
Button.WasPressed()
Detects if button is was pressed
Button.IsReleased()
Detects if button is released
DeviceConfig.IsEdge()
Checks for specific hardware
DeviceConfig.IsFlea()
Checks for specific hardware
DeviceConfig.IsPico()
Checks for specific hardware
DeviceConfig.IsPulse()
Checks for specific hardware
DeviceConfig.MaxPinIO()
NA
Returns # available GPIOs
DeviceConfig.MaxPinAnalog()
NA
Returns # available Analog pins
Digital.Read()
Reads digital pin
Digital.Write()
Writes to digital pin
Display.Clear()
Clears the display black or white
Display.CreateImage()
Set display configuration
Display.Configuration()
Set display configuration
Display.DrawBuffer()
Updates the entire display, using stream, with automatic Show()
Display.DrawBufferBytes()
Updates the entire display, using stream, with automatic Show()
Display.DrawCircle()
Draws a circle on the display
Display.DrawFillRect()
Draws a filled rectangle on the display
Display.DrawImage()
Draws an image using an array
Display.DrawImageBytes()
Draws an image using bytes
Display.DrawImageScale()
Works same as DrawImage() adds scaling
Display.DrawLine()
Draws a line on the display
Display.DrawRectangle()
Draws a rectangle on the display
Display.DrawText()
Draws a text on the display
Display.DrawTextScale()
Draws scaled text on the display
Display.SetPixel()
Draws pixel on the display
Display.Show()
Sends the display buffer
Distance.Read()
Used to read distance sensors
Frequency.Write()
Hardware generated PWM signal
I2c.Write()
I2C write, using stream
I2c.Read()
I2C read, using stream
I2c.WriteRead()
I2C write/read, using stream
Infrared.Enable()
Enables pin for IR signal capture
Infrared.Read()
Reads value from IR enabled pin
Led.Set()
Controls the on-board LED
Neo.Clear()
Clears all LED's in memory
Neo.SetColor()
Set's a specific LED to a color
Neo.Show()
Transfers the internal pattern to LEDs
Neo.SetMultiple()
Updates all LEDs, using streams, with automatic Show()
Led.Set()
Controls the on-board LED
ServoMoto.Set()
Sets servo motor connected to a pin
Sound.Play()
Sets buzzer sound on supported hardware
Sound.Stop()
Stops buzzer sound on supported hardware
Spi.Configuration()
Configures SPI bus
Spi.Palette()
Sets the desired color for a palette
Spi.Read()
Reads SPI byte
Spi.Write()
Sends SPI byte
Spi.Write4bpp()
Streams and converts data from 4BPP to 16BPP
Spi.WriteRead()
Write & Read SPI bytes
System.Beep()
Uses any pin to generate a tone
System.GetTickMicroseconds()
Returns system time in microseconds
System.GetTickMilliseconds()
Returns system time in milliseconds
System.Reset()
Resets the board
Touch.Read()
Initialize a pin for touch
Uart.BytesToRead()
How many bytes buffered and ready to read
Uart.Enable()
Initialize UART
Uart.Read()
Read UART data
Uart.Write()
Write UART data
Version()
Returns the current DUE firmware version
For convenience, the Pin Enum includes, ButtonA, ButtonB and Led. For example: dev.Digital.Write(dev.Pin.Led, True)
These methods allow developers to control DUE Scripts right from within JS
Method
Description
Script.Execute()
Executes the single line of code immediately
Script.IsRunning()
Checks if DUE Script is running
Script.Load()
Loads the line into internal buffer
Script.New()
Clears the program stored in flash
Script.Read()
Read the program stored in flash and return as string
Script.Record()
Sends the internal buffer to the device, overwriting any previous programs
Script.Run()
Runs the program stored in flash
This example will load a simple program line by line and then record it.
dev.Script.Load("c = 10")
dev.Script.Load("@Blink");
dev.Script.Load("Led(100,100,c)")
dev.Script.Record()
This is an example to execute a single line(immediate mode). This does not modify the application stored in flash.
dev.Script.Execute("LED(200,200,10)")
You can also access a previously recorder program using goto (to label) or by calling a function that has a return. This example calls the recorded program above.
dev.Script.Execute("c=5:goto Blink")