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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use hmdee_core::Error;
use hmdee_core::math::Scalar;
use crate::usb::ByteOrder;
use std::io::prelude::*;
use std::{cmp, fmt, io};
use byteorder::ReadBytesExt;
use na;
pub const FRAME_SIZE: usize = 64;
pub trait Readable : Sized {
fn read(read: &mut dyn Read) -> Result<Self, Error>;
fn read_bytes(raw: &[u8; FRAME_SIZE]) -> Result<Self, Error> {
Self::read(&mut io::Cursor::new(&raw[..]))
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Readout {
pub buttons: Buttons,
pub volume: u8,
pub status: Status,
pub instants: [InertiaInstant; 2],
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Buttons {
pub plus: bool,
pub minus: bool,
pub mute: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Status {
pub worn: bool,
pub display_active: bool,
pub hdmi_disconnected: bool,
pub microphone_muted: bool,
pub headphone_connected: bool,
pub tick: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct InertiaInstant {
pub gyroscope_raw: na::Vector3<i16>,
pub accelerometer_raw: na::Vector3<i16>,
}
impl InertiaInstant {
pub fn accelerometer(&self) -> na::Vector3<Scalar> {
let f = |c| {
let raw = c << 4;
-(raw as Scalar / 32768.0)
};
na::Vector3::new(f(self.accelerometer_raw.x),
f(self.accelerometer_raw.y),
f(-self.accelerometer_raw.z))
}
pub fn gyroscope(&self) -> na::Vector3<Scalar> {
let f = |c| {
let pi = ::std::f64::consts::PI as Scalar;
(c as Scalar / 32768.0) * 2000.0
* (pi / 180.0)
};
na::Vector3::new(f(self.gyroscope_raw.x),
f(self.gyroscope_raw.y),
f(-self.gyroscope_raw.z))
}
}
impl Readable for Readout {
fn read(read: &mut dyn Read) -> Result<Self, Error> {
let buttons = Buttons::read(read)?;
read_reserved(read, 1)?;
let volume = read.read_u8()?;
read_reserved(read, 5)?;
let status = Status::read(read)?;
read_reserved(read, 11)?;
let instant_one = InertiaInstant::read(read)?;
let instant_two = InertiaInstant::read(read)?;
let instants = [instant_one, instant_two];
read_reserved(read, 12)?;
Ok(Readout {
buttons, volume, status, instants,
})
}
}
fn read_reserved(read: &mut dyn Read, n: usize) -> Result<(), Error> {
for _ in 0..n {
read.read_u8()?;
}
Ok(())
}
impl Readable for Buttons {
fn read(read: &mut dyn Read) -> Result<Self, Error> {
let b = read.read_u8()?;
Ok(Buttons {
plus: (b & 0b0010) != 0,
minus: (b & 0b0100) != 0,
mute: (b & 0b1000) != 0,
})
}
}
impl Readable for Status {
fn read(read: &mut dyn Read) -> Result<Self, Error> {
let b = read.read_u8()?;
Ok(Status {
worn: (b & (1 << 0)) != 0,
display_active: (b & (1 << 1)) != 0,
hdmi_disconnected: (b & (1 << 2)) != 0,
microphone_muted: (b & (1 << 3)) != 0,
headphone_connected: (b & (1 << 4)) != 0,
tick: (b & (1 << 6)) != 0,
})
}
}
impl<T> Readable for na::Vector3<T>
where T: Copy + Readable + fmt::Debug + cmp::PartialEq + 'static{
fn read(read: &mut dyn Read) -> Result<Self, Error> {
Ok(na::Vector3::new(
Readable::read(read)?,
Readable::read(read)?,
Readable::read(read)?,
))
}
}
impl Readable for InertiaInstant {
fn read(read: &mut dyn Read) -> Result<Self, Error> {
let gyroscope_raw = Readable::read(read)?;
let accelerometer_raw = Readable::read(read)?;
read_reserved(read, 4)?;
Ok(InertiaInstant { gyroscope_raw, accelerometer_raw })
}
}
macro_rules! impl_readable_primitive {
($ty:ident, $read_fn:ident, $byte_order:ty) => {
impl Readable for $ty {
fn read(read: &mut dyn Read) -> Result<Self, Error> {
Ok(read.$read_fn::<$byte_order>()?)
}
}
};
($ty:ident, $read_fn:ident) => {
impl Readable for $ty {
fn read(read: &mut dyn Read) -> Result<Self, Error> {
Ok(read.$read_fn()?)
}
}
};
}
impl_readable_primitive!(i8, read_i8);
impl_readable_primitive!(u8, read_u8);
impl_readable_primitive!(i16, read_i16, ByteOrder);
impl_readable_primitive!(u16, read_u16, ByteOrder);
impl_readable_primitive!(i32, read_i32, ByteOrder);
impl_readable_primitive!(u32, read_u32, ByteOrder);
#[cfg(test)]
mod test {
use super::*;
use std::io;
#[test]
fn reads_exactly_64_bytes() {
let data: [u8; 64] = [0; 64];
let mut read = io::Cursor::new(&data[..]);
assert_eq!(0, read.position());
Readout::read(&mut read).expect("failed to parse sensor readout");
assert_eq!(FRAME_SIZE, read.position() as usize);
}
}