keyberon_smart_keyboard/input/
smart_keymap.rs

1/// Callbacks for the keymap.
2pub struct KeymapCallbacks {
3    /// Callback for resetting keyboard state.
4    pub reset: Option<fn() -> ()>,
5    /// Callback for entering the bootloader.
6    pub reset_to_bootloader: Option<fn() -> ()>,
7}
8
9/// The keyboard "backend", manages the keyboard from the events received
10/// (presses/releases of coordinates on a keyboard layout).
11/// through to listing HID scancodes to report using HIDs.
12#[derive(Debug)]
13pub struct KeyboardBackend {
14    keymap: smart_keymap::Keymap,
15    keymap_output: smart_keymap::keymap::KeymapOutput,
16}
17
18impl KeyboardBackend {
19    /// Constructs a new keyboard backend.
20    pub fn new() -> Self {
21        Self::new_with_keymap(smart_keymap::new_keymap())
22    }
23
24    /// Constructs a new keyboard backend with the given keymap.
25    pub fn new_with_keymap(keymap: smart_keymap::Keymap) -> Self {
26        Self {
27            keymap,
28            keymap_output: smart_keymap::keymap::KeymapOutput::default(),
29        }
30    }
31
32    /// Set the keymap callbacks.
33    pub fn set_callbacks(&mut self, callbacks: KeymapCallbacks) {
34        use smart_keymap::keymap::KeymapCallback;
35        if let Some(callback_fn) = callbacks.reset {
36            self.keymap.set_callback(KeymapCallback::Reset, callback_fn);
37        }
38        if let Some(callback_fn) = callbacks.reset_to_bootloader {
39            self.keymap
40                .set_callback(KeymapCallback::ResetToBootloader, callback_fn);
41        }
42    }
43
44    /// Register a key event.
45    pub fn event(&mut self, event: smart_keymap::input::Event) {
46        self.keymap.handle_input(event);
47    }
48
49    /// A time event.
50    ///
51    /// This method must be called regularly, typically every millisecond.
52    ///
53    /// Returns true if the pressed_key_codes have changed.
54    pub fn tick(&mut self) -> bool {
55        self.keymap.tick();
56
57        let keymap_output = self.keymap.report_output();
58
59        let old_keymap_output = core::mem::replace(&mut self.keymap_output, keymap_output);
60
61        old_keymap_output != self.keymap_output
62    }
63
64    pub fn keymap_output(&self) -> &smart_keymap::keymap::KeymapOutput {
65        &self.keymap_output
66    }
67}
68
69/// Constructs a [smart_keymap::input::Event] from a [keyberon::layout::Event],
70///  using a map from row, column to (maybe) keymap index.
71pub fn keymap_index_of<const COLS: usize, const ROWS: usize>(
72    indices: &[[Option<u16>; COLS]; ROWS],
73    ev: keyberon::layout::Event,
74) -> Option<smart_keymap::input::Event> {
75    match ev {
76        keyberon::layout::Event::Press(r, c) => indices[r as usize][c as usize]
77            .map(|keymap_index| smart_keymap::input::Event::Press { keymap_index }),
78        keyberon::layout::Event::Release(r, c) => indices[r as usize][c as usize]
79            .map(|keymap_index| smart_keymap::input::Event::Release { keymap_index }),
80    }
81}