From c24d9919a2a35602e4b819094115ec1efe61addb Mon Sep 17 00:00:00 2001 From: nazo6 Date: Thu, 21 Nov 2024 21:33:32 +0900 Subject: [PATCH] feat(keymanager): make able to use alloc vec instead of heapless --- lib/rktk-keymanager/Cargo.toml | 9 +++++-- lib/rktk-keymanager/src/lib.rs | 8 +++++++ lib/rktk-keymanager/src/state/common.rs | 4 ++-- lib/rktk-keymanager/src/state/key_resolver.rs | 9 +++---- .../src/state/manager/keyboard/mod.rs | 2 +- .../src/state/manager/keyboard/reporter.rs | 2 +- .../src/state/manager/mouse/mod.rs | 8 +++---- lib/rktk-keymanager/src/state/pressed.rs | 24 ++++++++----------- tools/cli/src/commands/build/profile.rs | 2 +- 9 files changed, 39 insertions(+), 29 deletions(-) diff --git a/lib/rktk-keymanager/Cargo.toml b/lib/rktk-keymanager/Cargo.toml index 40855542..ff1542df 100644 --- a/lib/rktk-keymanager/Cargo.toml +++ b/lib/rktk-keymanager/Cargo.toml @@ -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 } @@ -30,6 +30,11 @@ usbd-hid = { workspace = true } [features] +default = ["heapless"] + +alloc = [] +heapless = ["dep:heapless"] + # Enables state management. state = ["dep:usbd-hid"] # Enables serialization. @@ -41,4 +46,4 @@ std = [] # Enables type export by specta. tsify = ["std", "dep:tsify-next", "dep:wasm-bindgen", "postcard"] -_check = ["state"] +_check = ["state", "alloc"] diff --git a/lib/rktk-keymanager/src/lib.rs b/lib/rktk-keymanager/src/lib.rs index fb1b2915..0d30bd00 100644 --- a/lib/rktk-keymanager/src/lib.rs +++ b/lib/rktk-keymanager/src/lib.rs @@ -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 = alloc::vec::Vec; + +#[cfg(all(feature = "heapless", not(feature = "alloc")))] +type Vec = heapless::Vec; use keycode::{KeyAction, KeyCode}; diff --git a/lib/rktk-keymanager/src/state/common.rs b/lib/rktk-keymanager/src/state/common.rs index a3f024df..e86b9bd6 100644 --- a/lib/rktk-keymanager/src/state/common.rs +++ b/lib/rktk-keymanager/src/state/common.rs @@ -47,7 +47,7 @@ impl, + pub keycodes: crate::Vec, pub now: Instant, } @@ -55,7 +55,7 @@ impl CommonLocalState { pub fn new(now: Instant) -> Self { Self { normal_key_pressed: false, - keycodes: heapless::Vec::new(), + keycodes: crate::Vec::new(), now, } } diff --git a/lib/rktk-keymanager/src/state/key_resolver.rs b/lib/rktk-keymanager/src/state/key_resolver.rs index 09a2837c..34b1bd86 100644 --- a/lib/rktk-keymanager/src/state/key_resolver.rs +++ b/lib/rktk-keymanager/src/state/key_resolver.rs @@ -1,4 +1,5 @@ use crate::time::{Duration, Instant}; +use crate::Vec; use super::{ config::{ @@ -46,7 +47,7 @@ struct TapDanceActiveState { pub struct KeyResolver { key_state: [[Option; COL]; ROW], tap_dance: [TapDance; MAX_TAP_DANCE_REPEAT_COUNT as usize], - oneshot: heapless::Vec, + oneshot: Vec, tap_threshold: Duration, tap_dance_threshold: Duration, } @@ -62,7 +63,7 @@ impl KeyResolver { 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(), @@ -106,10 +107,10 @@ impl KeyResolver { 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 { diff --git a/lib/rktk-keymanager/src/state/manager/keyboard/mod.rs b/lib/rktk-keymanager/src/state/manager/keyboard/mod.rs index 8d2bf10e..a86d8964 100644 --- a/lib/rktk-keymanager/src/state/manager/keyboard/mod.rs +++ b/lib/rktk-keymanager/src/state/manager/keyboard/mod.rs @@ -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(); diff --git a/lib/rktk-keymanager/src/state/manager/keyboard/reporter.rs b/lib/rktk-keymanager/src/state/manager/keyboard/reporter.rs index 69719d22..a7463026 100644 --- a/lib/rktk-keymanager/src/state/manager/keyboard/reporter.rs +++ b/lib/rktk-keymanager/src/state/manager/keyboard/reporter.rs @@ -12,7 +12,7 @@ impl KeyboardReportGenerator { } #[allow(clippy::get_first)] - pub fn gen(&mut self, keycodes: &heapless::Vec, modifier: u8) -> Option { + pub fn gen(&mut self, keycodes: &crate::Vec, modifier: u8) -> Option { if modifier == 0 && keycodes.is_empty() { if !self.empty_kb_sent { self.empty_kb_sent = true; diff --git a/lib/rktk-keymanager/src/state/manager/mouse/mod.rs b/lib/rktk-keymanager/src/state/manager/mouse/mod.rs index 8a7cc492..c0901821 100644 --- a/lib/rktk-keymanager/src/state/manager/mouse/mod.rs +++ b/lib/rktk-keymanager/src/state/manager/mouse/mod.rs @@ -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; } diff --git a/lib/rktk-keymanager/src/state/pressed.rs b/lib/rktk-keymanager/src/state/pressed.rs index ac522d6d..3c9c145e 100644 --- a/lib/rktk-keymanager/src/state/pressed.rs +++ b/lib/rktk-keymanager/src/state/pressed.rs @@ -1,4 +1,4 @@ -use heapless::Vec; +use crate::Vec; use super::KeyChangeEvent; @@ -33,21 +33,17 @@ impl Pressed { 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, + }); } _ => {} } @@ -65,7 +61,7 @@ impl Pressed { .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 }); } } } diff --git a/tools/cli/src/commands/build/profile.rs b/tools/cli/src/commands/build/profile.rs index e1344646..30b2bfcd 100644 --- a/tools/cli/src/commands/build/profile.rs +++ b/tools/cli/src/commands/build/profile.rs @@ -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()), }, },