Skip to content

Commit

Permalink
fix(via): fix via keycode converter
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Nov 14, 2023
1 parent d415e3f commit 676de3f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 64 deletions.
5 changes: 4 additions & 1 deletion rmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ cortex-m = "0.7.7"
cortex-m-rt = "0.7.3"
num_enum = { version = "0.7.0", default-features = false }
log = "0.4.19"
rtic-monotonics = { version = "1.0.0", features = ["cortex-m-systick"] }
rtic-monotonics = { version = "1", features = [
"cortex-m-systick",
"systick-10khz",
] }
usb-device = "0.2.9"
usbd-hid = "0.6.1"
packed_struct = { version = "0.10.1", default-features = false }
Expand Down
114 changes: 57 additions & 57 deletions rmk/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::keycode::{KeyCode, ModifierCombination};
use log::error;
use packed_struct::prelude::*;

/// A KeyAction is the action of a keyboard position, stored in keymap.
/// It can be a single action like triggering a key, or a composite keyboard action like TapHold
Expand Down Expand Up @@ -55,55 +53,56 @@ pub enum KeyAction {
}

impl KeyAction {
pub fn to_u16(&self) -> u16 {
match self {
KeyAction::No => 0x0000,
KeyAction::Transparent => 0x0001,
KeyAction::Single(a) => a.to_u16(),
KeyAction::Tap(a) => 0x0001 | a.to_u16(),
KeyAction::OneShot(a) => 0x0010 | a.to_u16(),
KeyAction::WithModifier(a, m) => {
let mut modifier_bits = [0];
// Ignore packing error
ModifierCombination::pack_to_slice(m, &mut modifier_bits).unwrap_or_default();
0x4000 | ((modifier_bits[0] as u16) << 8) | a.to_u16()
}
KeyAction::ModifierTapHold(action, modifier) => match action {
Action::Key(k) => {
if k.is_basic() {
let mut modifier_bits = [0];
// Ignore packing error
ModifierCombination::pack_to_slice(modifier, &mut modifier_bits)
.unwrap_or_default();
0x6000 | ((modifier_bits[0] as u16) << 8) | *k as u16
} else {
0x000
}
}
_ => {
error!("ModifierTapHold supports basic keycodes");
0x0000
}
},
KeyAction::LayerTapHold(action, layer) => {
if *layer < 8 {
0x8000 | ((*layer as u16) << 15) | action.to_u16()
} else {
error!("LayerTapHold supports layers 0~7, got {}", layer);
0x0000
}
}
KeyAction::TapHold(_tap, _hold) => {
error!("Unsupported TapHold action: {:?}", self);
0x0000
}
}
}


// FIXME: remove it later
// Depreciated, uses to_via_keycode
// pub fn to_u16(&self) -> u16 {
// match self {
// KeyAction::No => 0x0000,
// KeyAction::Transparent => 0x0001,
// KeyAction::Single(a) => a.to_u16(),
// KeyAction::Tap(a) => 0x0001 | a.to_u16(),
// KeyAction::OneShot(a) => 0x0010 | a.to_u16(),
// KeyAction::WithModifier(a, m) => {
// let mut modifier_bits = [0];
// // Ignore packing error
// ModifierCombination::pack_to_slice(m, &mut modifier_bits).unwrap_or_default();
// 0x4000 | ((modifier_bits[0] as u16) << 8) | a.to_u16()
// }
// KeyAction::ModifierTapHold(action, modifier) => match action {
// Action::Key(k) => {
// if k.is_basic() {
// let mut modifier_bits = [0];
// // Ignore packing error
// ModifierCombination::pack_to_slice(modifier, &mut modifier_bits)
// .unwrap_or_default();
// 0x6000 | ((modifier_bits[0] as u16) << 8) | *k as u16
// } else {
// 0x000
// }
// }
// _ => {
// error!("ModifierTapHold supports basic keycodes");
// 0x0000
// }
// },
// KeyAction::LayerTapHold(action, layer) => {
// if *layer < 8 {
// 0x8000 | ((*layer as u16) << 15) | action.to_u16()
// } else {
// error!("LayerTapHold supports layers 0~7, got {}", layer);
// 0x0000
// }
// }
// KeyAction::TapHold(_tap, _hold) => {
// error!("Unsupported TapHold action: {:?}", self);
// 0x0000
// }
// }
// }
}

/// A single basic action that a keyboard can execute.
/// An Action can be represented in 12 bits, aka 0x000 ~ 0xFFF
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Action {
/// A normal key stroke, uses for all keycodes defined in `KeyCode` enum, including mouse key, consumer/system control, etc.
Expand All @@ -119,13 +118,14 @@ pub enum Action {
}

impl Action {
pub fn to_u16(&self) -> u16 {
match self {
Action::Key(k) => *k as u16,
Action::LayerOn(layer) => *layer as u16,
Action::LayerOff(layer) => *layer as u16,
Action::LayerToggle(layer) => *layer as u16,
Action::Modifier(m) => m.to_bits() as u16,
}
}
// FIXME: remove it later
// pub fn to_u16(&self) -> u16 {
// match self {
// Action::Key(k) => *k as u16,
// Action::LayerOn(layer) => 0x5100 & (*layer as u16),
// Action::LayerOff(layer) => *layer as u16,
// Action::LayerToggle(layer) => *layer as u16,
// Action::Modifier(m) => m.to_bits() as u16,
// }
// }
}
6 changes: 3 additions & 3 deletions rmk/src/eeprom/eekeymap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{action::KeyAction, via::keycode_convert::from_via_keycode};
use crate::{action::KeyAction, via::keycode_convert::{from_via_keycode, to_via_keycode}};
use byteorder::{BigEndian, ByteOrder};
use embedded_storage::nor_flash::NorFlash;

Expand All @@ -18,15 +18,15 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
// 2-byte value, relative addr should be i*2
let addr = DYNAMIC_KEYMAP_ADDR + (i * 2) as u16;
let mut buf: [u8; 2] = [0xFF; 2];
BigEndian::write_u16(&mut buf, action.to_u16());
BigEndian::write_u16(&mut buf, to_via_keycode(action.clone()));
self.write_byte(addr, &buf);
});
}

pub fn set_keymap_action(&mut self, row: usize, col: usize, layer: usize, action: KeyAction) {
let addr = self.get_keymap_addr(row, col, layer);
let mut buf: [u8; 2] = [0xFF; 2];
BigEndian::write_u16(&mut buf, action.to_u16());
BigEndian::write_u16(&mut buf, to_via_keycode(action));
self.write_byte(addr, &buf);
}

Expand Down
8 changes: 5 additions & 3 deletions rmk/src/via/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ pub fn process_via_packet<
let row = report.output_data[2] as usize;
let col = report.output_data[3] as usize;
let keycode = BigEndian::read_u16(&report.output_data[4..6]);
let action = from_via_keycode(keycode);
info!(
"Setting keycode: 0x{:02X?} at ({},{}), layer {}",
keycode, row, col, layer
"Setting keycode: 0x{:02X?} at ({},{}), layer {} as {:?}",
keycode, row, col, layer, action
);
let action = from_via_keycode(keycode);
keymap.set_action_at(row, col, layer, action.clone());
match eeprom {
Some(e) => e.set_keymap_action(row, col, layer, action),
Expand Down Expand Up @@ -156,6 +156,8 @@ pub fn process_via_packet<
let offset = BigEndian::read_u16(&report.output_data[1..3]);
// size <= 28
let size = report.output_data[3];
// FIXME: Cannot get complete keymap
info!("Getting keymap buffer, offset: {}, size: {}", offset, size);
let mut idx = 4;
keymap
.layers
Expand Down

0 comments on commit 676de3f

Please sign in to comment.