Skip to content

Latest commit

 

History

History
141 lines (111 loc) · 4.45 KB

File metadata and controls

141 lines (111 loc) · 4.45 KB

API documentation

Controller

The controller is creating a connection to the USB device (Arduino) to send data over WebUSB. By using the default args you will only see the following Arduino in the user prompt:

  • Arduino Leonardo
  • Arduino Leonardo ETH
  • Seeeduino Lite
Param Type Description
args Object Arguments to configure the controller
args.filters Array.<Object> List of devices that are whitelisted when opening the user prompt to select an Arduino
args.device Object The selected Arduino to use as the controller
args.leds Array.<number> Holds all the values for each led of the leds

Example

import Controller from 'webusb-neopixel-controller/controller.js'

// Create a new controller using the default properties
const controller = new Controller()

controller.enable() ⇒ Promise

Enable WebUSB and save the selected Arduino into controller.device

Note: This function has to be triggered by a user gesture

Example

controller.enable().then(() => {
  // Create a connection to the selected Arduino
  controller.connect().then(() => {
    // Successfully created a connection
  })
})
.catch(error => {
  // No Arduino was selected by the user
})

controller.getPairedDevice() ⇒ Promise

Get a USB device that was already paired with the browser.

controller.autoConnect() ⇒ Promise

Automatically connect to a USB device that was already paired with the Browser and save it into controller.device

Example

controller.autoConnect()
  .then(() => {
    // Connected to already paired Arduino
  })
  .catch(error => {
    // Nothing found or found paired Arduino, but it's not connected to computer
  })

controller.connect() ⇒ Promise

Open a connection to the selected USB device and tell the device that we are ready to send data to it.

Example

controller.connect().then(() => {
  // Successfully created a connection to the selected Arduino
})

controller.readLoop() ⇒ Promise

Read data from the NeoPixel Controller

controller.send(data) ⇒ Promise

Send data to the USB device to update the leds

Param Type Description
data Array List containing all leds that should be updated

Example

controller.send([255, 0, 0])

controller.update(led, value)

Update the led(s) of the leds with the provided value

Param Type Description
led number The led to update
value number | Array.<number> The value to update the led, supporting two different modes: single (= number) & multi (= Array)

Example (Update a single led)

// Update led #1 with color red (rgb(255, 0, 0))
controller.update(1, [255, 0, 0])

Example (Update multiple leds starting with led)

// Update led #5 with red (rgb(255, 0, 0)), #6 with green (rgb(0, 255, 0))
controller.update(5, [255, 0, 0, 0, 255, 0])

controller.disconnect() ⇒ Promise

Disconnect from the USB device

Note: The device is still paired to the browser!

Example

controller.disconnect().then(() => {
  // Destroyed connection to USB device, but USB device is still paired with the browser
})