From cce29eb3ba00b384cce7246b3f624fb40ef80d7e Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 22 Mar 2023 16:44:20 +0100 Subject: [PATCH] Start reading registers --- src/mouse_sensor.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/mouse_sensor.rs b/src/mouse_sensor.rs index 0035b68..06fbf00 100644 --- a/src/mouse_sensor.rs +++ b/src/mouse_sensor.rs @@ -1,3 +1,5 @@ +use core::mem; + use cortex_m::prelude::_embedded_hal_spi_FullDuplex; use embedded_hal::digital::v2::OutputPin; use fugit::HertzU32; @@ -27,6 +29,26 @@ impl MouseSensor { const READ: u8 = 0x7F; const WRITE: u8 = 0x80; + const REG_PRODUCT_ID1: u8 = 0x00; + const REG_PRODUCT_ID2: u8 = 0x01; + const REG_MOTION_STATUS: u8 = 0x02; + const REG_DELTA_X: u8 = 0x03; + const REG_DELTA_Y: u8 = 0x04; + const REG_OPERATION_MODE: u8 = 0x05; + const REG_CONFIGURATION: u8 = 0x06; + const REG_WRITE_PROTECT: u8 = 0x09; + const REG_SLEEP1: u8 = 0x0A; + const REG_SLEEP2: u8 = 0x0B; + const REG_SLEEP3: u8 = 0x0C; + const REG_CPI_X: u8 = 0x0D; + const REG_CPI_Y: u8 = 0x0E; + const REG_DELTA_XY_HI: u8 = 0x12; + const REG_IQC: u8 = 0x13; + const REG_SHUTTER: u8 = 0x14; + const REG_FRAME_AVG: u8 = 0x17; + const REG_MOUSE_OPTION: u8 = 0x19; + const REG_SPI_MODE: u8 = 0x26; + pub fn new( pins: Pins, resets: &mut RESETS, @@ -90,6 +112,37 @@ impl MouseSensor { } pub fn read_movement(&mut self) -> Option<(i8, i8)> { - todo!() + let (motion, x_overflow, y_overflow) = self.motion_detected(); + + if motion { + if x_overflow || y_overflow { + return None; + } + + let x = self.delta_x(); + let y = self.delta_y(); + + Some((x, y)) + } else { + None + } + } + + pub fn delta_x(&mut self) -> i8 { + unsafe { mem::transmute(self.read(Self::REG_DELTA_X)) } + } + + pub fn delta_y(&mut self) -> i8 { + unsafe { mem::transmute(self.read(Self::REG_DELTA_Y)) } + } + + pub fn motion_detected(&mut self) -> (bool, bool, bool) { + let reg = self.read(Self::REG_MOTION_STATUS); + + let motion = (reg & 0b10000000) != 0; + let x_overflow = (reg & 0b00010000) != 0; + let y_overflow = (reg & 0b00001000) != 0; + + (motion, x_overflow, y_overflow) } }