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 a UsbInterface is dropped, it is automatically released.

§Example:

use cross_usb::usb::{Device, Interface, Recipient, ControlType, ControlIn};
use cross_usb::device_filter;

// Obtain a device using its VendorID and ProductID
let filter = vec![
    device_filter!{vendor_id: 0x054c, product_id: 0x00c9}
];

let device = cross_usb::get_device(filter).await.expect("Failed to get device");

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

// Send a Control transfer to the device, obtaining
// the result and storing it in `result`, and you're done!
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 module contains the traits and associated functions and structs which allow for USB communication.

Macros§

Structs§

Functions§

  • Gets a single device from a list of VendorID and ProductIDs