pub const TOUCH_POINT_COUNT: usize = 4;
const LIFTED: u8 = 0xff;
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct TouchMouseStatus: u8 {
const MOUSE_LIFTED = 1 << 0;
const BUTTON_DOWN = 1 << 1;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct TouchMousePoint {
pub x: u16,
pub y: u16,
pub width_x: u8,
pub width_y: u8,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum TouchMouseRawEvent {
RawData {
touches: [Option<TouchMousePoint>; TOUCH_POINT_COUNT],
},
StatusChanged(TouchMouseStatus),
}
fn decode_touch(bytes: &[u8]) -> Option<TouchMousePoint> {
let [x_high, y_high, low_nibbles, widths] = [bytes[0], bytes[1], bytes[2], bytes[3]];
if x_high == LIFTED {
return None;
}
Some(TouchMousePoint {
x: (u16::from(x_high) << 4) | u16::from(low_nibbles & 0x0f),
y: (u16::from(y_high) << 4) | u16::from(low_nibbles >> 4),
width_x: widths & 0x0f,
width_y: widths >> 4,
})
}
pub(super) fn decode_event(sub_id: u8, payload: &[u8; 16]) -> Option<TouchMouseRawEvent> {
match sub_id {
0 => {
let mut touches = [None; TOUCH_POINT_COUNT];
for (i, touch) in touches.iter_mut().enumerate() {
*touch = decode_touch(&payload[i * 4..i * 4 + 4]);
}
Some(TouchMouseRawEvent::RawData { touches })
}
1 => Some(TouchMouseRawEvent::StatusChanged(
TouchMouseStatus::from_bits_retain(payload[0]),
)),
_ => None,
}
}