Crate cross_usb

source ·
Expand description

Cross USB is a USB library which works seamlessly across native and WASM targets.

The idea is the user only has to write one way to access USB devices, which can be compiled to both WASM and native targets without any additional conditional compilation or configuration.

For native device support, this library uses nusb, a cross platform USB library written in Rust and comparable to the very popular libusb C library. Web Assembly support is provided by web-sys with the Web USB API.

When an Interface is dropped, it is automatically released.

§CURRENT LIMITATIONS:

  • Hotplug support is not implemented. Waiting on hotplug support in nusb.

  • Until this pull request is merged into wasm bindgen, getting a list of USB devices is not possible on WASM targets. However, this isn’t a huge deal as the user gets a list to select from anyway.

§Example:

use cross_usb::prelude::*;
use cross_usb::usb::{Recipient, ControlType, ControlIn};
use cross_usb::device_filter;

// Obtain a device descriptor using a DeviceFilter,
// in this case with its VendorID and ProductID
let filters = vec![
    device_filter!{vendor_id: 0x054c, product_id: 0x00c9}
];
let dev_descriptor = cross_usb::get_device(filters).await.expect("Failed to find device");

// Open the device that the descriptor is describing
let dev = dev_descriptor.open().await.expect("Failed to open device");

// Obtain an interface of the device
let interface = dev.open_interface(0).await.expect("Failed to open interface");

// Send a Control transfer to the device, obtaining
// the result and storing it in `result`
let result = interface.control_in(ControlIn {
        control_type: ControlType::Vendor,
        recipient: Recipient::Interface,
        request: 0x01,
        value: 0,
        index: 0,
        length: 4,
    })
    .await
    .expect("Sending control transfer failed");

Modules§

  • This prelude imports all the necessary traits needed to actually use USB devices and interfaces.
  • This module contains the traits and associated functions and structs which allow for USB communication.

Macros§

Structs§

Functions§