Fix unused async

This commit is contained in:
hodasemi 2023-04-15 09:15:43 +02:00
parent 73f246e7d3
commit 0658e3015d
3 changed files with 55 additions and 62 deletions

View file

@ -2,14 +2,12 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(never_type)] #![feature(never_type)]
#![feature(async_closure)]
mod mouse_hid; mod mouse_hid;
mod mouse_sensor; mod mouse_sensor;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_futures::join::join; use embassy_futures::join::join;
use embassy_time::{Duration, Timer};
use usbd_hid::descriptor::MouseReport; use usbd_hid::descriptor::MouseReport;
@ -35,14 +33,20 @@ async fn main(_spawner: Spawner) {
let usb_fut = MouseHID::run_usb().await; let usb_fut = MouseHID::run_usb().await;
const PIXEL: i8 = 2;
const WAIT_TIME: Duration = Duration::from_secs(10);
// Do stuff with the class! // Do stuff with the class!
let hid_fut = async { let hid_fut = async {
loop { loop {
mouse_move(&mut mouse_hid, WAIT_TIME, PIXEL).await; if let Some((x, y)) = mouse_sensor.read_movement().await {
mouse_move(&mut mouse_hid, Duration::from_millis(50), -PIXEL).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. // If we had made everything `'static` above instead, we could do this using separate tasks instead.
join(hid_fut, usb_fut).await; 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;
}

View file

@ -76,24 +76,25 @@ where
Self { cs, spi, log } Self { cs, spi, log }
} }
fn access<F, R>(&mut self, f: F) -> R async fn access<F, R>(&mut self, f: F) -> R
where where
F: Fn(&mut Self) -> R, F: Fn(&mut Self) -> R,
{ {
self.cs.set_low(); self.cs.set_low();
Timer::after(Duration::from_micros(1)); Timer::after(Duration::from_micros(1)).await;
let res = f(self); let res = f(self);
self.cs.set_high(); self.cs.set_high();
Timer::after(Duration::from_micros(2)); Timer::after(Duration::from_micros(2)).await;
res res
} }
fn read(&mut self, address: u8) -> u8 { async fn read(&mut self, address: u8) -> u8 {
loop { loop {
let res = self.access(move |me| { let res = self
.access(move |me| {
if me.spi.blocking_write(&[address & Self::READ, 0]).is_err() { if me.spi.blocking_write(&[address & Self::READ, 0]).is_err() {
(me.log)("send address (read) failed"); (me.log)("send address (read) failed");
} }
@ -106,7 +107,8 @@ where
} }
buf[0] << 1 buf[0] << 1
}); })
.await;
if res != address { if res != address {
return res; 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| { self.access(move |me| {
if me if me
.spi .spi
@ -123,7 +125,8 @@ where
{ {
(me.log)("send address (write) failed"); (me.log)("send address (write) failed");
} }
}); })
.await;
} }
} }
@ -133,27 +136,27 @@ where
CS: CsPin<T> + 'a, CS: CsPin<T> + 'a,
T: SpiInstance, T: SpiInstance,
{ {
pub fn delta_x(&mut self) -> i8 { pub async fn delta_x(&mut self) -> i8 {
unsafe { mem::transmute(self.read(Self::REG_DELTA_X)) } unsafe { mem::transmute(self.read(Self::REG_DELTA_X).await) }
} }
pub fn delta_y(&mut self) -> i8 { pub async fn delta_y(&mut self) -> i8 {
unsafe { mem::transmute(self.read(Self::REG_DELTA_Y)) } unsafe { mem::transmute(self.read(Self::REG_DELTA_Y).await) }
} }
pub fn product_id_1(&mut self) -> u8 { pub async fn product_id_1(&mut self) -> u8 {
self.read(Self::REG_PRODUCT_ID1) self.read(Self::REG_PRODUCT_ID1).await
} }
pub fn product_id_2(&mut self) -> u8 { pub async fn product_id_2(&mut self) -> u8 {
self.read(Self::REG_PRODUCT_ID2) self.read(Self::REG_PRODUCT_ID2).await
} }
pub fn operation_mode(&mut self) -> u8 { pub async fn operation_mode(&mut self) -> u8 {
self.read(Self::REG_OPERATION_MODE) self.read(Self::REG_OPERATION_MODE).await
} }
pub fn motion_status(&mut self) -> u8 { pub async fn motion_status(&mut self) -> u8 {
self.read(Self::REG_MOTION_STATUS) self.read(Self::REG_MOTION_STATUS).await
} }
} }

View file

@ -112,13 +112,13 @@ where
// verify initialization // verify initialization
loop { loop {
if me.verify_product_id_1() { if me.verify_product_id_1().await {
break; break;
} }
} }
loop { loop {
if me.verify_product_id_2() { if me.verify_product_id_2().await {
break; break;
} }
} }
@ -128,20 +128,20 @@ where
me me
} }
fn verify_product_id_1(&mut self) -> bool { async fn verify_product_id_1(&mut self) -> bool {
const DEFAULT_VALUE: u8 = 0x30; 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; 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) { async fn motion_detected(&mut self) -> (bool, bool, bool) {
let reg = self.spi_device.motion_status(); let reg = self.spi_device.motion_status().await;
let motion = (reg & 0b10000000) != 0; let motion = (reg & 0b10000000) != 0;
let x_overflow = (reg & 0b00010000) != 0; let x_overflow = (reg & 0b00010000) != 0;
@ -150,16 +150,16 @@ where
(motion, x_overflow, y_overflow) (motion, x_overflow, y_overflow)
} }
pub fn read_movement(&'d mut self) -> Option<(i8, i8)> { pub async fn read_movement(&'d mut self) -> Option<(i8, i8)> {
let (motion, x_overflow, y_overflow) = self.motion_detected(); let (motion, x_overflow, y_overflow) = self.motion_detected().await;
if motion { if motion {
if x_overflow || y_overflow { if x_overflow || y_overflow {
return None; return None;
} }
let x = self.spi_device.delta_x(); let x = self.spi_device.delta_x().await;
let y = self.spi_device.delta_y(); let y = self.spi_device.delta_y().await;
Some((x, y)) Some((x, y))
} else { } else {