Skip to content

Commit

Permalink
feat(keymanager): make able to use alloc vec instead of heapless
Browse files Browse the repository at this point in the history
  • Loading branch information
nazo6 committed Nov 21, 2024
1 parent 425e7f7 commit c24d991
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 29 deletions.
9 changes: 7 additions & 2 deletions lib/rktk-keymanager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository.workspace = true

[dependencies]
bitflags = { version = "2.6.0" }
heapless = { workspace = true }
heapless = { workspace = true, optional = true }
usbd-hid = { workspace = true, optional = true }

paste = { workspace = true }
Expand All @@ -30,6 +30,11 @@ usbd-hid = { workspace = true }


[features]
default = ["heapless"]

alloc = []
heapless = ["dep:heapless"]

# Enables state management.
state = ["dep:usbd-hid"]
# Enables serialization.
Expand All @@ -41,4 +46,4 @@ std = []
# Enables type export by specta.
tsify = ["std", "dep:tsify-next", "dep:wasm-bindgen", "postcard"]

_check = ["state"]
_check = ["state", "alloc"]
8 changes: 8 additions & 0 deletions lib/rktk-keymanager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

//! # rktk-keymanager
//! A library for managing key state and keymaps for self-made keyboards.
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
type Vec<T, const N: usize> = alloc::vec::Vec<T>;

#[cfg(all(feature = "heapless", not(feature = "alloc")))]
type Vec<T, const N: usize> = heapless::Vec<T, N>;

Check warning on line 13 in lib/rktk-keymanager/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

type alias `Vec` is never used

use keycode::{KeyAction, KeyCode};

Expand Down
4 changes: 2 additions & 2 deletions lib/rktk-keymanager/src/state/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ impl<const LAYER: usize, const ROW: usize, const COL: usize, const ENCODER_COUNT

pub(super) struct CommonLocalState {
pub normal_key_pressed: bool,
pub keycodes: heapless::Vec<u8, 6>,
pub keycodes: crate::Vec<u8, 6>,
pub now: Instant,
}

impl CommonLocalState {
pub fn new(now: Instant) -> Self {
Self {
normal_key_pressed: false,
keycodes: heapless::Vec::new(),
keycodes: crate::Vec::new(),
now,
}
}
Expand Down
9 changes: 5 additions & 4 deletions lib/rktk-keymanager/src/state/key_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::time::{Duration, Instant};
use crate::Vec;

use super::{
config::{
Expand Down Expand Up @@ -46,7 +47,7 @@ struct TapDanceActiveState {
pub struct KeyResolver<const ROW: usize, const COL: usize> {
key_state: [[Option<KeyPressedState>; COL]; ROW],
tap_dance: [TapDance; MAX_TAP_DANCE_REPEAT_COUNT as usize],
oneshot: heapless::Vec<OneShotState, { ONESHOT_STATE_SIZE as usize }>,
oneshot: Vec<OneShotState, { ONESHOT_STATE_SIZE as usize }>,
tap_threshold: Duration,
tap_dance_threshold: Duration,
}
Expand All @@ -62,7 +63,7 @@ impl<const ROW: usize, const COL: usize> KeyResolver<ROW, COL> {
pub fn new(mut config: KeyResolverConfig) -> Self {
Self {
key_state: core::array::from_fn(|_| core::array::from_fn(|_| None)),
oneshot: heapless::Vec::new(),
oneshot: Vec::new(),
tap_dance: core::array::from_fn(|i| TapDance {
state: None,
config: config.tap_dance[i].take(),
Expand Down Expand Up @@ -106,10 +107,10 @@ impl<const ROW: usize, const COL: usize> KeyResolver<ROW, COL> {
cls: &CommonLocalState,
key_events: &KeyStatusEvents,
encoder_events: &[(u8, EncoderDirection)],
) -> heapless::Vec<(EventType, KeyCode), { MAX_RESOLVED_KEY_COUNT as usize }> {
) -> Vec<(EventType, KeyCode), { MAX_RESOLVED_KEY_COUNT as usize }> {
use EventType::*;

let mut resolved_keys = heapless::Vec::new();
let mut resolved_keys = Vec::new();

if let Some(loc) = key_events.pressed.first() {
for osc in &mut self.oneshot {
Expand Down
2 changes: 1 addition & 1 deletion lib/rktk-keymanager/src/state/manager/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl KeyboardLocalState {
if let EventType::Pressed = event {
common_local_state.normal_key_pressed = true;
}
common_local_state.keycodes.push(*key as u8).ok();
let _ = common_local_state.keycodes.push(*key as u8);
}
(_, KeyCode::Modifier(mod_key)) => {
self.modifier |= mod_key.bits();
Expand Down
2 changes: 1 addition & 1 deletion lib/rktk-keymanager/src/state/manager/keyboard/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl KeyboardReportGenerator {
}

#[allow(clippy::get_first)]
pub fn gen(&mut self, keycodes: &heapless::Vec<u8, 6>, modifier: u8) -> Option<KeyboardReport> {
pub fn gen(&mut self, keycodes: &crate::Vec<u8, 6>, modifier: u8) -> Option<KeyboardReport> {
if modifier == 0 && keycodes.is_empty() {
if !self.empty_kb_sent {
self.empty_kb_sent = true;
Expand Down
8 changes: 4 additions & 4 deletions lib/rktk-keymanager/src/state/manager/mouse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ impl MouseLocalState {

let mut reset = true;
if global_mouse_state.arrowball_move.1 > 50 {
common_local_state.keycodes.push(Key::Right as u8).ok();
let _ = common_local_state.keycodes.push(Key::Right as u8);
} else if global_mouse_state.arrowball_move.1 < -50 {
common_local_state.keycodes.push(Key::Left as u8).ok();
let _ = common_local_state.keycodes.push(Key::Left as u8);
} else if global_mouse_state.arrowball_move.0 > 50 {
common_local_state.keycodes.push(Key::Down as u8).ok();
let _ = common_local_state.keycodes.push(Key::Down as u8);
} else if global_mouse_state.arrowball_move.0 < -50 {
common_local_state.keycodes.push(Key::Up as u8).ok();
let _ = common_local_state.keycodes.push(Key::Up as u8);
} else {
reset = false;
}
Expand Down
24 changes: 10 additions & 14 deletions lib/rktk-keymanager/src/state/pressed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use heapless::Vec;
use crate::Vec;

use super::KeyChangeEvent;

Expand Down Expand Up @@ -33,21 +33,17 @@ impl<const COL: usize, const ROW: usize> Pressed<COL, ROW> {
match (event.pressed, *key_pressed) {
(true, false) => {
*key_pressed = true;
pressed_events
.push(KeyLocation {
row: event.row,
col: event.col,
})
.ok();
let _ = pressed_events.push(KeyLocation {
row: event.row,
col: event.col,
});
}
(false, true) => {
*key_pressed = false;
released_events
.push(KeyLocation {
row: event.row,
col: event.col,
})
.ok();
let _ = released_events.push(KeyLocation {
row: event.row,
col: event.col,
});
}
_ => {}
}
Expand All @@ -65,7 +61,7 @@ impl<const COL: usize, const ROW: usize> Pressed<COL, ROW> {
.chain(released_events.iter())
.any(|e| e.row == row && e.col == col)
{
pressing_events.push(KeyLocation { row, col }).ok();
let _ = pressing_events.push(KeyLocation { row, col });
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/src/commands/build/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ gen_profile!(
debug: Some(true),
},
cargo_cmd: CargoCmd {
build_std: Some("core,panic_abort".to_string()),
build_std: Some("core,alloc,panic_abort".to_string()),
build_std_features: Some("panic_immediate_abort,optimize_for_size".to_string()),
},
},
Expand Down

0 comments on commit c24d991

Please sign in to comment.