7 releases (4 breaking)

0.5.0 Jul 24, 2022
0.4.0 Feb 18, 2022
0.3.0 Sep 26, 2021
0.2.0 Sep 24, 2021
0.1.2 Sep 8, 2021

#164 in Hardware support

MIT license

72KB
2K SLoC

Rust 1.5K SLoC // 0.1% comments TypeScript 486 SLoC // 0.1% comments Shell 3 SLoC // 0.4% comments

webusb

Implementation of the WebUSB specification in Rust.

Documentation Package Coverage Status

[dependencies]
webusb = "0.5.0"

ko-fi

Usage with Deno

import "https://deno.land/x/webusb/mod.ts";

const devices = await navigator.usb.getDevices();
// Arduino Leonardo
let device = devices.find((p) => p.productId == 0x8036);

await device.open();
console.log("Device opened.");

if (device.configuration === null) {
  device.selectConfiguration(1);
}

console.log(`${device.productName} - ${device.serialNumber}`);

await device.claimInterface(2);
await device.selectAlternateInterface(2, 0);
await device.controlTransferOut({
  "requestType": "class",
  "recipient": "interface",
  "request": 0x22,
  "value": 0x01,
  "index": 2,
});

while (true) {
  const action = prompt(">>");
  if (action.toLowerCase() == "exit") break;
  const data = new TextEncoder().encode(action);
  await device.transferOut(4, data);
  console.info("Transfer.");
}

await device.controlTransferOut({
  "requestType": "class",
  "recipient": "interface",
  "request": 0x22,
  "value": 0x00,
  "index": 2,
});

await device.close();
console.log("Bye.");

Testing

Hardware tests are run before merging a PR and then on main. The test runner is a self-hosted Linux x86_64 machine, it is connected to an Arduino Leonardo (ATmega32u4) via micro USB connection.

Tests are reviewed and triggered by maintainers on PRs to prevent malicious execution. Load this sketch into yours to run the tests locally.

When writing tests you might encounter frequent Io / NoDevice errors, this can be due to loose wired connection. Mark these tests as #[flaky_test::flaky_test].

License

MIT License

Dependencies

~0–610KB
~13K SLoC