- Controller
- .enable() ⇒
Promise
- .getPairedDevice() ⇒
Promise
- .autoConnect() ⇒
Promise
- .connect() ⇒
Promise
- .readLoop() ⇒
Promise
- .send(data) ⇒
Promise
- .update(led, value)
- .disconnect() ⇒
Promise
- .enable() ⇒
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()
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
})
Get a USB device that was already paired with the browser.
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
})
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
})
Read data from the NeoPixel Controller
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])
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])
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
})