From 8a619dac7967f7af93468508624ea639f7dd84ce Mon Sep 17 00:00:00 2001 From: Josh <24302717+JPrier@users.noreply.github.com> Date: Tue, 22 Jul 2025 16:56:58 -0700 Subject: [PATCH 1/2] Fix clippy issues and add Default impl --- bootloader/src/main.rs | 5 ++++- core/src/lib.rs | 11 +++++++++-- core/src/logic.rs | 6 ++++++ core/src/traits.rs | 3 +++ core/tests/mock_matrix.rs | 11 +++++++++++ core/tests/test_matrix.rs | 12 ++++++++++++ firmware/src/main.rs | 8 +++++--- simulator/src/main.rs | 2 +- simulator/src/sim_hal.rs | 4 +--- 9 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 core/src/traits.rs create mode 100644 core/tests/mock_matrix.rs create mode 100644 core/tests/test_matrix.rs diff --git a/bootloader/src/main.rs b/bootloader/src/main.rs index d2dd400..aa4cfda 100644 --- a/bootloader/src/main.rs +++ b/bootloader/src/main.rs @@ -2,6 +2,7 @@ #![no_main] use cortex_m::asm; use cortex_m_rt::entry; +#[cfg(not(test))] use panic_halt as _; use stm32h7::stm32h723; @@ -23,5 +24,7 @@ fn main() -> ! { } // Stay in bootloader (update mode) - loop {} + loop { + cortex_m::asm::wfi(); + } } diff --git a/core/src/lib.rs b/core/src/lib.rs index bfeecdc..f055851 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -10,9 +10,16 @@ extern crate std; pub type KeyState = u64; pub mod debounce; -pub use debounce::{Debouncer, MaskDebounce, NoDebounce, TimeDebounce}; -#[cfg(any(feature = "no-debounce", feature = "time-debounce", feature = "mask-debounce"))] +#[cfg(any( + feature = "no-debounce", + feature = "time-debounce", + feature = "mask-debounce" +))] pub use debounce::ActiveDebouncer; +pub use debounce::{Debouncer, MaskDebounce, NoDebounce, TimeDebounce}; + +pub mod traits; +pub use traits::MatrixHardware; /// Abstraction over the hardware keyboard interface. This trait is /// implemented by the STM32 firmware as well as the Linux simulator. diff --git a/core/src/logic.rs b/core/src/logic.rs index 18b5608..dfa87ce 100644 --- a/core/src/logic.rs +++ b/core/src/logic.rs @@ -11,6 +11,12 @@ pub struct KeyboardLogic { stable: KeyState, } +impl Default for KeyboardLogic { + fn default() -> Self { + Self::new() + } +} + impl KeyboardLogic { pub fn new() -> Self { Self { diff --git a/core/src/traits.rs b/core/src/traits.rs new file mode 100644 index 0000000..7959c06 --- /dev/null +++ b/core/src/traits.rs @@ -0,0 +1,3 @@ +pub trait MatrixHardware { + fn read_row(&self, row: usize) -> u16; +} diff --git a/core/tests/mock_matrix.rs b/core/tests/mock_matrix.rs new file mode 100644 index 0000000..cc48828 --- /dev/null +++ b/core/tests/mock_matrix.rs @@ -0,0 +1,11 @@ +use keyboard_core::MatrixHardware; + +pub struct MockMatrix { + pub rows: Vec, +} + +impl MatrixHardware for MockMatrix { + fn read_row(&self, row: usize) -> u16 { + self.rows[row] + } +} diff --git a/core/tests/test_matrix.rs b/core/tests/test_matrix.rs new file mode 100644 index 0000000..54699ee --- /dev/null +++ b/core/tests/test_matrix.rs @@ -0,0 +1,12 @@ +use keyboard_core::MatrixHardware; +mod mock_matrix; +use mock_matrix::MockMatrix; + +#[test] +fn read_row_returns_correct_value() { + let mock = MockMatrix { + rows: vec![0b1100u16, 0b0011u16], + }; + assert_eq!(mock.read_row(0), 0b1100u16); + assert_eq!(mock.read_row(1), 0b0011u16); +} diff --git a/firmware/src/main.rs b/firmware/src/main.rs index c09c96c..dcc63fe 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -3,6 +3,7 @@ use cortex_m::peripheral::{Peripherals, SYST}; use cortex_m_rt::entry; use keyboard_core::{self as core, KeyEventHandler, KeyboardHW, Timer}; +#[cfg(not(test))] use panic_halt as _; use stm32h7::stm32h723; @@ -20,7 +21,6 @@ impl Stm32Keyboard { struct SysTimer { syst: SYST, - start: u64, } struct EventSink; @@ -67,9 +67,11 @@ fn main() -> ! { syst.set_reload(SYSCLK_HZ / 1000); syst.clear_current(); syst.enable_counter(); - let timer = SysTimer { syst, start: 0 }; + let timer = SysTimer { syst }; let mut events = EventSink; core::run(&mut hw, &timer, &mut events); - loop {} + loop { + cortex_m::asm::nop(); + } } diff --git a/simulator/src/main.rs b/simulator/src/main.rs index a546c01..befd57b 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -3,7 +3,7 @@ mod input_scenario; use sim_hal::{SimKeyboard, SimTimer}; use input_scenario::example_scenario; -use keyboard_core::{KeyboardHW, Timer}; +use keyboard_core::KeyboardHW; fn scan_and_process(hw: &mut impl KeyboardHW, time: u64) { let keys = hw.read_keys(); diff --git a/simulator/src/sim_hal.rs b/simulator/src/sim_hal.rs index d9c613d..9b2037f 100644 --- a/simulator/src/sim_hal.rs +++ b/simulator/src/sim_hal.rs @@ -3,16 +3,14 @@ use keyboard_core::{KeyboardHW, Timer}; pub struct SimKeyboard { pub keys: u64, pub active_row: Option, - pub num_rows: usize, pub num_cols: usize, } impl SimKeyboard { - pub fn new(rows: usize, cols: usize) -> Self { + pub fn new(_rows: usize, cols: usize) -> Self { SimKeyboard { keys: 0, active_row: None, - num_rows: rows, num_cols: cols, } } From 6a59625a636099d44a82d9e8762f76756e928c92 Mon Sep 17 00:00:00 2001 From: Josh <24302717+JPrier@users.noreply.github.com> Date: Tue, 22 Jul 2025 17:04:57 -0700 Subject: [PATCH 2/2] derive Default for KeyboardLogic --- core/src/logic.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/core/src/logic.rs b/core/src/logic.rs index dfa87ce..a621b02 100644 --- a/core/src/logic.rs +++ b/core/src/logic.rs @@ -6,17 +6,12 @@ const NUM_KEYS: usize = NUM_ROWS * NUM_COLS; const DEBOUNCE_BITS: u8 = 5; const MASK: u16 = (1 << DEBOUNCE_BITS) - 1; +#[derive(Default)] pub struct KeyboardLogic { history: [u16; NUM_KEYS], stable: KeyState, } -impl Default for KeyboardLogic { - fn default() -> Self { - Self::new() - } -} - impl KeyboardLogic { pub fn new() -> Self { Self {