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:
-
Isochronous and interrupt transfers are currently not supported. This will probably change in a future release.
-
Hotplug support is not implemented. Waiting on hotplug support in nusb.
-
When compiling this crate on a WASM target, you must use either
RUSTFLAGS=--cfg=web_sys_unstable_apis
or by passing the argument in a.cargo/config.toml
file. Read more here: https://rustwasm.github.io/wasm-bindgen/web-sys/unstable-apis.html
§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. - Device
Filter - Information about a USB device for use in
get_device
orget_device_list
. - Device
Info - 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
DeviceInfo
s from a list of VendorID and ProductIDs