use cst820_registers::{
FingerNum, Gesture, GestureId, RegisterAddress, TouchEvent, XposH, XposL, YposH, YposL,
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Touch {
pub x: u16,
pub y: u16,
pub fingers: u8,
pub event: TouchEvent,
pub gesture: Gesture,
}
impl Touch {
pub(crate) const START: RegisterAddress = RegisterAddress::GESTURE_ID;
pub(crate) const LEN: usize = 6;
pub fn from_bytes(bytes: &[u8; Self::LEN]) -> Self {
let gesture = GestureId::from(bytes[0]);
let fingers = FingerNum::from(bytes[1]);
let xh = XposH::from(bytes[2]);
let xl = XposL::from(bytes[3]);
let yh = YposH::from(bytes[4]);
let yl = YposL::from(bytes[5]);
Self {
x: u16::from(xh.xpos_high()) << 8 | u16::from(xl.value()),
y: u16::from(yh.ypos_high()) << 8 | u16::from(yl.value()),
fingers: fingers.value(),
event: xh.event(),
gesture: gesture.gesture(),
}
}
pub fn pressed(&self) -> bool {
matches!(self.fingers, 1..=2)
&& matches!(self.event, TouchEvent::Down | TouchEvent::Contact)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode() {
let touch = Touch::from_bytes(&[0x04, 0x01, 0x81, 0x23, 0xF0, 0xAB]);
assert_eq!(
touch,
Touch {
x: 0x123,
y: 0x0AB,
fingers: 1,
event: TouchEvent::Contact,
gesture: Gesture::SlideRight,
}
);
assert!(touch.pressed());
}
#[test]
fn garbage_is_not_a_touch() {
assert!(!Touch::from_bytes(&[0xFF; Touch::LEN]).pressed());
}
#[test]
fn lift_is_not_a_touch() {
assert!(!Touch::from_bytes(&[0x00, 0x01, 0x40, 0x10, 0x00, 0x10]).pressed());
}
}