Skip to content

Commit

Permalink
Merge pull request #19 from daystram/feat/modified-key
Browse files Browse the repository at this point in the history
Support modified keys
  • Loading branch information
daystram authored Sep 7, 2024
2 parents ca33a4e + bd6d2ad commit bf042be
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 123 deletions.
125 changes: 112 additions & 13 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use core::mem;
use defmt::Format;
use enum_map::Enum;
use usbd_human_interface_device::page::Keyboard;

#[allow(dead_code)]
Expand All @@ -8,24 +10,14 @@ pub enum Action<L: LayerIndex> {
Pass,
None,
Key(Key),
ModifiedKey(ModifiedKey),
Control(Control),
LayerModifier(L),
}

impl<L: LayerIndex> From<Key> for Action<L> {
fn from(value: Key) -> Self {
Action::Key(value)
}
}

impl<L: LayerIndex> From<L> for Action<L> {
fn from(value: L) -> Self {
Action::LayerModifier(value)
}
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Format, PartialEq)]
#[repr(u16)]
pub enum Key {
Escape,
F1,
Expand Down Expand Up @@ -114,6 +106,12 @@ pub enum Key {
VolumeDown,
}

impl<L: LayerIndex> From<Key> for Action<L> {
fn from(from: Key) -> Action<L> {
Action::Key(from)
}
}

impl From<Key> for Keyboard {
fn from(from: Key) -> Keyboard {
match from {
Expand Down Expand Up @@ -206,6 +204,107 @@ impl From<Key> for Keyboard {
}
}

#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Default, Format, PartialEq)]
#[repr(u8)]
pub enum Modifier {
#[default]
None = 0,
LeftShift = 1 << 0,
LeftControl = 1 << 1,
LeftAlt = 1 << 2,
LeftGUI = 1 << 3,
RightShift = 1 << 4,
RightControl = 1 << 5,
RightAlt = 1 << 6,
RightGUI = 1 << 7,
}

impl<L: LayerIndex> From<Modifier> for Action<L> {
fn from(from: Modifier) -> Self {
match from {
Modifier::LeftControl => Action::Key(Key::LeftControl),
Modifier::LeftShift => Action::Key(Key::LeftShift),
Modifier::LeftAlt => Action::Key(Key::LeftAlt),
Modifier::LeftGUI => Action::Key(Key::LeftGUI),
Modifier::RightControl => Action::Key(Key::RightControl),
Modifier::RightShift => Action::Key(Key::RightShift),
Modifier::RightAlt => Action::Key(Key::RightAlt),
Modifier::RightGUI => Action::Key(Key::RightGUI),
_ => Action::None,
}
}
}

#[derive(Clone, Copy, Debug, Format)]
pub struct ModifiedKey(pub u16);

impl ModifiedKey {
pub fn get_modifiers(self) -> [Modifier; 8] {
let mut mods = [Default::default(); 8];
let modifier_bits = self.0 >> 8;
mods.iter_mut().enumerate().for_each(|(i, m)| {
let mask = 1 << i;
if (modifier_bits & mask) != 0 {
*m = unsafe { mem::transmute::<u8, Modifier>(mask as u8) };
}
});
mods
}

pub fn get_key(self) -> Key {
unsafe { mem::transmute(self.0 & 0x00FF) }
}
}

macro_rules! LS {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::LeftShift as u16) << 8)
};
}

macro_rules! LC {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::LeftControl as u16) << 8)
};
}

macro_rules! LA {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::LeftAlt as u16) << 8)
};
}

macro_rules! LG {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::LeftGUI as u16) << 8)
};
}

macro_rules! RS {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::RightShift as u16) << 8)
};
}

macro_rules! RC {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::RightControl as u16) << 8)
};
}

macro_rules! RA {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::RightAlt as u16) << 8)
};
}

macro_rules! RG {
($key:expr) => {
ModifiedKey($key as u16 | (Modifier::RightGUI as u16) << 8)
};
}

#[allow(dead_code, clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, Format, PartialEq)]
pub enum Control {
Expand All @@ -218,7 +317,7 @@ pub enum Control {
RGBDirectionToggle,
}

pub trait LayerIndex: Copy + Default + PartialEq + PartialOrd + Format + Into<usize> {}
pub trait LayerIndex: Copy + Default + PartialEq + PartialOrd + Enum + Format {}

#[derive(Clone, Copy, Debug, Default, Format, PartialEq)]
pub enum Edge {
Expand Down
38 changes: 17 additions & 21 deletions src/keyboard/default/layout/default.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(dead_code)]
use defmt::Format;
use enum_map::enum_map;
use enum_map::{enum_map, Enum};

use crate::{
key::{
Expand All @@ -12,9 +11,7 @@ use crate::{
rotary::Direction,
};

pub const LAYER_COUNT: usize = 2;

#[derive(Clone, Copy, Default, Format, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Default, Enum, Format, PartialEq, PartialOrd)]
pub enum Layer {
#[default]
Base,
Expand All @@ -23,36 +20,35 @@ pub enum Layer {

impl LayerIndex for Layer {}

impl From<Layer> for usize {
fn from(value: Layer) -> usize {
value as usize
}
}

#[rustfmt::skip]
pub fn get_input_map() -> InputMap<{ <super::super::Keyboard as Configurator>::LAYER_COUNT }, { <super::super::Keyboard as Configurator>::KEY_MATRIX_ROW_COUNT }, { <super::super::Keyboard as Configurator>::KEY_MATRIX_COL_COUNT }, <super::super::Keyboard as Configurator>::Layer> {
pub fn get_input_map() -> InputMap<
{ <super::super::Keyboard as Configurator>::LAYER_COUNT },
{ <super::super::Keyboard as Configurator>::KEY_MATRIX_ROW_COUNT },
{ <super::super::Keyboard as Configurator>::KEY_MATRIX_COL_COUNT },
<super::super::Keyboard as Configurator>::Layer,
> {
#[rustfmt::skip]
InputMap::new(
[
[
enum_map! {
Layer::Base => [
[K(Key::A), K(Key::B)],
[___________, LM(Layer::Function1)],
],
[
Layer::Function1 => [
[K(Key::C), K(Key::D)],
[C(Control::RGBAnimationNext), ___________],
],
],
[
enum_map! {
},
enum_map! {
Layer::Base => enum_map! {
Direction::Clockwise => C(Control::RGBBrightnessUp),
Direction::CounterClockwise => C(Control::RGBBrightnessDown),
_ => ___________,
},
enum_map! {
Layer::Function1 => enum_map! {
Direction::Clockwise => C(Control::RGBSpeedUp),
Direction::CounterClockwise => C(Control::RGBSpeedDown),
_ => ___________,
},
],
},
)
}
2 changes: 0 additions & 2 deletions src/keyboard/default/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ mod default;
#[cfg(layout = "default")]
use default as selected_layout;

pub use selected_layout::LAYER_COUNT;

pub use selected_layout::Layer;

pub use selected_layout::get_input_map;
42 changes: 19 additions & 23 deletions src/keyboard/kb_dev/layout/default.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(dead_code)]
use defmt::Format;
use enum_map::enum_map;
use enum_map::{enum_map, Enum};

use crate::{
key::{
Expand All @@ -12,9 +11,7 @@ use crate::{
rotary::Direction,
};

pub const LAYER_COUNT: usize = 3;

#[derive(Clone, Copy, Default, Format, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Default, Enum, Format, PartialEq, PartialOrd)]
pub enum Layer {
#[default]
Base,
Expand All @@ -24,54 +21,53 @@ pub enum Layer {

impl LayerIndex for Layer {}

impl From<Layer> for usize {
fn from(value: Layer) -> usize {
value as usize
}
}

#[rustfmt::skip]
pub fn get_input_map() -> InputMap<{ <super::super::Keyboard as Configurator>::LAYER_COUNT }, { <super::super::Keyboard as Configurator>::KEY_MATRIX_ROW_COUNT }, { <super::super::Keyboard as Configurator>::KEY_MATRIX_COL_COUNT }, <super::super::Keyboard as Configurator>::Layer> {
pub fn get_input_map() -> InputMap<
{ <super::super::Keyboard as Configurator>::LAYER_COUNT },
{ <super::super::Keyboard as Configurator>::KEY_MATRIX_ROW_COUNT },
{ <super::super::Keyboard as Configurator>::KEY_MATRIX_COL_COUNT },
<super::super::Keyboard as Configurator>::Layer,
> {
#[rustfmt::skip]
InputMap::new(
[
[
enum_map! {
Layer::Base => [
[K(Key::Escape), K(Key::Keyboard1), K(Key::Keyboard2), K(Key::Keyboard3), K(Key::Keyboard4), K(Key::Keyboard5), K(Key::Keyboard6), K(Key::Keyboard7), K(Key::Keyboard8), K(Key::Keyboard9), K(Key::Keyboard0), K(Key::Minus), K(Key::Equal), K(Key::DeleteBackspace), K(Key::DeleteForward)],
[K(Key::Tab), K(Key::Q), K(Key::W), K(Key::E), K(Key::R), K(Key::T), K(Key::Y), K(Key::U), K(Key::I), K(Key::O), K(Key::P), K(Key::LeftBrace), K(Key::RightBrace), K(Key::Backslash), K(Key::Home)],
[K(Key::CapsLock), K(Key::A), K(Key::S), K(Key::D), K(Key::F), K(Key::G), K(Key::H), K(Key::J), K(Key::K), K(Key::L), K(Key::Semicolon), K(Key::Apostrophe), ___________, K(Key::ReturnEnter), K(Key::PageUp)],
[K(Key::LeftShift), K(Key::Z), K(Key::X), K(Key::C), K(Key::V), K(Key::B), K(Key::N), K(Key::M), K(Key::Comma), K(Key::Dot), K(Key::ForwardSlash), ___________, K(Key::RightShift), K(Key::UpArrow), K(Key::PageDown)],
[K(Key::LeftControl), K(Key::LeftAlt), K(Key::LeftGUI), ___________, ___________, ___________, K(Key::Space), ___________, ___________, ___________, LM(Layer::Function1), K(Key::RightAlt), K(Key::LeftArrow), K(Key::DownArrow), K(Key::RightArrow)],
],
[
Layer::Function1 => [
[K(Key::Grave), K(Key::F1), K(Key::F2), K(Key::F3), K(Key::F4), K(Key::F5), K(Key::F6), K(Key::F7), K(Key::F8), K(Key::F9), K(Key::F10), K(Key::F11), K(Key::F12), ___________, ___________],
[___________, C(Control::RGBAnimationNext), C(Control::RGBSpeedUp), C(Control::RGBBrightnessUp), ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
[___________, C(Control::RGBAnimationPrevious), C(Control::RGBSpeedDown), C(Control::RGBBrightnessDown), ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
[K(Key::LeftShift), ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, K(Key::RightShift), ___________, ___________],
[K(Key::LeftControl), K(Key::LeftAlt), K(Key::LeftGUI), ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, LM(Layer::Function2), ___________, ___________, ___________],
],
[
Layer::Function2 => [
[___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
[___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
[___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
[___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
[___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________, ___________],
],
],
[
enum_map! {
},
enum_map! {
Layer::Base => enum_map! {
Direction::Clockwise => K(Key::VolumeUp),
Direction::CounterClockwise => K(Key::VolumeDown),
_ => ___________,
},
enum_map! {
Layer::Function1 => enum_map! {
Direction::Clockwise => C(Control::RGBBrightnessUp),
Direction::CounterClockwise => C(Control::RGBBrightnessDown),
_ => ___________,
},
enum_map! {
Layer::Function2 => enum_map! {
Direction::Clockwise => C(Control::RGBSpeedUp),
Direction::CounterClockwise => C(Control::RGBSpeedDown),
_ => ___________,
},
],
},
)
}
2 changes: 0 additions & 2 deletions src/keyboard/kb_dev/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ mod default;
#[cfg(layout = "default")]
use default as selected_layout;

pub use selected_layout::LAYER_COUNT;

pub use selected_layout::Layer;

pub use selected_layout::get_input_map;
2 changes: 1 addition & 1 deletion src/keyboard/kb_dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const ENABLE_RGB_MATRIX: bool = true;
pub struct Keyboard {}

impl Configurator for Keyboard {
const NAME: &str = "kb_dev";
const NAME: &str = "kb-dev";

const KEY_MATRIX_ROW_COUNT: usize = 5;
const KEY_MATRIX_COL_COUNT: usize = 15;
Expand Down
11 changes: 5 additions & 6 deletions src/keyboard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::cell::RefCell;

use alloc::rc::Rc;
use core::{cell::RefCell, mem};
use hal::{fugit::HertzU32, gpio, pac, pio, pwm};
use rtic_sync::arbiter::Arbiter;
use ssd1306::prelude::I2CInterface;
Expand Down Expand Up @@ -87,8 +86,8 @@ impl Configuration {
pub trait Configurator {
const NAME: &str;

const LAYER_COUNT: usize = selected_keyboard::layout::LAYER_COUNT;
type Layer: LayerIndex = selected_keyboard::layout::Layer;
const LAYER_COUNT: usize = mem::variant_count::<Self::Layer>();

const KEY_MATRIX_ROW_COUNT: usize;
const KEY_MATRIX_COL_COUNT: usize;
Expand All @@ -111,9 +110,9 @@ pub trait Configurator {
);

fn get_input_map() -> InputMap<
{ selected_keyboard::layout::LAYER_COUNT },
{ selected_keyboard::Keyboard::KEY_MATRIX_ROW_COUNT },
{ selected_keyboard::Keyboard::KEY_MATRIX_COL_COUNT },
{ <selected_keyboard::Keyboard as Configurator>::LAYER_COUNT },
{ <selected_keyboard::Keyboard as Configurator>::KEY_MATRIX_ROW_COUNT },
{ <selected_keyboard::Keyboard as Configurator>::KEY_MATRIX_COL_COUNT },
selected_keyboard::layout::Layer,
> {
selected_keyboard::layout::get_input_map()
Expand Down
Loading

0 comments on commit bf042be

Please sign in to comment.