keyberon_smart_keyboard/
input.rs

1use core::convert::Infallible;
2
3use keyberon::debounce::Debouncer;
4use keyberon::layout::Event;
5
6/// For input from the smart_keymap crate.
7pub mod smart_keymap;
8
9/// Matrix scan result type.
10pub type PressedKeys<const COLS: usize, const ROWS: usize> = [[bool; COLS]; ROWS];
11
12// R for 'matrix get result type',
13// E for 'error of matrix get result type'.
14pub trait MatrixScanner<const COLS: usize, const ROWS: usize, E = Infallible> {
15    /// Check whether SW_1_1 is pressed.
16    fn is_boot_key_pressed(&mut self) -> bool;
17    fn get(&mut self) -> Result<[[bool; COLS]; ROWS], E>;
18}
19
20/// The keyboard "frontend", manages the keyboard from the hardware matrix
21/// through to keyboard events (presses/releases of coordinates on a keyboard layout).
22///
23/// This takes care of scanning the keyboard matrix, debouncing.
24pub struct Keyboard<const COLS: usize, const ROWS: usize, M: MatrixScanner<COLS, ROWS>> {
25    pub matrix: M,
26    pub debouncer: Debouncer<PressedKeys<COLS, ROWS>>,
27}
28
29impl<const COLS: usize, const ROWS: usize, M: MatrixScanner<COLS, ROWS>> Keyboard<COLS, ROWS, M> {
30    /// Constructs a new [Keyboard].
31    pub fn new(matrix: M, debouncer: Debouncer<PressedKeys<COLS, ROWS>>) -> Self {
32        Self { matrix, debouncer }
33    }
34
35    /// Scans the matrix and returns the debounced events.
36    pub fn events(&mut self) -> heapless::Vec<Event, 8> {
37        let key_presses = self.matrix.get().unwrap();
38        self.debouncer.events(key_presses).collect()
39    }
40}