From d63a2592b4caa7ccfc0d78d643e032e1d4665bcf Mon Sep 17 00:00:00 2001 From: Zac Wilson Date: Mon, 7 Jul 2025 23:00:28 +0100 Subject: [PATCH] Started keyboard rewrite --- cardputer-bsc/src/keyboard.rs | 111 +++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/cardputer-bsc/src/keyboard.rs b/cardputer-bsc/src/keyboard.rs index 63a71b1..42c4a42 100644 --- a/cardputer-bsc/src/keyboard.rs +++ b/cardputer-bsc/src/keyboard.rs @@ -38,7 +38,10 @@ // SOFTWARE. use esp_idf_hal::{ - gpio::{Gpio11, Gpio13, Gpio15, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Gpio9, Input, InputOutput, Level, Output, PinDriver}, + gpio::{ + Gpio11, Gpio13, Gpio15, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Gpio9, Input, + InputOutput, Level, Output, PinDriver, + }, peripheral::Peripheral, }; use esp_idf_sys::EspError; @@ -104,6 +107,112 @@ pub enum KeyImprint { Space, } +impl KeyImprint { + fn from_packed_address(i: usize, j: usize) -> Self { + match (i,j) { + (0, 0) => KeyImprint::Backquote, + (0, 1) => KeyImprint::Two, + (0, 2) => KeyImprint::Four, + (0, 3) => KeyImprint::Six, + (0, 4) => KeyImprint::Eight, + (0, 5) => KeyImprint::Zero, + (0, 6) => KeyImprint::Equal, + + (1, 0) => KeyImprint::Tab, + (1, 1) => KeyImprint::W, + (1, 2) => KeyImprint::R, + (1, 3) => KeyImprint::Y, + (1, 4) => KeyImprint::I, + (1, 5) => KeyImprint::P, + (1, 6) => KeyImprint::CloseSquareBracket, + + (2, 0) => KeyImprint::LeftFn, + (2, 1) => KeyImprint::A, + (2, 2) => KeyImprint::D, + (2, 3) => KeyImprint::G, + (2, 4) => KeyImprint::J, + (2, 5) => KeyImprint::L, + (2, 6) => KeyImprint::Quote, + + (3, 0) => KeyImprint::LeftCtrl, + (3, 1) => KeyImprint::LeftAlt, + (3, 2) => KeyImprint::X, + (3, 3) => KeyImprint::V, + (3, 4) => KeyImprint::N, + (3, 5) => KeyImprint::Comma, + (3, 6) => KeyImprint::Slash, + + (4, 0) => KeyImprint::One, + (4, 1) => KeyImprint::Three, + (4, 2) => KeyImprint::Five, + (4, 3) => KeyImprint::Seven, + (4, 4) => KeyImprint::Nine, + (4, 5) => KeyImprint::Minus, + (4, 6) => KeyImprint::Backspace, + + (5, 0) => KeyImprint::Q, + (5, 1) => KeyImprint::E, + (5, 2) => KeyImprint::T, + (5, 3) => KeyImprint::U, + (5, 4) => KeyImprint::O, + (5, 5) => KeyImprint::OpenSquareBracket, + (5, 6) => KeyImprint::Backslash, + + (6, 0) => KeyImprint::LeftShift, + (6, 1) => KeyImprint::S, + (6, 2) => KeyImprint::F, + (6, 3) => KeyImprint::H, + (6, 4) => KeyImprint::K, + (6, 5) => KeyImprint::SemiColon, + (6, 6) => KeyImprint::Enter, + + (7, 0) => KeyImprint::LeftOpt, + (7, 1) => KeyImprint::Z, + (7, 2) => KeyImprint::C, + (7, 3) => KeyImprint::B, + (7, 4) => KeyImprint::M, + (7, 5) => KeyImprint::Period, + (7, 6) => KeyImprint::Space, + + _ => unreachable!() + } + } +} + +struct FullKeyboardState { + packed_state: [u8; 7], +} + +enum KeyAction { + Pressed, + Released, +} + +impl FullKeyboardState { + fn changes<'a>(&'a self, old: &'a Self) -> impl Iterator + 'a { + self.packed_state + .iter() + .zip(old.packed_state.iter()) + .enumerate() + .flat_map(|(i, (new, old))| { + let diff = new ^ old; // XOR to find what changed + (0..8).filter_map(move |j| { + if (1 << j) & diff != 0 { + let key = KeyImprint::from_packed_address(i, j); + let action = if (1 << j) & new == 0 { + KeyAction::Released + } else { + KeyAction::Pressed + }; + Some((key, action)) + } else { + None + } + }) + }) + } +} + #[derive(Debug, Clone, Copy, PartialEq)] pub enum Modified { Graph(char),