diff --git a/rmk/src/matrix.rs b/rmk/src/matrix.rs index 614f0266..ec9d2ffd 100644 --- a/rmk/src/matrix.rs +++ b/rmk/src/matrix.rs @@ -3,7 +3,7 @@ use crate::{ keyboard::{key_event_channel, KeyEvent}, CONNECTION_STATE, }; -use defmt::{error, info, Format}; +use defmt::{info, Format}; use embassy_time::{Instant, Timer}; use embedded_hal::digital::{InputPin, OutputPin}; #[cfg(feature = "async_matrix")] diff --git a/rmk/src/split/driver.rs b/rmk/src/split/driver.rs index b7264542..44a531cf 100644 --- a/rmk/src/split/driver.rs +++ b/rmk/src/split/driver.rs @@ -34,6 +34,7 @@ pub(crate) trait SplitWriter { /// /// The `ROW` and `COL` are the number of rows and columns of the corresponding peripheral's keyboard matrix. /// The `ROW_OFFSET` and `COL_OFFSET` are the offset of the peripheral's matrix in the keyboard's matrix. +/// TODO: Rename `PeripheralMatrixMonitor` pub(crate) struct PeripheralMatrixMonitor< const ROW: usize, const COL: usize, @@ -73,7 +74,7 @@ impl< error!("SplitDriver write error: {}", e); } loop { - match select(self.receiver.read(), embassy_time::Timer::after_millis(200)).await { + match select(self.receiver.read(), embassy_time::Timer::after_millis(500)).await { embassy_futures::select::Either::First(read_result) => match read_result { Ok(received_message) => { debug!("Received peripheral message: {}", received_message); @@ -101,20 +102,15 @@ impl< Err(e) => error!("Peripheral message read error: {:?}", e), }, embassy_futures::select::Either::Second(_) => { - // Check ConnectionState every 200ms - // Current, only ConnectionState will be notified to peripheral - let current_conn_state = CONNECTION_STATE.load(Ordering::Acquire); - if conn_state != current_conn_state { - // ConnectionState changed, notify peripheral - conn_state = current_conn_state; - if let Err(e) = self - .receiver - .write(&SplitMessage::ConnectionState(conn_state)) - .await - { - error!("SplitDriver write error: {}", e); - }; - } + // Sync ConnectionState every 500ms + conn_state = CONNECTION_STATE.load(Ordering::Acquire); + if let Err(e) = self + .receiver + .write(&SplitMessage::ConnectionState(conn_state)) + .await + { + error!("SplitDriver write error: {}", e); + }; } } } diff --git a/rmk/src/split/nrf/central.rs b/rmk/src/split/nrf/central.rs index 2ed88acd..543addbf 100644 --- a/rmk/src/split/nrf/central.rs +++ b/rmk/src/split/nrf/central.rs @@ -8,9 +8,12 @@ use embassy_sync::{ }; use nrf_softdevice::ble::{central, gatt_client, Address, AddressType}; -use crate::split::{ - driver::{PeripheralMatrixMonitor, SplitDriverError, SplitReader, SplitWriter}, - SplitMessage, SPLIT_MESSAGE_MAX_SIZE, +use crate::{ + split::{ + driver::{PeripheralMatrixMonitor, SplitDriverError, SplitReader, SplitWriter}, + SplitMessage, SPLIT_MESSAGE_MAX_SIZE, + }, + CONNECTION_STATE, }; /// Gatt client used in split central to receive split message from peripherals @@ -46,6 +49,7 @@ pub(crate) async fn run_ble_peripheral_monitor< let split_ble_driver = BleSplitCentralDriver { receiver: receive_receiver, sender: notify_sender, + connection_state: CONNECTION_STATE.load(Ordering::Acquire), }; let peripheral = @@ -169,6 +173,8 @@ pub(crate) struct BleSplitCentralDriver<'a> { pub(crate) receiver: Receiver<'a, CriticalSectionRawMutex, SplitMessage, 8>, // Sender that send message to peripherals pub(crate) sender: Sender<'a, CriticalSectionRawMutex, SplitMessage, 8>, + // Cached connection state + connection_state: bool, } impl<'a> SplitReader for BleSplitCentralDriver<'a> { @@ -179,6 +185,14 @@ impl<'a> SplitReader for BleSplitCentralDriver<'a> { impl SplitWriter for BleSplitCentralDriver<'_> { async fn write(&mut self, message: &SplitMessage) -> Result { + if let SplitMessage::ConnectionState(state) = message { + // Check if the connection state is changed + if self.connection_state == *state { + return Ok(SPLIT_MESSAGE_MAX_SIZE); + } + // ConnectionState changed, update cached state and notify peripheral + self.connection_state = *state; + } self.sender.send(message.clone()).await; Ok(SPLIT_MESSAGE_MAX_SIZE) } diff --git a/rmk/src/split/serial/mod.rs b/rmk/src/split/serial/mod.rs index 66503bf4..7c8a34f2 100644 --- a/rmk/src/split/serial/mod.rs +++ b/rmk/src/split/serial/mod.rs @@ -1,3 +1,5 @@ +use core::sync::atomic::Ordering; + use defmt::{error, info}; use embedded_io_async::{Read, Write}; @@ -8,6 +10,7 @@ use crate::{ peripheral::SplitPeripheral, SplitMessage, SPLIT_MESSAGE_MAX_SIZE, }, + CONNECTION_STATE, }; use super::driver::SplitDriverError;