Start reading registers
This commit is contained in:
parent
2f8406da9b
commit
cce29eb3ba
1 changed files with 54 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue