Skip to content

Commit

Permalink
refactor: move Combo struct to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasotti committed Dec 6, 2024
1 parent 19fa660 commit 411ee2c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 56 deletions.
55 changes: 55 additions & 0 deletions rmk/src/combo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use heapless::Vec;

use crate::action::KeyAction;

// Default number of macros
pub(crate) const COMBO_MAX_NUM: usize = 8;
// Default size of macros
pub(crate) const COMBO_MAX_LENGTH: usize = 4;

pub(crate) struct Combo {
pub(crate) actions: Vec<KeyAction, COMBO_MAX_LENGTH>,
pub(crate) output: KeyAction,
state: u8,
}

impl Combo {
pub fn new(actions: Vec<KeyAction, COMBO_MAX_LENGTH>, output: KeyAction) -> Self {
Self {
actions,
output,
state: 0,
}
}

pub fn empty() -> Self {
Self::new(Vec::new(), KeyAction::No)
}

pub fn update(&mut self, key_action: KeyAction) -> bool {
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
}
}

pub fn done(&self) -> bool {
self.started() && self.keys_pressed() == self.actions.len() as u32
}

pub fn started(&self) -> bool {
self.state != 0
}

pub fn keys_pressed(&self) -> u32 {
self.state.count_ones()
}

pub fn reset(&mut self) {
self.state = 0;
}
}
3 changes: 2 additions & 1 deletion rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::combo::{Combo, COMBO_MAX_LENGTH};
use crate::config::BehaviorConfig;
use crate::{
action::{Action, KeyAction},
hid::{ConnectionType, HidWriterWrapper},
keyboard_macro::{MacroOperation, NUM_MACRO},
keycode::{KeyCode, ModifierCombination},
keymap::{Combo, KeyMap, COMBO_MAX_LENGTH},
keymap::KeyMap,
usb::descriptor::{CompositeReport, CompositeReportType, ViaReport},
KEYBOARD_STATE,
};
Expand Down
52 changes: 1 addition & 51 deletions rmk/src/keymap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
action::KeyAction,
combo::{Combo, COMBO_MAX_NUM},
keyboard::KeyEvent,
keyboard_macro::{MacroOperation, MACRO_SPACE_SIZE},
keycode::KeyCode,
Expand All @@ -8,59 +9,8 @@ use crate::{
};
use defmt::{error, warn};
use embedded_storage_async::nor_flash::NorFlash;
use heapless::Vec;
use num_enum::FromPrimitive;

pub(crate) const COMBO_MAX_NUM: usize = 8;
pub(crate) const COMBO_MAX_LENGTH: usize = 4;

pub(crate) struct Combo {
pub(crate) actions: Vec<KeyAction, COMBO_MAX_LENGTH>,
pub(crate) output: KeyAction,
state: u8,
}

impl Combo {
pub fn new(actions: Vec<KeyAction, COMBO_MAX_LENGTH>, output: KeyAction) -> Self {
Self {
actions,
output,
state: 0,
}
}

pub fn empty() -> Self {
Self::new(Vec::new(), KeyAction::No)
}

pub fn update(&mut self, key_action: KeyAction) -> bool {
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
}
}

pub fn done(&self) -> bool {
self.started() && self.keys_pressed() == self.actions.len() as u32
}

pub fn started(&self) -> bool {
self.state != 0
}

pub fn keys_pressed(&self) -> u32 {
self.state.count_ones()
}

pub fn reset(&mut self) {
self.state = 0;
}
}

/// Keymap represents the stack of layers.
///
/// The conception of Keymap in rmk is borrowed from qmk: <https://docs.qmk.fm/#/keymap>.
Expand Down
1 change: 1 addition & 0 deletions rmk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use {embedded_storage_async::nor_flash::NorFlash as AsyncNorFlash, storage::Stor
pub mod action;
#[cfg(feature = "_ble")]
pub mod ble;
mod combo;
pub mod config;
mod debounce;
pub mod direct_pin;
Expand Down
4 changes: 2 additions & 2 deletions rmk/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod eeconfig;
pub mod nor_flash;

use crate::config::StorageConfig;
use crate::{combo::Combo, config::StorageConfig};
use byteorder::{BigEndian, ByteOrder};
use core::fmt::Debug;
use core::ops::Range;
Expand All @@ -19,11 +19,11 @@ use sequential_storage::{
#[cfg(feature = "_nrf_ble")]
use {crate::ble::nrf::bonder::BondInfo, core::mem};

use crate::keyboard_macro::MACRO_SPACE_SIZE;
use crate::{
action::KeyAction,
via::keycode_convert::{from_via_keycode, to_via_keycode},
};
use crate::{keyboard_macro::MACRO_SPACE_SIZE, keymap::Combo};

use self::eeconfig::EeKeymapConfig;

Expand Down
5 changes: 3 additions & 2 deletions rmk/src/via/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use super::{
vial::{VialCommand, VialDynamic},
};
use crate::{
config::VialConfig,
action::KeyAction,
combo::COMBO_MAX_NUM,
config::VialConfig,
hid::{HidError, HidReaderWriterWrapper},
keyboard_macro::{MACRO_SPACE_SIZE, NUM_MACRO},
keymap::{KeyMap, COMBO_MAX_NUM},
keymap::KeyMap,
storage::{ComboData, FlashOperationMessage, FLASH_CHANNEL},
usb::descriptor::ViaReport,
via::{
Expand Down

0 comments on commit 411ee2c

Please sign in to comment.