* Make blocking APIs optionally async `DeviceInfo::open`, `Device::from_fd`, `Device::set_configuration`, `Device::reset`, `Interface::set_alt_setting`, `Interface::clear_halt` all perform IO but are currently blocking because the underlying OS APIs are blocking. `list_devices`,`list_buses`, `Device::claim_interface` `Device::detach_and_claim_interface` theoretically don't perform IO, but are also included here because they need to be async on WebUSB. The `MaybeFuture` trait allows deferring these actions to the thread pool from the `blocking` crate when used asynchronously with `.await` / `IntoFuture`, or directly runs the blocking syscall synchronously with a `.wait()` method.
33 lines
885 B
Rust
33 lines
885 B
Rust
use futures_lite::future::block_on;
|
|
use nusb::{transfer::RequestBuffer, MaybeFuture};
|
|
|
|
fn main() {
|
|
env_logger::init();
|
|
let di = nusb::list_devices()
|
|
.wait()
|
|
.unwrap()
|
|
.find(|d| d.vendor_id() == 0x59e3 && d.product_id() == 0x0a23)
|
|
.expect("device should be connected");
|
|
|
|
println!("Device info: {di:?}");
|
|
|
|
let device = di.open().wait().unwrap();
|
|
let interface = device.claim_interface(0).wait().unwrap();
|
|
|
|
block_on(interface.bulk_out(0x02, Vec::from([1, 2, 3, 4, 5])))
|
|
.into_result()
|
|
.unwrap();
|
|
|
|
let mut queue = interface.bulk_in_queue(0x81);
|
|
|
|
loop {
|
|
while queue.pending() < 8 {
|
|
queue.submit(RequestBuffer::new(256));
|
|
}
|
|
let result = block_on(queue.next_complete());
|
|
println!("{result:?}");
|
|
if result.status.is_err() {
|
|
break;
|
|
}
|
|
}
|
|
}
|