mirror of
https://github.com/G2-Games/cross-usb.git
synced 2025-04-19 21:32:53 -05:00
Ran cargo fmt
This commit is contained in:
parent
f910a93468
commit
e2b970d817
3 changed files with 44 additions and 36 deletions
|
@ -1,5 +1,5 @@
|
||||||
use std::error::Error;
|
|
||||||
use nusb;
|
use nusb;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
use crate::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
|
use crate::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ pub async fn get_device(vendor_id: u16, product_id: u16) -> Result<UsbDevice, Bo
|
||||||
|
|
||||||
Ok(UsbDevice {
|
Ok(UsbDevice {
|
||||||
device_info,
|
device_info,
|
||||||
device
|
device,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ impl Device for UsbDevice {
|
||||||
async fn reset(&self) -> Result<(), Box<dyn Error>> {
|
async fn reset(&self) -> Result<(), Box<dyn Error>> {
|
||||||
match self.device.reset() {
|
match self.device.reset() {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(e) => Err(e.into())
|
Err(e) => Err(e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,20 +89,29 @@ impl<'a> Interface<'a> for UsbInterface {
|
||||||
async fn control_out(&self, data: ControlOut<'a>) -> Result<(), Box<dyn Error>> {
|
async fn control_out(&self, data: ControlOut<'a>) -> Result<(), Box<dyn Error>> {
|
||||||
match self.interface.control_out(data.into()).await.into_result() {
|
match self.interface.control_out(data.into()).await.into_result() {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(e) => Err(e.into())
|
Err(e) => Err(e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn bulk_in(&self, endpoint: u8, length: usize) -> Result<Vec<u8>, Box<dyn Error>> {
|
async fn bulk_in(&self, endpoint: u8, length: usize) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||||
let request_buffer = nusb::transfer::RequestBuffer::new(length);
|
let request_buffer = nusb::transfer::RequestBuffer::new(length);
|
||||||
|
|
||||||
Ok(self.interface.bulk_in(endpoint, request_buffer).await.into_result()?)
|
Ok(self
|
||||||
|
.interface
|
||||||
|
.bulk_in(endpoint, request_buffer)
|
||||||
|
.await
|
||||||
|
.into_result()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn bulk_out(&self, endpoint: u8, data: &[u8]) -> Result<usize, Box<dyn Error>> {
|
async fn bulk_out(&self, endpoint: u8, data: &[u8]) -> Result<usize, Box<dyn Error>> {
|
||||||
match self.interface.bulk_out(endpoint, data.to_vec()).await.into_result() {
|
match self
|
||||||
|
.interface
|
||||||
|
.bulk_out(endpoint, data.to_vec())
|
||||||
|
.await
|
||||||
|
.into_result()
|
||||||
|
{
|
||||||
Ok(len) => Ok(len.actual_length()),
|
Ok(len) => Ok(len.actual_length()),
|
||||||
Err(e) => Err(e.into())
|
Err(e) => Err(e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,7 +155,6 @@ impl From<ControlType> for nusb::transfer::ControlType {
|
||||||
impl From<Recipient> for nusb::transfer::Recipient {
|
impl From<Recipient> for nusb::transfer::Recipient {
|
||||||
fn from(val: Recipient) -> Self {
|
fn from(val: Recipient) -> Self {
|
||||||
match val {
|
match val {
|
||||||
|
|
||||||
Recipient::Device => nusb::transfer::Recipient::Device,
|
Recipient::Device => nusb::transfer::Recipient::Device,
|
||||||
Recipient::Interface => nusb::transfer::Recipient::Interface,
|
Recipient::Interface => nusb::transfer::Recipient::Interface,
|
||||||
Recipient::Endpoint => nusb::transfer::Recipient::Endpoint,
|
Recipient::Endpoint => nusb::transfer::Recipient::Endpoint,
|
||||||
|
|
|
@ -2,20 +2,13 @@
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
use web_sys::{
|
use js_sys::{Array, Object, Promise, Uint8Array};
|
||||||
console,
|
|
||||||
Usb,
|
|
||||||
UsbDevice as WasmUsbDevice,
|
|
||||||
UsbInterface as WasmUsbInterface,
|
|
||||||
UsbControlTransferParameters,
|
|
||||||
UsbInTransferResult,
|
|
||||||
UsbOutTransferResult,
|
|
||||||
UsbRecipient,
|
|
||||||
UsbRequestType,
|
|
||||||
UsbDeviceRequestOptions,
|
|
||||||
};
|
|
||||||
use js_sys::{Array, Uint8Array, Promise, Object};
|
|
||||||
use wasm_bindgen_futures::JsFuture;
|
use wasm_bindgen_futures::JsFuture;
|
||||||
|
use web_sys::{
|
||||||
|
console, Usb, UsbControlTransferParameters, UsbDevice as WasmUsbDevice,
|
||||||
|
UsbDeviceRequestOptions, UsbInTransferResult, UsbInterface as WasmUsbInterface,
|
||||||
|
UsbOutTransferResult, UsbRecipient, UsbRequestType,
|
||||||
|
};
|
||||||
|
|
||||||
// Crate stuff
|
// Crate stuff
|
||||||
use crate::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
|
use crate::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
|
||||||
|
@ -62,15 +55,13 @@ pub async fn get_device(vendor_id: u16, product_id: u16) -> Result<UsbDevice, js
|
||||||
Ok(dev) => dev.into(),
|
Ok(dev) => dev.into(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
console::log_1(&err.clone());
|
console::log_1(&err.clone());
|
||||||
return Err(err.into())
|
return Err(err.into());
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?;
|
let _open_promise = JsFuture::from(Promise::resolve(&device.open())).await?;
|
||||||
|
|
||||||
Ok(UsbDevice {
|
Ok(UsbDevice { device })
|
||||||
device
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device for UsbDevice {
|
impl Device for UsbDevice {
|
||||||
|
@ -78,19 +69,20 @@ impl Device for UsbDevice {
|
||||||
type UsbInterface = UsbInterface;
|
type UsbInterface = UsbInterface;
|
||||||
|
|
||||||
async fn open_interface(&self, number: u8) -> Result<UsbInterface, Box<dyn Error>> {
|
async fn open_interface(&self, number: u8) -> Result<UsbInterface, Box<dyn Error>> {
|
||||||
let dev_promise = JsFuture::from(Promise::resolve(&self.device.claim_interface(number))).await;
|
let dev_promise =
|
||||||
|
JsFuture::from(Promise::resolve(&self.device.claim_interface(number))).await;
|
||||||
|
|
||||||
// Wait for the interface to be claimed
|
// Wait for the interface to be claimed
|
||||||
let _device: WasmUsbDevice = match dev_promise {
|
let _device: WasmUsbDevice = match dev_promise {
|
||||||
Ok(dev) => dev.into(),
|
Ok(dev) => dev.into(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
console::log_1(&err.clone());
|
console::log_1(&err.clone());
|
||||||
return Err(format!("{:?}", err).into())
|
return Err(format!("{:?}", err).into());
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(UsbInterface {
|
Ok(UsbInterface {
|
||||||
device: self.device.clone()
|
device: self.device.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +150,11 @@ impl<'a> Interface<'a> for UsbInterface {
|
||||||
let array_obj = Object::try_from(&array).unwrap();
|
let array_obj = Object::try_from(&array).unwrap();
|
||||||
let params: UsbControlTransferParameters = data.into();
|
let params: UsbControlTransferParameters = data.into();
|
||||||
|
|
||||||
let promise = Promise::resolve(&self.device.control_transfer_out_with_buffer_source(¶ms, array_obj));
|
let promise = Promise::resolve(
|
||||||
|
&self
|
||||||
|
.device
|
||||||
|
.control_transfer_out_with_buffer_source(¶ms, array_obj),
|
||||||
|
);
|
||||||
let result = JsFuture::from(promise).await;
|
let result = JsFuture::from(promise).await;
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
|
@ -191,7 +187,11 @@ impl<'a> Interface<'a> for UsbInterface {
|
||||||
let array = Uint8Array::from(data);
|
let array = Uint8Array::from(data);
|
||||||
let array_obj = Object::try_from(&array).unwrap();
|
let array_obj = Object::try_from(&array).unwrap();
|
||||||
|
|
||||||
let promise = Promise::resolve(&self.device.transfer_out_with_buffer_source(endpoint, array_obj));
|
let promise = Promise::resolve(
|
||||||
|
&self
|
||||||
|
.device
|
||||||
|
.transfer_out_with_buffer_source(endpoint, array_obj),
|
||||||
|
);
|
||||||
|
|
||||||
let result = JsFuture::from(promise).await;
|
let result = JsFuture::from(promise).await;
|
||||||
|
|
||||||
|
@ -211,7 +211,7 @@ impl From<ControlIn> for UsbControlTransferParameters {
|
||||||
value.recipient.into(),
|
value.recipient.into(),
|
||||||
value.request,
|
value.request,
|
||||||
value.control_type.into(),
|
value.control_type.into(),
|
||||||
value.value
|
value.value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,7 @@ impl From<ControlOut<'_>> for UsbControlTransferParameters {
|
||||||
value.recipient.into(),
|
value.recipient.into(),
|
||||||
value.request,
|
value.request,
|
||||||
value.control_type.into(),
|
value.control_type.into(),
|
||||||
value.value
|
value.value,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#![cfg_attr(debug_assertions, allow(async_fn_in_trait))]
|
#![cfg_attr(debug_assertions, allow(async_fn_in_trait))]
|
||||||
use std::error::Error;
|
|
||||||
use crate::context::UsbInterface;
|
use crate::context::UsbInterface;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
/// A unique USB device
|
/// A unique USB device
|
||||||
pub trait Device {
|
pub trait Device {
|
||||||
|
|
Loading…
Reference in a new issue