Skip to content

Latest commit

 

History

History
154 lines (121 loc) · 10.5 KB

javascript.md

File metadata and controls

154 lines (121 loc) · 10.5 KB

JavaScript


JavaSript

JavaScript is one of the core technologies used by the World Wide Web. 98% of all websites use JavaScript.

DUE integrates beautifully with JS, allowing web developers access to hardware devices.

This website example shows a good example JavaScript DUE extensibility. demo.duelink.com

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. We'll be running our program on a local machine using NodeJS. No changes are needed there but we are using Microsoft Visual Studio Code as a personal preference.

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.

console.log("Hello World");

We are now ready to bring in the DUE JavaScript library, npm install DUELink. The library is not NPM hosted yet and can be downloaded from here https://github.com/ghi-electronics/due-libraries/tree/main/javascript save the duelink.js, util.js and serialusb.js, add to your program's local folder. The library also needs SerialPort https://serialport.io/ download and install npm install serialport.

We can now instantiate and get the DUELink controller ready. We will be using SerialUSB() here. If using a web browser, WebSerial() will be used instead.

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 the method in the code sample below

import {SerialUSB} from './serialusb.js';
import * as due from './duelink.js';

let BrainPad = new due.DUELinkController(new SerialUSB());
await BrainPad.Connect();

// Flash the LED 20 times (on for 200ms and off for 800ms)
await BrainPad.Led.Set(200,800,10);

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.

| Python 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.JustPressed() |BtnDown() | Detects if button is was pressed | Button.JustReleased() |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()](../due-script/c led.md) | 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 | 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.Print() |Print() | Print to LCD display and Debug Output on the same line | System.PrintLn() |PrintLn() | Print to LCD display then moves to next line | System.Reset() |Reset() | Resets the board | System.Wait() |Wait() | Pause the system in milliseconds | 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 JavaScript.

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