| 1 | use std::time::Duration; |
| 2 | |
| 3 | use nusb::transfer::{Control, ControlType, Recipient}; |
| 4 | |
| 5 | fn main() { |
| 6 | env_logger::init(); |
| 7 | let di = nusb::list_devices() |
| 8 | .unwrap() |
| 9 | .find(|d| d.vendor_id() == 0x59e3 && d.product_id() == 0x0a23) |
| 10 | .expect("device should be connected"); |
| 11 | |
| 12 | println!("Device info: {di:?}"); |
| 13 | |
| 14 | let device = di.open().unwrap(); |
| 15 | |
| 16 | // Linux can make control transfers without claiming an interface |
| 17 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 18 | { |
| 19 | let result = device.control_out_blocking( |
| 20 | Control { |
| 21 | control_type: ControlType::Vendor, |
| 22 | recipient: Recipient::Device, |
| 23 | request: 0x81, |
| 24 | value: 0x9999, |
| 25 | index: 0x9999, |
| 26 | }, |
| 27 | &[1, 2, 3, 4], |
| 28 | Duration::from_secs(1), |
| 29 | ); |
| 30 | println!("{result:?}"); |
| 31 | |
| 32 | let mut buf = [0; 64]; |
| 33 | |
| 34 | let len = device |
| 35 | .control_in_blocking( |
| 36 | Control { |
| 37 | control_type: ControlType::Vendor, |
| 38 | recipient: Recipient::Device, |
| 39 | request: 0x81, |
| 40 | value: 0x9999, |
| 41 | index: 0x9999, |
| 42 | }, |
| 43 | &mut buf, |
| 44 | Duration::from_secs(1), |
| 45 | ) |
| 46 | .unwrap(); |
| 47 | |
| 48 | println!("{result:?}, {data:?}", data = &buf[..len]); |
| 49 | } |
| 50 | |
| 51 | // but we also provide an API on the `Interface` to support Windows |
| 52 | let interface = device.claim_interface(0).unwrap(); |
| 53 | |
| 54 | let result = interface.control_out_blocking( |
| 55 | Control { |
| 56 | control_type: ControlType::Vendor, |
| 57 | recipient: Recipient::Device, |
| 58 | request: 0x81, |
| 59 | value: 0x9999, |
| 60 | index: 0x9999, |
| 61 | }, |
| 62 | &[1, 2, 3, 4, 5], |
| 63 | Duration::from_secs(1), |
| 64 | ); |
| 65 | println!("{result:?}"); |
| 66 | |
| 67 | let mut buf = [0; 64]; |
| 68 | |
| 69 | let len = interface |
| 70 | .control_in_blocking( |
| 71 | Control { |
| 72 | control_type: ControlType::Vendor, |
| 73 | recipient: Recipient::Device, |
| 74 | request: 0x81, |
| 75 | value: 0x9999, |
| 76 | index: 0x9999, |
| 77 | }, |
| 78 | &mut buf, |
| 79 | Duration::from_secs(1), |
| 80 | ) |
| 81 | .unwrap(); |
| 82 | println!("{data:?}", data = &buf[..len]); |
| 83 | } |