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
//! The sensor protocol.
//!
//! The sensor readout frame looks like this.
//!
//! ```text
//! struct {
//!  	uint8_t reserved:1;
//!  	uint8_t plus:1;
//!  	uint8_t minus:1;
//!  	uint8_t mute:1;
//! } button;
//! uint8_t reserved0;
//! uint8_t volume;
//! uint8_t reserved1[5];
//! union {
//! 	uint8_t as_byte;
//! 	struct {
//! 		uint8_t worn:1;
//! 		uint8_t display_active:1;
//! 		uint8_t hdmi_disconnected:1;
//! 		uint8_t microphone_muted:1;
//! 		uint8_t headphone_connected:1;
//! 		uint8_t reserved:2;
//! 		uint8_t tick:1;
//! 	};
//! } status;
//! uint8_t reserved2[11];
//! struct {
//! 	struct {
//! 		int16_t yaw;
//! 		int16_t pitch;
//! 		int16_t roll;
//! 	} gyro;
//! 	struct  {
//! 		int16_t x;
//! 		int16_t y;
//! 		int16_t z;
//! 	} accel;
//! 	uint8_t reserved[4];
//! } data[2];
//! uint8_t reserved3[12];
//! ```

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;

/// The sensor frame size.
pub const FRAME_SIZE: usize = 64;

/// Something that can be read from raw bytes.
pub trait Readable : Sized {
    /// Reads a new value from a reader.
    fn read(read: &mut dyn Read) -> Result<Self, Error>;

    /// Reads a new value from raw bytes.
    fn read_bytes(raw: &[u8; FRAME_SIZE]) -> Result<Self, Error> {
        Self::read(&mut io::Cursor::new(&raw[..]))
    }
}

/// The status of the PSVR headset sensors.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Readout {
    /// The status of the buttons.
    pub buttons: Buttons,
    /// The audio volume.
    pub volume: u8,
    /// The status of the headset.
    pub status: Status,
    /// The inertia at different instants of time.
    pub instants: [InertiaInstant; 2],
}

/// The status of the PSVR headset buttons.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Buttons {
    /// True if the plus button is pressed.
    ///
    /// This button is usually the volume up button.
    pub plus: bool,
    /// True if the minus button is pressed.
    ///
    /// This button is usually the volume up button.
    pub minus: bool,
    /// True if the mute button is pressed.
    pub mute: bool,
}

/// The headset status.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Status {
    /// True if the headset is currently being worn.
    pub worn: bool,
    pub display_active: bool,
    /// Is the HDMI disconnected.
    pub hdmi_disconnected: bool,
    /// Is the microphone muted.
    pub microphone_muted: bool,
    /// Is there headphones plugged into the headset
    pub headphone_connected: bool,
    pub tick: bool,
}

/// Inertia sensor values at an instant in time.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct InertiaInstant {
    /// The gyroscope readout.
    pub gyroscope_raw: na::Vector3<i16>,
    /// The accelerometer readout.
    pub accelerometer_raw: na::Vector3<i16>,
}

impl InertiaInstant {
    /// Gets the accelerometer readout vector.
    pub fn accelerometer(&self) -> na::Vector3<Scalar> {
        let f = |c| {
            let raw = c << 4; // Shift 12->16 bits.
            -(raw as Scalar / 32768.0)
        };

        na::Vector3::new(f(self.accelerometer_raw.x),
                         f(self.accelerometer_raw.y),
                         f(-self.accelerometer_raw.z))
    }

    /// Gets the gyroscope readout vector.
    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) // DEGTORAD
        };

        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,
        })
    }
}

/// Reads reserved data.
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 {
            // reserved:  (b & 0b0001) != 0,
            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,
            // reserved:         (b & (1 << 5)) != 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);
    }
}