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.

§CURRENT LIMITATIONS:

§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§

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

Macros§

device_filter
Macro to create a device filter more easily.

Structs§

Device
A USB device, you must open an Interface to perform transfers.
DeviceFilter
Information about a USB device for use in get_device or get_device_list.
DeviceInfo
Information about a USB device, containing information about a device without claiming it
Interface
A USB interface to perform transfers with.

Functions§

get_device
Gets a single (the first found) device as a DeviceInfo from a list of VendorID and ProductIDs
get_device_list
Gets a list of DeviceInfos from a list of VendorID and ProductIDs