keyberon_smart_keyboard/input/
smart_keymap.rs1pub struct KeymapCallbacks {
3 pub reset: Option<fn() -> ()>,
5 pub reset_to_bootloader: Option<fn() -> ()>,
7}
8
9#[derive(Debug)]
13pub struct KeyboardBackend {
14 keymap: smart_keymap::Keymap,
15 keymap_output: smart_keymap::keymap::KeymapOutput,
16}
17
18impl KeyboardBackend {
19 pub fn new() -> Self {
21 Self::new_with_keymap(smart_keymap::new_keymap())
22 }
23
24 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 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 pub fn event(&mut self, event: smart_keymap::input::Event) {
46 self.keymap.handle_input(event);
47 }
48
49 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
69pub 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}