Skip to content

Commit

Permalink
fix: keys getting stuck when processing combos
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasotti committed Dec 7, 2024
1 parent ee8fecb commit b2478a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
11 changes: 7 additions & 4 deletions rmk/src/combo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use heapless::Vec;

use crate::action::KeyAction;
use crate::{action::KeyAction, keyboard::KeyEvent};

// Default number of macros
pub(crate) const COMBO_MAX_NUM: usize = 8;
Expand All @@ -26,15 +26,18 @@ impl Combo {
Self::new(Vec::new(), KeyAction::No)
}

pub fn update(&mut self, key_action: KeyAction) -> bool {
pub fn update(&mut self, key_action: KeyAction, key_event: KeyEvent) -> bool {
if !key_event.pressed || key_action == KeyAction::No {
return false;
}

let action_idx = self.actions.iter().position(|&a| a == key_action);
if let Some(i) = action_idx {
self.state |= 1 << i;
true
} else {
self.reset();
false
}
action_idx.is_some()
}

pub fn done(&self) -> bool {
Expand Down
39 changes: 20 additions & 19 deletions rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,27 +324,20 @@ impl<'a, const ROW: usize, const COL: usize, const NUM_LAYER: usize>
key_action: KeyAction,
key_event: KeyEvent,
) -> Option<KeyAction> {
for combo in self.keymap.borrow_mut().combos.iter_mut() {
if !key_event.pressed && combo.done() && combo.actions.contains(&key_action) {
combo.reset();
return Some(combo.output);
}
}

if self
.combo_actions_buffer
.push_back((key_action, key_event))
.is_err()
{
error!("Combo actions buffer overflowed! This is a bug and should not happen!");
}

let mut is_combo_action = false;
for combo in self.keymap.borrow_mut().combos.iter_mut() {
is_combo_action |= combo.update(key_action);
is_combo_action |= combo.update(key_action, key_event);
}

if is_combo_action && key_event.pressed {
if key_event.pressed && is_combo_action {
if self
.combo_actions_buffer
.push_back((key_action, key_event))
.is_err()
{
error!("Combo actions buffer overflowed! This is a bug and should not happen!");
}

let next_action = self
.keymap
.borrow_mut()
Expand All @@ -365,8 +358,17 @@ impl<'a, const ROW: usize, const COL: usize, const NUM_LAYER: usize>
}
next_action
} else {
if !key_event.pressed {
for combo in self.keymap.borrow_mut().combos.iter_mut() {
if combo.done() && combo.actions.contains(&key_action) {
combo.reset();
return Some(combo.output);
}
}
}

self.dispatch_combos().await;
None
Some(key_action)
}
}

Expand All @@ -378,7 +380,6 @@ impl<'a, const ROW: usize, const COL: usize, const NUM_LAYER: usize>
.borrow_mut()
.combos
.iter_mut()
.filter(|c| !c.done())
.for_each(Combo::reset);
}

Expand Down

0 comments on commit b2478a7

Please sign in to comment.