diff --git a/firmware/src/key_mapping.rs b/firmware/src/key_mapping.rs index cbae9a5..a66a1a6 100644 --- a/firmware/src/key_mapping.rs +++ b/firmware/src/key_mapping.rs @@ -1,37 +1,24 @@ -use crate::{key_codes::KeyCode, NUM_COLS, NUM_ROWS}; +use crate::{ + key_codes::KeyCode::{self, *}, + NUM_COLS, NUM_ROWS, +}; #[rustfmt::skip] -pub const NORMAL_LAYER_MAPPING: [[KeyCode; NUM_ROWS]; NUM_COLS] = [ - [KeyCode::Escape, KeyCode::Tilde, KeyCode::Tab, KeyCode::CapsLock, KeyCode::LeftShift, KeyCode::Fn], - [KeyCode::F1, KeyCode::Num1, KeyCode::Q, KeyCode::A, KeyCode::Empty, KeyCode::LeftCtrl], - [KeyCode::F2, KeyCode::Num2, KeyCode::W, KeyCode::S, KeyCode::Z, KeyCode::LeftAlt], - [KeyCode::F3, KeyCode::Num3, KeyCode::E, KeyCode::D, KeyCode::X, KeyCode::LeftCmd], - [KeyCode::F4, KeyCode::Num4, KeyCode::R, KeyCode::F, KeyCode::C, KeyCode::Empty], - [KeyCode::F5, KeyCode::Num5, KeyCode::T, KeyCode::G, KeyCode::V, KeyCode::Empty], - [KeyCode::Empty, KeyCode::Num6, KeyCode::Y, KeyCode::H, KeyCode::B, KeyCode::Space], - [KeyCode::F6, KeyCode::Num7, KeyCode::U, KeyCode::J, KeyCode::N, KeyCode::Empty], - [KeyCode::F7, KeyCode::Num8, KeyCode::I, KeyCode::K, KeyCode::M, KeyCode::Empty], - [KeyCode::F8, KeyCode::Num9, KeyCode::O, KeyCode::L, KeyCode::Comma, KeyCode::Empty], - [KeyCode::F9, KeyCode::Num0, KeyCode::P, KeyCode::Semicolon, KeyCode::Period, KeyCode::RightCmd], - [KeyCode::F10, KeyCode::Minus, KeyCode::LeftSquareBracket, KeyCode::SingleQuote, KeyCode::ForwardSlash, KeyCode::Left], - [KeyCode::F11, KeyCode::Equals, KeyCode::RightSquareBracket, KeyCode::Enter, KeyCode::Up, KeyCode::Down], - [KeyCode::F12, KeyCode::Backspace, KeyCode::BackSlash, KeyCode::Empty, KeyCode::Empty, KeyCode::Right], +pub const NORMAL_LAYER_MAPPING: [[KeyCode; NUM_COLS]; NUM_ROWS] = [ + [Escape, F1, F2, F3, F4, F5, Empty, F6, F7, F8, F9, F10, F11, F12], + [Tilde, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num0, Minus, Equals, Backspace], + [Tab, Q, W, E, R, T, Y, U, I, O, P, LeftSquareBracket, RightSquareBracket, BackSlash], + [CapsLock, A, S, D, F, G, H, J, K, L, Semicolon, SingleQuote, Enter, Empty], + [LeftShift, Empty, Z, X, C, V, B, N, M, Comma, Period, ForwardSlash, Up, Empty], + [Fn, LeftCtrl, LeftAlt, LeftCmd, Empty, Empty, Space, Empty, Empty, Empty, RightCmd, Left, Down, Right], ]; #[rustfmt::skip] -pub const FN_LAYER_MAPPING: [[KeyCode; NUM_ROWS]; NUM_COLS] = [ - [KeyCode::Escape, KeyCode::Tilde, KeyCode::Tab, KeyCode::CapsLock, KeyCode::LeftShift, KeyCode::Empty], - [KeyCode::F1, KeyCode::Num1, KeyCode::Q, KeyCode::A, KeyCode::Empty, KeyCode::LeftCtrl], - [KeyCode::F2, KeyCode::Num2, KeyCode::W, KeyCode::S, KeyCode::Z, KeyCode::LeftAlt], - [KeyCode::F3, KeyCode::Num3, KeyCode::E, KeyCode::D, KeyCode::X, KeyCode::LeftCmd], - [KeyCode::F4, KeyCode::Num4, KeyCode::R, KeyCode::F, KeyCode::C, KeyCode::Empty], - [KeyCode::F5, KeyCode::Num5, KeyCode::T, KeyCode::G, KeyCode::V, KeyCode::Empty], - [KeyCode::Empty, KeyCode::Num6, KeyCode::Y, KeyCode::H, KeyCode::B, KeyCode::Space], - [KeyCode::F6, KeyCode::Num7, KeyCode::U, KeyCode::J, KeyCode::N, KeyCode::Empty], - [KeyCode::F7, KeyCode::Num8, KeyCode::I, KeyCode::K, KeyCode::M, KeyCode::Empty], - [KeyCode::F8, KeyCode::Num9, KeyCode::O, KeyCode::L, KeyCode::Comma, KeyCode::Empty], - [KeyCode::F9, KeyCode::Num0, KeyCode::P, KeyCode::Semicolon, KeyCode::Period, KeyCode::RightCmd], - [KeyCode::VolumeMute, KeyCode::Minus, KeyCode::LeftSquareBracket, KeyCode::SingleQuote, KeyCode::ForwardSlash, KeyCode::Left], - [KeyCode::VolumeDown, KeyCode::Equals, KeyCode::RightSquareBracket, KeyCode::Enter, KeyCode::Up, KeyCode::Down], - [KeyCode::VolumeUp, KeyCode::Backspace, KeyCode::BackSlash, KeyCode::Empty, KeyCode::Empty, KeyCode::Right], +pub const FN_LAYER_MAPPING: [[KeyCode; NUM_COLS]; NUM_ROWS] = [ + [Escape, F1, F2, F3, F4, F5, Empty, F6, F7, F8, F9, VolumeMute, VolumeDown, VolumeUp], + [Tilde, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num0, Minus, Equals, Backspace], + [Tab, Q, W, E, R, T, Y, U, I, O, P, LeftSquareBracket, RightSquareBracket, BackSlash], + [CapsLock, A, S, D, F, G, H, J, K, L, Semicolon, SingleQuote, Enter, Empty], + [LeftShift, Empty, Z, X, C, V, B, N, M, Comma, Period, ForwardSlash, Up, Empty], + [Empty, LeftCtrl, LeftAlt, LeftCmd, Empty, Empty, Space, Empty, Empty, Empty, RightCmd, Left, Down, Right], ]; diff --git a/firmware/src/key_scan.rs b/firmware/src/key_scan.rs index 742a04c..1b70c70 100644 --- a/firmware/src/key_scan.rs +++ b/firmware/src/key_scan.rs @@ -1,10 +1,14 @@ +use crate::{ + key_mapping::{FN_LAYER_MAPPING, NORMAL_LAYER_MAPPING}, + NUM_COLS, NUM_ROWS, +}; use core::{convert::Infallible, ops::Deref}; use cortex_m::delay::Delay; use embedded_hal::digital::v2::InputPin; use usbd_hid::descriptor::KeyboardReport; -use crate::{debounce::Debounce, key_codes::KeyCode, key_mapping}; +use crate::{debounce::Debounce, key_codes::KeyCode}; #[derive(Clone, Copy)] pub struct KeyScan { @@ -21,8 +25,8 @@ impl Deref for KeyScan KeyScan { pub fn scan( - rows: &[&dyn InputPin], - columns: &mut [&mut dyn embedded_hal::digital::v2::OutputPin], + rows: [&dyn InputPin; NUM_ROWS], + columns: &mut [&mut dyn embedded_hal::digital::v2::OutputPin; NUM_COLS], delay: &mut Delay, debounce: &mut Debounce, ) -> Self { @@ -61,11 +65,11 @@ impl From From( + matrix: [[KeyCode; NUM_COLS]; NUM_ROWS], +) -> [[KeyCode; NUM_ROWS]; NUM_COLS] { + let mut new_matrix: [[KeyCode; NUM_ROWS]; NUM_COLS] = [[KeyCode::Empty; NUM_ROWS]; NUM_COLS]; + + let mut col = 0; + + while col < NUM_COLS { + let mut row = 0; + while row < NUM_ROWS { + new_matrix[col][row] = matrix[row][col]; + row += 1; + } + + col += 1; + } + + new_matrix +} diff --git a/firmware/src/main.rs b/firmware/src/main.rs index ce3ff02..1f6d6c1 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -4,6 +4,7 @@ #![no_main] #![no_std] +use crate::key_scan::TRANSPOSED_NORMAL_LAYER_MAPPING; use usb_device::class::UsbClass; mod debounce; mod hid_descriptor; @@ -102,7 +103,7 @@ fn main() -> ! { rp2040_hal::gpio::Pins::new(pac.IO_BANK0, pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS); // Set up keyboard matrix pins. - let rows: &[&dyn InputPin] = &[ + let rows: [&dyn InputPin; NUM_ROWS] = [ &pins.gpio26.into_pull_down_input(), &pins.gpio25.into_pull_down_input(), &pins.gpio27.into_pull_down_input(), @@ -111,7 +112,7 @@ fn main() -> ! { &pins.gpio24.into_pull_down_input(), ]; - let cols: &mut [&mut dyn OutputPin] = &mut [ + let mut cols: [&mut dyn OutputPin; NUM_COLS] = [ &mut pins.gpio29.into_push_pull_output(), &mut pins.gpio16.into_push_pull_output(), &mut pins.gpio17.into_push_pull_output(), @@ -132,7 +133,7 @@ fn main() -> ! { let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); let mut modifier_mask = [[false; NUM_ROWS]; NUM_COLS]; - for (col, mapping_col) in modifier_mask.iter_mut().zip(key_mapping::NORMAL_LAYER_MAPPING) { + for (col, mapping_col) in modifier_mask.iter_mut().zip(TRANSPOSED_NORMAL_LAYER_MAPPING) { for (key, mapping_key) in col.iter_mut().zip(mapping_col) { *key = mapping_key.is_modifier(); } @@ -142,7 +143,7 @@ fn main() -> ! { let mut debounce: Debounce = Debounce::new(DEBOUNCE_TICKS, modifier_mask); // Do an initial scan of the keys so that we immediately have something to report to the host when asked. - let scan = KeyScan::scan(rows, cols, &mut delay, &mut debounce); + let scan = KeyScan::scan(rows, &mut cols, &mut delay, &mut debounce); critical_section::with(|cs| { KEYBOARD_REPORT.replace(cs, scan.into()); }); @@ -203,7 +204,7 @@ fn main() -> ! { } info!("Entering main loop"); loop { - let scan = KeyScan::scan(rows, cols, &mut delay, &mut debounce); + let scan = KeyScan::scan(rows, &mut cols, &mut delay, &mut debounce); critical_section::with(|cs| { KEYBOARD_REPORT.replace(cs, scan.into()); });