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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#![warn(missing_docs)]
use std::{io::Cursor, time::Duration};
use byteorder::{ReadBytesExt, LE};
use rusb::{request_type, Device, DeviceHandle, DeviceList, GlobalContext};
pub struct Glasses {
device_handle: DeviceHandle<GlobalContext>,
}
#[derive(Debug, Clone)]
pub enum Error {
UsbError(rusb::Error),
NotFound,
Other(&'static str),
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub enum GlassesEvent {
Accelerometer(SensorData3D),
Gyroscope(SensorData3D),
Magnetometer(SensorData3D),
Misc(MiscSensors),
}
#[derive(Debug, Clone)]
pub struct SensorData3D {
pub timestamp: u64,
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Debug, Clone)]
pub struct MiscSensors {
pub keys: u8,
pub proximity: bool,
}
pub enum DisplayMode {
SameOnBoth = 0,
Stereo = 1,
}
const INTERRUPT_IN_ENDPOINT: u8 = 0x82;
const TIMEOUT: Duration = Duration::from_millis(250);
impl Glasses {
pub fn new() -> Result<Self> {
let device = Self::get_rusb_device()?;
let interface_num = Self::get_interrupt_interface(&device)?;
let mut device_handle = device.open()?;
device_handle.set_auto_detach_kernel_driver(true)?;
device_handle.claim_interface(interface_num)?;
let result = Self { device_handle };
Ok(result)
}
fn get_rusb_device() -> Result<Device<GlobalContext>> {
for device in DeviceList::new()?.iter() {
if let Ok(desc) = device.device_descriptor() {
if desc.vendor_id() == 0x04d2 && desc.product_id() == 0x162f {
return Ok(device);
}
}
}
Err(Error::NotFound)
}
fn get_interrupt_interface(device: &Device<GlobalContext>) -> Result<u8> {
let config_desc = device.config_descriptor(0)?;
for interface in config_desc.interfaces() {
for desc in interface.descriptors() {
for endpoint in desc.endpoint_descriptors() {
if endpoint.address() == INTERRUPT_IN_ENDPOINT {
return Ok(interface.number());
}
}
}
}
Err(Error::Other(
"Could not find endpoint, wrong USB structure (probably)",
))
}
pub fn serial(&self) -> Result<String> {
let mut result = [0u8; 0x40];
self.device_handle.read_control(
request_type(
rusb::Direction::In,
rusb::RequestType::Vendor,
rusb::Recipient::Device,
),
0x81,
0x100,
0,
&mut result,
TIMEOUT,
)?;
Ok(
String::from_utf8(result.iter().copied().take_while(|c| *c != 0).collect())
.map_err(|_| "Invalid serial string")?,
)
}
pub fn read_event(&self) -> Result<GlassesEvent> {
loop {
let mut result = [0u8; 0x40];
self.device_handle
.read_interrupt(INTERRUPT_IN_ENDPOINT, &mut result, TIMEOUT)?;
if result[0] == 2 {
return Ok(GlassesEvent::Misc(MiscSensors {
keys: result[47],
proximity: result[51] == 0,
}));
}
if result[0] == 4 {
let mut cursor = Cursor::new(&result);
cursor.set_position(9);
let timestamp = cursor.read_u64::<LE>().unwrap();
cursor.set_position(21);
let x = cursor.read_f32::<LE>().unwrap();
let y = cursor.read_f32::<LE>().unwrap();
let z = cursor.read_f32::<LE>().unwrap();
let sensor_data = SensorData3D { timestamp, x, y, z };
match result[1] {
1 => return Ok(GlassesEvent::Accelerometer(sensor_data)),
2 => return Ok(GlassesEvent::Gyroscope(sensor_data)),
3 => return Ok(GlassesEvent::Magnetometer(sensor_data)),
_ => (),
}
}
}
}
pub fn set_display_mode(&self, display_mode: DisplayMode) -> Result<()> {
self.device_handle.write_control(
request_type(
rusb::Direction::Out,
rusb::RequestType::Vendor,
rusb::Recipient::Device,
),
0x1,
display_mode as u16,
0x1,
&[0u8; 1],
TIMEOUT,
)?;
Ok(())
}
}
impl From<rusb::Error> for Error {
fn from(e: rusb::Error) -> Self {
Error::UsbError(e)
}
}
impl From<&'static str> for Error {
fn from(e: &'static str) -> Self {
Error::Other(e)
}
}