From 3392dec944e46aef9ce50f6ee44182132ac85acb Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Mon, 23 Jun 2025 19:01:36 -0600 Subject: [PATCH] fix keyboard timing --- src/peripherals/battery.rs | 2 +- src/peripherals/mod.rs | 32 ++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/peripherals/battery.rs b/src/peripherals/battery.rs index cd634ad..405b797 100644 --- a/src/peripherals/battery.rs +++ b/src/peripherals/battery.rs @@ -2,7 +2,7 @@ use embassy_rp::{ i2c::{Async, I2c}, peripherals::I2C1, }; -use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Watch}; +use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex, watch::Watch}; const REG_ID_BAT: u8 = 0x0b; diff --git a/src/peripherals/mod.rs b/src/peripherals/mod.rs index d0537c6..6d3468b 100644 --- a/src/peripherals/mod.rs +++ b/src/peripherals/mod.rs @@ -1,11 +1,13 @@ -/// handles polling keyboard events and battery levels from mcu over i2c1 -/// +//! handles polling keyboard events and battery levels from mcu over i2c1 +//! + +use embassy_futures::join::join; use embassy_rp::{ i2c::{Async, I2c}, peripherals::I2C1, }; -use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Sender}; -use embassy_time::Timer; +use embassy_sync::{blocking_mutex::raw::NoopRawMutex, channel::Sender, mutex::Mutex}; +use embassy_time::{Duration, Timer}; #[cfg(feature = "defmt")] use defmt::info; @@ -48,9 +50,23 @@ pub async fn peripherals_task( } } - loop { - read_battery(&mut i2c).await; + let i2c: Mutex> = Mutex::new(i2c); - read_keyboard_fifo(&mut i2c, &mut keyboard_channel).await; - } + join( + async { + loop { + Timer::after(Duration::from_secs(10)).await; + let mut guard = i2c.lock().await; + read_battery(&mut guard).await; + } + }, + async { + loop { + Timer::after(Duration::from_millis(50)).await; + let mut guard = i2c.lock().await; + read_keyboard_fifo(&mut guard, &mut keyboard_channel).await; + } + }, + ) + .await; }