Skip to content

About

A library for accessing Particle USB devices

Resources

Stars

5 stars

Watchers

33 watching

Forks

Repository files navigation

particle-usb

CICD

A library for accessing Particle USB devices.

Note: This library requires Particle firmware 0.8.0 or later.

Installation | Usage | API Reference | Development | Testing | Releasing | License

Installation

Using npm:

$ npm install particle-usb @particle/device-constants

NOTE: particle-usb declares @particle/device-constants as a peerDependency - this ensures your app only ever has one copy of that dependency

Usage

In Node.js:

import * as usb from 'particle-usb';

Enumerating devices

const devices = await usb.getDevices();
for (let device of devices) {
  console.log(device.type); // Prints device type, e.g. "Photon"
}

Opening devices

Most of the device methods, such as reset(), require the device to be open:

const devices = await usb.getDevices();
if (devices.length == 0) {
  throw new Error('No devices found');
}
const device = devices[0];
await device.open();
await device.reset(); // Resets the device

It is possible to open a device by ID:

const device = await usb.openDeviceById('0123456789abcdef01234567');
await device.reset();

In the browser

Browsers only expose devices the user has explicitly granted access to, which the browser's device picker asks for.

getDevices() returns the devices already granted, and shows the picker so the user can add one. The devices come back unopened:

button.addEventListener('click', async () => {
  const devices = await usb.getDevices();
  if (devices.length === 0) {
    throw new Error('No devices found');
  }
  const device = devices[0];
  await device.open();
});

openDeviceById() never shows the picker. It only looks at the devices already granted, and returns the device already open, or throws a NotFoundError when that device has not been granted access or is not attached. Because it never prompts, it does not need a user gesture:

const device = await usb.openDeviceById('0123456789abcdef01234567');
await device.reset();

requestDevice() always shows the picker and resolves to the device the user selected, unopened:

button.addEventListener('click', async () => {
  const device = await usb.requestDevice(); // Throws a NotFoundError if the user cancels
  await device.open();
});

Pass an id to scope the picker to a single device. The picker lists that device in whichever mode it is currently in, and updates live as it enumerates, which is useful to grant access to a device that is about to enter DFU mode (the browser keeps a separate grant for the DFU mode of a device):

button.addEventListener('click', async () => {
  const device = await usb.requestDevice({ id: '0123456789abcdef01234567' });
  await device.open();
});

getDevices() and requestDevice() show the picker, so they must be called from a user gesture, such as a click handler. Outside of one the browser refuses to show the picker.

The device should be closed when it is no longer needed:

await device.close();

API reference

For more information, read the API reference on GitHub.

Development

Installing

  1. Install Node.js [[email protected] and [email protected] are required]
  2. Clone this repository $ git clone [email protected]:particle-iot/particle-usb.git && cd ./particle-usb
  3. Install dependencies $ npm install
  4. View available commands $ npm run
  5. Run the tests $ npm test
  6. Start Hacking!

Testing

Particle USB has a number of automated test suites and related commands. The most important are:

  • npm test - run all tests
  • npm run test:e2e - run all end-to-end tests NOTE: Requires additional setup
  • npm run test:ci - run all tests excluding device-dependent end-to-end tests as CI does
  • npm run lint - run the linter and print any errors to your terminal
  • npm run coverage - report code coverage stats

All tests use mocha, chai, and sinon with coverage handled by nyc.

We recommend running locally if you can as it greatly shortens your feedback loop. However, CI also runs against every PR and error reporting is publicly available.

Releasing

For release instructions, see RELEASE.md

License

This library is released under the Apache 2.0 license. See LICENSE for details.

About

A library for accessing Particle USB devices

Resources

Stars

5 stars

Watchers

33 watching

Forks

Releases

Packages

Used by

Contributors

Languages