Giter VIP home page Giter VIP logo

webusb's Introduction

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

webusb's People

Contributors

bors[bot] avatar littledivy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webusb's Issues

Documentation: Context (and example?)

To a newcomer looking at Rust support for WebUSB, the README is lacking information on where in the stack this sits -- could be for use for on the device itself (in embedded Rust), or could run in the browser (eg. through wasm-bindgen).

From the dependencies, I get the impression that it's to be used natively on the host, and peeking at the projects you're involved in it might be the part of WebUSB that runs inside the browser, but that's a guess. If it is right, text like this could be helpful:

This crate implements the browser part of the WebUSB specification.
That is, it accesses the host system's USB devices through libusb, and provides the Navigator .usb object that is typically used through JavaScript.

An example of how this is used can be found [in the deno source code](...).

For different places in the stack where Rust and WebUSB can be used together, see [usbd-webusb](https://github.com/redpfire/usbd-webusb) (which would go into the device firmware itself), or [web-sys's Usb struct](https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Usb.html) (which compiled to WebAssembly and run inside the browser). The present crate provides the code inbetween.

(Note that I haven't used or tested usbd-webusb or this particular module of web-sys. These serve not so much as recommendations but more to frame how this crate is used.)

Building for WASM

I have struggled for a while to get this crate to compile for WASM, but I can't seem to figure it out. It fails when attempting to link libusb.

FFI bindings to Deno

Deno might never implement WebUSB due to its testing requirements. We have a test runner and Deno has FFI so why not build a third-party module? :-)

Device class / typings are not exported

It's impossible to declare TypeScript typings for a function that takes in a USB device as an argument, since it is not exported:

import usb from "https://deno.land/x/[email protected]/mod.ts"

const devices = await usb.getDevices()

initDevice(devices[0])

async function initDevice(device: /* what goes here? */) {
    await device.open()
    console.log(`${device.productName} - ${device.serialNumber}`);
}

Likewise, it's impossible to tell if an object is a USB device via an instanceof check, since the class isn't exported either.

Could you consider exporting the types/classes?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.