diff --git a/src/main.rs b/src/main.rs index d0e268d..9f5071c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,12 @@ #![no_main] #![feature(type_alias_impl_trait)] #![feature(never_type)] -#![feature(async_closure)] mod mouse_hid; mod mouse_sensor; use embassy_executor::Spawner; use embassy_futures::join::join; -use embassy_time::{Duration, Timer}; use usbd_hid::descriptor::MouseReport; @@ -35,14 +33,20 @@ async fn main(_spawner: Spawner) { let usb_fut = MouseHID::run_usb().await; - const PIXEL: i8 = 2; - const WAIT_TIME: Duration = Duration::from_secs(10); - // Do stuff with the class! let hid_fut = async { loop { - mouse_move(&mut mouse_hid, WAIT_TIME, PIXEL).await; - mouse_move(&mut mouse_hid, Duration::from_millis(50), -PIXEL).await; + if let Some((x, y)) = mouse_sensor.read_movement().await { + let report = MouseReport { + buttons: 0, + x, + y, + wheel: 0, + pan: 0, + }; + + mouse_hid.mouse_report(report).await; + } } }; @@ -50,17 +54,3 @@ async fn main(_spawner: Spawner) { // If we had made everything `'static` above instead, we could do this using separate tasks instead. join(hid_fut, usb_fut).await; } - -async fn mouse_move(mouse_hid: &mut MouseHID<'static>, duration: Duration, v: i8) { - Timer::after(duration).await; - - let report = MouseReport { - buttons: 0, - x: 0, - y: v, - wheel: 0, - pan: 0, - }; - - mouse_hid.mouse_report(report).await; -} diff --git a/src/mouse_sensor/paw3212db_tjdt.rs b/src/mouse_sensor/paw3212db_tjdt.rs index c6550d2..74f870f 100644 --- a/src/mouse_sensor/paw3212db_tjdt.rs +++ b/src/mouse_sensor/paw3212db_tjdt.rs @@ -76,37 +76,39 @@ where Self { cs, spi, log } } - fn access(&mut self, f: F) -> R + async fn access(&mut self, f: F) -> R where F: Fn(&mut Self) -> R, { self.cs.set_low(); - Timer::after(Duration::from_micros(1)); + Timer::after(Duration::from_micros(1)).await; let res = f(self); self.cs.set_high(); - Timer::after(Duration::from_micros(2)); + Timer::after(Duration::from_micros(2)).await; res } - fn read(&mut self, address: u8) -> u8 { + async fn read(&mut self, address: u8) -> u8 { loop { - let res = self.access(move |me| { - if me.spi.blocking_write(&[address & Self::READ, 0]).is_err() { - (me.log)("send address (read) failed"); - } + let res = self + .access(move |me| { + if me.spi.blocking_write(&[address & Self::READ, 0]).is_err() { + (me.log)("send address (read) failed"); + } - let mut buf = [0]; + let mut buf = [0]; - if me.spi.blocking_read(&mut buf).is_err() { - (me.log)("read failed"); - return 0; - } + if me.spi.blocking_read(&mut buf).is_err() { + (me.log)("read failed"); + return 0; + } - buf[0] << 1 - }); + buf[0] << 1 + }) + .await; if res != address { return res; @@ -114,7 +116,7 @@ where } } - fn write(&mut self, address: u8, value: u8) { + async fn write(&mut self, address: u8, value: u8) { self.access(move |me| { if me .spi @@ -123,7 +125,8 @@ where { (me.log)("send address (write) failed"); } - }); + }) + .await; } } @@ -133,27 +136,27 @@ where CS: CsPin + 'a, T: SpiInstance, { - pub fn delta_x(&mut self) -> i8 { - unsafe { mem::transmute(self.read(Self::REG_DELTA_X)) } + pub async fn delta_x(&mut self) -> i8 { + unsafe { mem::transmute(self.read(Self::REG_DELTA_X).await) } } - pub fn delta_y(&mut self) -> i8 { - unsafe { mem::transmute(self.read(Self::REG_DELTA_Y)) } + pub async fn delta_y(&mut self) -> i8 { + unsafe { mem::transmute(self.read(Self::REG_DELTA_Y).await) } } - pub fn product_id_1(&mut self) -> u8 { - self.read(Self::REG_PRODUCT_ID1) + pub async fn product_id_1(&mut self) -> u8 { + self.read(Self::REG_PRODUCT_ID1).await } - pub fn product_id_2(&mut self) -> u8 { - self.read(Self::REG_PRODUCT_ID2) + pub async fn product_id_2(&mut self) -> u8 { + self.read(Self::REG_PRODUCT_ID2).await } - pub fn operation_mode(&mut self) -> u8 { - self.read(Self::REG_OPERATION_MODE) + pub async fn operation_mode(&mut self) -> u8 { + self.read(Self::REG_OPERATION_MODE).await } - pub fn motion_status(&mut self) -> u8 { - self.read(Self::REG_MOTION_STATUS) + pub async fn motion_status(&mut self) -> u8 { + self.read(Self::REG_MOTION_STATUS).await } } diff --git a/src/mouse_sensor/wrapper.rs b/src/mouse_sensor/wrapper.rs index 48dab7c..1d4bb18 100644 --- a/src/mouse_sensor/wrapper.rs +++ b/src/mouse_sensor/wrapper.rs @@ -112,13 +112,13 @@ where // verify initialization loop { - if me.verify_product_id_1() { + if me.verify_product_id_1().await { break; } } loop { - if me.verify_product_id_2() { + if me.verify_product_id_2().await { break; } } @@ -128,20 +128,20 @@ where me } - fn verify_product_id_1(&mut self) -> bool { + async fn verify_product_id_1(&mut self) -> bool { const DEFAULT_VALUE: u8 = 0x30; - DEFAULT_VALUE == self.spi_device.product_id_1() + DEFAULT_VALUE == self.spi_device.product_id_1().await } - fn verify_product_id_2(&mut self) -> bool { + async fn verify_product_id_2(&mut self) -> bool { const DEFAULT_VALUE: u8 = 0x02; - DEFAULT_VALUE == self.spi_device.product_id_2() + DEFAULT_VALUE == self.spi_device.product_id_2().await } - fn motion_detected(&mut self) -> (bool, bool, bool) { - let reg = self.spi_device.motion_status(); + async fn motion_detected(&mut self) -> (bool, bool, bool) { + let reg = self.spi_device.motion_status().await; let motion = (reg & 0b10000000) != 0; let x_overflow = (reg & 0b00010000) != 0; @@ -150,16 +150,16 @@ where (motion, x_overflow, y_overflow) } - pub fn read_movement(&'d mut self) -> Option<(i8, i8)> { - let (motion, x_overflow, y_overflow) = self.motion_detected(); + pub async fn read_movement(&'d mut self) -> Option<(i8, i8)> { + let (motion, x_overflow, y_overflow) = self.motion_detected().await; if motion { if x_overflow || y_overflow { return None; } - let x = self.spi_device.delta_x(); - let y = self.spi_device.delta_y(); + let x = self.spi_device.delta_x().await; + let y = self.spi_device.delta_y().await; Some((x, y)) } else {