1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use libusb;
use std::time::Duration;
const VID: u16 = 0xb49;
const PID: u16 = 0x64f;
pub struct TranceVibeDeviceInfo {
context: libusb::Context,
pub bus_number: u8,
pub address: u8,
}
pub struct TranceVibeDevice<'a> {
device: libusb::DeviceHandle<'a>,
}
impl<'a> TranceVibeDevice<'a> {
fn new(device: libusb::DeviceHandle<'a>) -> TranceVibeDevice<'a> {
TranceVibeDevice { device: device }
}
pub fn set(&mut self, speed: u8) -> libusb::Result<usize> {
let timeout = Duration::from_secs(1);
let bytes: [u8; 0] = [];
return self.device.write_control(libusb::request_type(libusb::Direction::Out,
libusb::RequestType::Vendor,
libusb::Recipient::Interface),
1,
speed as u16,
0,
&bytes,
timeout);
}
}
impl TranceVibeDeviceInfo {
fn new(bus_number: u8, address: u8) -> TranceVibeDeviceInfo {
TranceVibeDeviceInfo {
context: match libusb::Context::new() {
Ok(c) => c,
Err(_) => panic!("Can't create context!"),
},
bus_number: bus_number,
address: address,
}
}
pub fn open(&mut self) -> libusb::Result<TranceVibeDevice> {
let devices = match self.context.devices() {
Ok(d) => d,
Err(e) => return Err(e),
};
for mut dev in devices.iter() {
let device_desc = match dev.device_descriptor() {
Ok(d) => d,
Err(e) => return Err(e),
};
if device_desc.vendor_id() != VID || device_desc.product_id() != PID ||
dev.bus_number() != self.bus_number || dev.address() != self.address {
continue;
}
match dev.open() {
Ok(h) => return Ok(TranceVibeDevice::new(h)),
Err(e) => return Err(e),
};
}
return Err(libusb::Error::NoDevice);
}
}
pub fn get_devices() -> libusb::Result<Vec<TranceVibeDeviceInfo>> {
let mut context = match libusb::Context::new() {
Ok(c) => c,
Err(e) => return Err(e),
};
let mut devices = Vec::<TranceVibeDeviceInfo>::new();
let connected_devices = match context.devices() {
Ok(d) => d,
Err(e) => return Err(e),
};
for mut device in connected_devices.iter() {
let device_desc = match device.device_descriptor() {
Ok(d) => d,
Err(e) => return Err(e),
};
if device_desc.vendor_id() != VID || device_desc.product_id() != PID {
continue;
}
devices.push(TranceVibeDeviceInfo::new(device.bus_number(), device.address()));
}
Ok(devices)
}