Skip to content

Gravicode/due-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Javascript


Javascript

The Javascript DUE library allows for the use of full standard Javascript to access physical-computing.

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. No changes are needed there but we are using Microsoft Visual Studio Code as a personal preference.

Hello World

Just clone this repo, and open duelink.html with your fav browser for the sample app using DUE JS Library

Tip

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.

Tip

The DUE Javascript library requires serial port access.

Blinky!

Our first program will blink the on-board LED 20 times, where it comes on for 200ms and then it is off for 800ms.

Note

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!");

Javascript API

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()

ARead()

Reads analog pin

Analog.Write()

AWrite()

Reads analog pin

Button.Enable()

BtnEnable()

Sets up a button to be used

Button.WasPressed()

BtnDown()

Detects if button is was pressed

Button.IsReleased()

BtnDown()

Detects if button is released

DeviceConfig.IsEdge()

Version()

Checks for specific hardware

DeviceConfig.IsFlea()

Version()

Checks for specific hardware

DeviceConfig.IsPico()

Version()

Checks for specific hardware

DeviceConfig.IsPulse()

Version()

Checks for specific hardware

DeviceConfig.MaxPinIO()

NA

Returns # available GPIOs

DeviceConfig.MaxPinAnalog()

NA

Returns # available Analog pins

Digital.Read()

DRead()

Reads digital pin

Digital.Write()

DWrite()

Writes to digital pin

Display.Clear()

LcdClear()

Clears the display black or white

Display.CreateImage()

LcdConfig()

Set display configuration

Display.Configuration()

LcdConfig()

Set display configuration

Display.DrawBuffer()

LcdStream()

Updates the entire display, using stream, with automatic Show()

Display.DrawBufferBytes()

LcdStream()

Updates the entire display, using stream, with automatic Show()

Display.DrawCircle()

LcdCircle()

Draws a circle on the display

Display.DrawFillRect()

LcdFill()

Draws a filled rectangle on the display

Display.DrawImage()

LcdImg()

Draws an image using an array

Display.DrawImageBytes()

LcdImg()

Draws an image using bytes

Display.DrawImageScale()

LcdImg()

Works same as DrawImage() adds scaling

Display.DrawLine()

LcdLine()

Draws a line on the display

Display.DrawRectangle()

LcdRect()

Draws a rectangle on the display

Display.DrawText()

LcdText()

Draws a text on the display

Display.DrawTextScale()

LcdTextS()

Draws scaled text on the display

Display.SetPixel()

LcdPixel()

Draws pixel on the display

Display.Show()

LcdShow()

Sends the display buffer

Distance.Read()

Distance()

Used to read distance sensors

Frequency.Write()

Freq()

Hardware generated PWM signal

I2c.Write()

I2cStream()

I2C write, using stream

I2c.Read()

I2cStream()

I2C read, using stream

I2c.WriteRead()

I2cStream()

I2C write/read, using stream

Infrared.Enable()

IrEnable()

Enables pin for IR signal capture

Infrared.Read()

IrRead()

Reads value from IR enabled pin

Led.Set()

LED()

Controls the on-board LED

Neo.Clear()

NeoClear()

Clears all LED's in memory

Neo.SetColor()

NeoSet()

Set's a specific LED to a color

Neo.Show()

NeoShow()

Transfers the internal pattern to LEDs

Neo.SetMultiple()

NeoStream()

Updates all LEDs, using streams, with automatic Show()

Led.Set()

LED()

Controls the on-board LED

ServoMoto.Set()

ServoSet()

Sets servo motor connected to a pin

Sound.Play()

Sound()

Sets buzzer sound on supported hardware

Sound.Stop()

Sound()

Stops buzzer sound on supported hardware

Spi.Configuration()

SpiCfg()

Configures SPI bus

Spi.Palette()

Palette()

Sets the desired color for a palette

Spi.Read()

SpiByte()

Reads SPI byte

Spi.Write()

SpiByte()

Sends SPI byte

Spi.Write4bpp()

Spi4Bpp()

Streams and converts data from 4BPP to 16BPP

Spi.WriteRead()

SpiStream()

Write & Read SPI bytes

System.Beep()

Beep()

Uses any pin to generate a tone

System.GetTickMicroseconds()

TickUs()

Returns system time in microseconds

System.GetTickMilliseconds()

TickMs()

Returns system time in milliseconds

System.Reset()

Reset()

Resets the board

Touch.Read()

TouchRead()

Initialize a pin for touch

Uart.BytesToRead()

UartCount()

How many bytes buffered and ready to read

Uart.Enable()

UartInit()

Initialize UART

Uart.Read()

UartRead()

Read UART data

Uart.Write()

UartWrite()

Write UART data

Version()

Version()

Returns the current DUE firmware version

Note

For convenience, the Pin Enum includes, ButtonA, ButtonB and Led. For example: dev.Digital.Write(dev.Pin.Led, True)

DUE Script Control

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")

About

js driver for duelink

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors