Skip to main content

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",
21///  manages the keyboard from the hardware matrix through to keyboard events
22///  (presses/releases of coordinates on a keyboard layout).
23///
24/// This takes care of scanning the keyboard matrix, debouncing.
25pub struct Keyboard<const COLS: usize, const ROWS: usize, M: MatrixScanner<COLS, ROWS>> {
26    pub matrix: M,
27    pub debouncer: Debouncer<PressedKeys<COLS, ROWS>>,
28}
29
30impl<const COLS: usize, const ROWS: usize, M: MatrixScanner<COLS, ROWS>> Keyboard<COLS, ROWS, M> {
31    /// Constructs a new [Keyboard].
32    pub fn new(matrix: M, debouncer: Debouncer<PressedKeys<COLS, ROWS>>) -> Self {
33        Self { matrix, debouncer }
34    }
35
36    /// Scans the matrix and returns the debounced events.
37    pub fn events(&mut self) -> heapless::Vec<Event, 8> {
38        match self.matrix.get() {
39            Ok(key_presses) => self.debouncer.events(key_presses).collect(),
40            Err(_) => heapless::Vec::new(),
41        }
42    }
43}