-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly switched to
nusb
as the usb backend
- Loading branch information
Showing
3 changed files
with
46 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
[package] | ||
name = "sixaxis_go" | ||
version = "0.1.0" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
futures-lite = "2.2.0" | ||
hidapi = "2.4.1" | ||
futures-lite = "2.3.0" | ||
macaddr = "1.0.1" | ||
nusb = "0.1.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,64 @@ | ||
use std::error::Error; | ||
|
||
use futures_lite::future::block_on; | ||
use nusb::list_devices; | ||
use nusb::{Device, DeviceInfo, Interface, InterfaceInfo}; | ||
use nusb::transfer::{ControlType, ControlIn, Recipient, Control}; | ||
use nusb::{Interface, list_devices, transfer::{ControlIn, ControlType, Recipient, ControlOut}}; | ||
|
||
pub struct HidApi { | ||
pub struct SixaxisApi; | ||
|
||
} | ||
|
||
pub struct HidDevice { | ||
device: Device, | ||
pub struct SixaxisDevice { | ||
interface: Interface, | ||
} | ||
|
||
impl HidApi { | ||
impl SixaxisApi { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
pub fn open(&self, vendor_id: u16, product_id: u16) -> Result<HidDevice, Box<dyn Error>> { | ||
let device; | ||
for dev_info in list_devices()? { | ||
if dev_info.vendor_id() == vendor_id | ||
&& dev_info.product_id() == product_id | ||
{ | ||
let temp_dev = dev_info.open()?; | ||
pub fn open(&self, vendor_id: u16, product_id: u16) -> Result<SixaxisDevice, Box<dyn Error>> { | ||
let device = list_devices() | ||
.unwrap() | ||
.find(|dev| dev.vendor_id() == vendor_id && dev.product_id() == product_id) | ||
.expect("Unable to find SixAxis device!") | ||
.open()?; | ||
|
||
device = HidDevice::new(temp_dev, dev_info)?; | ||
return Ok(device) | ||
} | ||
} | ||
let interface = device.detach_and_claim_interface(0)?; | ||
|
||
Err("Could not find requested device".into()) | ||
Ok(SixaxisDevice { | ||
interface, | ||
}) | ||
} | ||
} | ||
|
||
impl HidDevice { | ||
fn new(device: Device, device_info: DeviceInfo) -> Result<Self, Box<dyn Error>> { | ||
let interface; | ||
for int_info in device_info.interfaces() { | ||
if int_info.class() == 3 { // Ensure it's an HID interface | ||
interface = device.detach_and_claim_interface(0)?; | ||
|
||
return Ok(Self { | ||
device, | ||
interface | ||
}) | ||
} | ||
} | ||
|
||
Err("Could not find valid interface".into()) | ||
} | ||
|
||
impl SixaxisDevice { | ||
pub fn get_feature_report(&self, report_number: u8) -> Result<Vec<u8>, Box<dyn Error>> { | ||
let result = block_on(self.interface.control_in( | ||
ControlIn { | ||
control_type: ControlType::Class, | ||
recipient: Recipient::Interface, | ||
request: 0x01, | ||
value: (3 << 8) | report_number as u16, | ||
request: 1, | ||
value: 0x300 | report_number as u16, | ||
index: 0, | ||
length: 8 | ||
} | ||
)).into_result()?; | ||
|
||
dbg!(result.clone()); | ||
let result = result[2..].to_vec(); | ||
|
||
Ok(result) | ||
} | ||
|
||
pub fn set_feature_report(&self, report_number: u8, out_buffer: &Vec<u8>) -> Result<(), Box<dyn Error>> { | ||
block_on(self.interface.control_out( | ||
ControlOut { | ||
control_type: ControlType::Class, | ||
recipient: Recipient::Interface, | ||
request: 9, | ||
value: 0x300 | report_number as u16, | ||
index: 0, | ||
data: out_buffer, | ||
} | ||
)).into_result()?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters