From 2c694ca104a63914e67c8337f7ee22ad2809ef8f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 22 Feb 2024 23:40:19 +0100 Subject: [PATCH] Starts refactoring --- .cargo/config.toml | 2 +- Cargo.toml | 1 + src/dispatch.rs | 55 +++++++++ src/keyboard.rs | 290 +++++++++++++++++++++++++++++++++++++++++++ src/keypress.rs | 52 ++++++++ src/keysend.rs | 14 +++ src/main.rs | 298 ++------------------------------------------- 7 files changed, 422 insertions(+), 290 deletions(-) create mode 100644 src/dispatch.rs create mode 100644 src/keyboard.rs create mode 100644 src/keypress.rs create mode 100644 src/keysend.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 69c44f7..5ed2769 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,7 @@ [unstable] # enabling these breaks the float tests during linking, with intrinsics # duplicated between embassy-rp and compilter_builtins -# build-std = ["core"] +build-std = ["core"] # build-std-features = ["panic_immediate_abort"] [target.'cfg(all(target_arch = "arm", target_os = "none"))'] diff --git a/Cargo.toml b/Cargo.toml index 28d6629..46db5b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ pio-proc = "0.2" pio = "0.2.1" rand = { version = "0.8.5", default-features = false } embassy-usb-driver = "0.1.0" +enum_dispatch = "0.3.12" [profile.release] debug = 2 diff --git a/src/dispatch.rs b/src/dispatch.rs new file mode 100644 index 0000000..c2862a0 --- /dev/null +++ b/src/dispatch.rs @@ -0,0 +1,55 @@ +//! Module: dispatch.rs +//! +//! This module translates a series of keypresses/modifiers into a macro. +//! +//! It analyzes the sequence of keypresses and associated modifiers captured by the keypress module to determine +//! the corresponding macro action. This could involve executing a predefined macro or custom action based on +//! the input sequence. +//! +//! Example Usage: +//! ``` +//! use dispatch::DispatchManager; +//! +//! let mut dispatch_manager = DispatchManager::new(); +//! let macro_action = dispatch_manager.process_keypresses(); // Process captured keypresses and get the corresponding macro action +//! ``` +use enum_dispatch::enum_dispatch; + +use crate::keyboard::KeyCode; + +#[enum_dispatch] +enum ActionsTypes { + /// Placeholder no-op action + NoOp, + // /// List of actions to be performed + // Macro, + // /// Just one keypress + // Key, + // /// Chord modifier, will affect the other key pressed + // Chord, + // /// Will act as a Chord unless just pressed (Then acts as a Macro) + // ChordMacro, + // /// Will act as a Chord unless just pressed (Then acts as a Key) + // ChordKey, +} + +#[enum_dispatch(ActionTypes)] +trait Act { + async fn launch(&self); +} + +struct Key { + key: KeyCode, +} + +impl Act for Key { + async fn launch(&self) { + send_keypress(self.key); + } +} + +struct NoOp {} + +impl Act for NoOp { + async fn launch(&self) {} +} diff --git a/src/keyboard.rs b/src/keyboard.rs new file mode 100644 index 0000000..fe0de3f --- /dev/null +++ b/src/keyboard.rs @@ -0,0 +1,290 @@ +//! Module: keyboard.rs +//! +//! This module handles the layout and retrieves the correct keypress or combination for a given letter. +//! +//! It provides functionality for mapping letters to keypresses or combinations, considering modifiers like +//! shift, control, alt, etc., and managing the keyboard layout. +//! +//! Example Usage: +//! ``` +//! use keyboard::Keyboard; +//! +//! let mut keyboard = Keyboard::new(); +//! let keypress = keyboard.get_keypress('A'); // Retrieves the keypress for the letter 'A' +//! ``` + +/// A keypress, with modifiers +pub struct RichKey { + key: KeyCode, + modifiers: [KeyCode; 8], +} + +/// Action types, that a macro can perform +pub enum ActionType { + /// No action performed + NoOp, + /// Wait a timing + Wait(usize), + /// Press a key + Keypress(RichKey), +} + +/// A type alias for a macro +pub type Macro = &'static [RichKey]; + +/// Define a key code according to the HID specification. Their names +/// correspond to the american QWERTY layout. +#[allow(unused)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[repr(u8)] +pub enum KeyCode { + /// The "no" key, a placeholder to express nothing. + No = 0x00, + /// Error if too much keys are pressed at the same time. + ErrorRollOver, + /// The POST fail error. + PostFail, + /// An undefined error occured. + ErrorUndefined, + /// `a` and `A`. + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, // 0x10 + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + /// `1` and `!`. + Kb1, + /// `2` and `@`. + Kb2, + /// `3` and `#`. + Kb3, // 0x20 + /// `4` and `$`. + Kb4, + /// `5` and `%`. + Kb5, + /// `6` and `^`. + Kb6, + /// `7` and `&`. + Kb7, + /// `8` and `*`. + Kb8, + /// `9` and `(`. + Kb9, + /// `0` and `)`. + Kb0, + Enter, + Escape, + BSpace, + Tab, + Space, + /// `-` and `_`. + Minus, + /// `=` and `+`. + Equal, + /// `[` and `{`. + LBracket, + /// `]` and `}`. + RBracket, // 0x30 + /// `\` and `|`. + Bslash, + /// Non-US `#` and `~` (Typically near the Enter key). + NonUsHash, + /// `;` and `:`. + SColon, + /// `'` and `"`. + Quote, + // How to have ` as code? + /// \` and `~`. + Grave, + /// `,` and `<`. + Comma, + /// `.` and `>`. + Dot, + /// `/` and `?`. + Slash, + CapsLock, + F1, + F2, + F3, + F4, + F5, + F6, + F7, // 0x40 + F8, + F9, + F10, + F11, + F12, + PScreen, + ScrollLock, + Pause, + Insert, + Home, + PgUp, + Delete, + End, + PgDown, + Right, + Left, // 0x50 + Down, + Up, + NumLock, + /// Keypad `/` + KpSlash, + /// Keypad `*` + KpAsterisk, + /// Keypad `-`. + KpMinus, + /// Keypad `+`. + KpPlus, + /// Keypad enter. + KpEnter, + /// Keypad 1. + Kp1, + Kp2, + Kp3, + Kp4, + Kp5, + Kp6, + Kp7, + Kp8, // 0x60 + Kp9, + Kp0, + KpDot, + /// Non-US `\` and `|` (Typically near the Left-Shift key) + NonUsBslash, + Application, // 0x65 + /// not a key, used for errors + Power, + /// Keypad `=`. + KpEqual, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F20, + F21, // 0x70 + F22, + F23, + F24, + Execute, + Help, + Menu, + Select, + Stop, + Again, + Undo, + Cut, + Copy, + Paste, + Find, + Mute, + VolUp, // 0x80 + VolDown, + /// Deprecated. + LockingCapsLock, + /// Deprecated. + LockingNumLock, + /// Deprecated. + LockingScrollLock, + /// Keypad `,`, also used for the brazilian keypad period (.) key. + KpComma, + /// Used on AS/400 keyboard + KpEqualSign, + Intl1, + Intl2, + Intl3, + Intl4, + Intl5, + Intl6, + Intl7, + Intl8, + Intl9, + Lang1, // 0x90 + Lang2, + Lang3, + Lang4, + Lang5, + Lang6, + Lang7, + Lang8, + Lang9, + AltErase, + SysReq, + Cancel, + Clear, + Prior, + Return, + Separator, + Out, // 0xA0 + Oper, + ClearAgain, + CrSel, + ExSel, + + // According to QMK, 0xA5-0xDF are not usable on modern keyboards + + // Modifiers + /// Left Control. + LCtrl = 0xE0, + /// Left Shift. + LShift, + /// Left Alt. + LAlt, + /// Left GUI (the Windows key). + LGui, + /// Right Control. + RCtrl, + /// Right Shift. + RShift, + /// Right Alt (or Alt Gr). + RAlt, + /// Right GUI (the Windows key). + RGui, // 0xE7 + + // Unofficial + MediaPlayPause = 0xE8, + MediaStopCD, + MediaPreviousSong, + MediaNextSong, + MediaEjectCD, + MediaVolUp, + MediaVolDown, + MediaMute, + MediaWWW, // 0xF0 + MediaBack, + MediaForward, + MediaStop, + MediaFind, + MediaScrollUp, + MediaScrollDown, + MediaEdit, + MediaSleep, + MediaCoffee, + MediaRefresh, + MediaCalc, // 0xFB +} diff --git a/src/keypress.rs b/src/keypress.rs new file mode 100644 index 0000000..810a2ab --- /dev/null +++ b/src/keypress.rs @@ -0,0 +1,52 @@ +//! Module: keypress.rs +//! +//! This module is responsible for gathering keypresses and sending them to the dispatch module for analysis. +//! +//! It provides functionality for capturing keypress events from the physical keyboard or any other input source, +//! accumulating them, and passing them to the dispatch module for further processing. +//! +//! Example Usage: +//! ``` +//! use keypress::KeypressManager; +//! +//! let mut keypress_manager = KeypressManager::new(); +//! keypress_manager.start_capture(); // Start capturing keypresses +//! +//! // Somewhere in the loop... +//! keypress_manager.capture_keypress('A'); // Captures a keypress event for the letter 'A' +//! ``` + +use core::sync::atomic::AtomicU32; +pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0); + +#[repr(u32)] +pub(crate) enum Keyboard { + A1 = 1, + A2 = 1 << 1, + A3 = 1 << 2, + A4 = 1 << 3, + B1 = 1 << 4, + B2 = 1 << 5, + B3 = 1 << 6, + B4 = 1 << 7, + C1 = 1 << 8, + C2 = 1 << 9, + C3 = 1 << 10, + C4 = 1 << 11, +} + +#[repr(u32)] +enum KeyboardMask { + A1 = 0b111111111111 - (1 << 0), + A2 = 0b111111111111 - (1 << 1), + A3 = 0b111111111111 - (1 << 2), + A4 = 0b111111111111 - (1 << 3), + B1 = 0b111111111111 - (1 << 4), + B2 = 0b111111111111 - (1 << 5), + B3 = 0b111111111111 - (1 << 6), + B4 = 0b111111111111 - (1 << 7), + C1 = 0b111111111111 - (1 << 8), + C2 = 0b111111111111 - (1 << 9), + C3 = 0b111111111111 - (1 << 10), + C4 = 0b111111111111 - (1 << 11), +} diff --git a/src/keysend.rs b/src/keysend.rs new file mode 100644 index 0000000..8923cf4 --- /dev/null +++ b/src/keysend.rs @@ -0,0 +1,14 @@ +//! Module: keysend.rs +//! +//! This module handles the sending of keypresses to the computer. +//! +//! It provides functionality for simulating keypress events and sending them to the computer's operating system +//! or other applications. This can involve low-level system calls or interacting with platform-specific APIs. +//! +//! Example Usage: +//! ``` +//! use keysend::KeysendManager; +//! +//! let mut keysend_manager = KeysendManager::new(); +//! keysend_manager.send_keypress('A'); // Simulate sending a keypress event for the letter 'A' +//! ``` diff --git a/src/main.rs b/src/main.rs index 91a4ca5..0ebb64e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ #![no_std] #![no_main] +mod dispatch; +mod keyboard; +mod keypress; +mod keysend; -use core::sync::atomic::{AtomicBool, AtomicU32, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; -// use embassy_sync::blocking_mutex::Mutex as BlockingMutex; - -// use core::cell::{RefCell, UnsafeCell}; +use crate::keyboard::KeyCode; +use crate::keypress::{Keyboard, KEYMASK}; use defmt::*; use embassy_executor::Spawner; use embassy_futures::join::join; @@ -13,300 +16,17 @@ use embassy_rp::bind_interrupts; use embassy_rp::gpio::{Input, Pull}; use embassy_rp::peripherals::USB; use embassy_rp::usb::{Driver, InterruptHandler}; -use embassy_usb::class::hid::{HidReaderWriter, HidWriter, ReportId, RequestHandler, State}; +use embassy_usb::class::hid::{HidReaderWriter, ReportId, RequestHandler, State}; use embassy_usb::control::OutResponse; use embassy_usb::{Builder, Config, Handler}; use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor}; + use {defmt_rtt as _, panic_probe as _}; bind_interrupts!(struct Irqs { USBCTRL_IRQ => InterruptHandler; }); -static KEYMASK: AtomicU32 = AtomicU32::new(0); -#[repr(u32)] -enum Keyboard { - A1 = 0b1, - A2 = 0b10, - A3 = 0b100, - A4 = 0b1000, - B1 = 0b10000, - B2 = 0b100000, - B3 = 0b1000000, - B4 = 0b10000000, - C1 = 0b100000000, - C2 = 0b1000000000, - C3 = 0b10000000000, - C4 = 0b100000000000, -} -#[repr(u32)] -enum KeyboardMask { - A1 = 0b0, - A2 = 0b01, - A3 = 0b011, - A4 = 0b0111, - B1 = 0b01111, - B2 = 0b011111, - B3 = 0b0111111, - B4 = 0b01111111, - C1 = 0b011111111, - C2 = 0b0111111111, - C3 = 0b01111111111, - C4 = 0b011111111111, -} -#[repr(u8)] -pub enum KeyCode { - /// The "no" key, a placeholder to express nothing. - No = 0x00, - /// Error if too much keys are pressed at the same time. - ErrorRollOver, - /// The POST fail error. - PostFail, - /// An undefined error occured. - ErrorUndefined, - /// `a` and `A`. - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, // 0x10 - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - /// `1` and `!`. - Kb1, - /// `2` and `@`. - Kb2, - /// `3` and `#`. - Kb3, // 0x20 - /// `4` and `$`. - Kb4, - /// `5` and `%`. - Kb5, - /// `6` and `^`. - Kb6, - /// `7` and `&`. - Kb7, - /// `8` and `*`. - Kb8, - /// `9` and `(`. - Kb9, - /// `0` and `)`. - Kb0, - Enter, - Escape, - BSpace, - Tab, - Space, - /// `-` and `_`. - Minus, - /// `=` and `+`. - Equal, - /// `[` and `{`. - LBracket, - /// `]` and `}`. - RBracket, // 0x30 - /// `\` and `|`. - Bslash, - /// Non-US `#` and `~` (Typically near the Enter key). - NonUsHash, - /// `;` and `:`. - SColon, - /// `'` and `"`. - Quote, - // How to have ` as code? - /// \` and `~`. - Grave, - /// `,` and `<`. - Comma, - /// `.` and `>`. - Dot, - /// `/` and `?`. - Slash, - CapsLock, - F1, - F2, - F3, - F4, - F5, - F6, - F7, // 0x40 - F8, - F9, - F10, - F11, - F12, - PScreen, - ScrollLock, - Pause, - Insert, - Home, - PgUp, - Delete, - End, - PgDown, - Right, - Left, // 0x50 - Down, - Up, - NumLock, - /// Keypad `/` - KpSlash, - /// Keypad `*` - KpAsterisk, - /// Keypad `-`. - KpMinus, - /// Keypad `+`. - KpPlus, - /// Keypad enter. - KpEnter, - /// Keypad 1. - Kp1, - Kp2, - Kp3, - Kp4, - Kp5, - Kp6, - Kp7, - Kp8, // 0x60 - Kp9, - Kp0, - KpDot, - /// Non-US `\` and `|` (Typically near the Left-Shift key) - NonUsBslash, - Application, // 0x65 - /// not a key, used for errors - Power, - /// Keypad `=`. - KpEqual, - F13, - F14, - F15, - F16, - F17, - F18, - F19, - F20, - F21, // 0x70 - F22, - F23, - F24, - Execute, - Help, - Menu, - Select, - Stop, - Again, - Undo, - Cut, - Copy, - Paste, - Find, - Mute, - VolUp, // 0x80 - VolDown, - /// Deprecated. - LockingCapsLock, - /// Deprecated. - LockingNumLock, - /// Deprecated. - LockingScrollLock, - /// Keypad `,`, also used for the brazilian keypad period (.) key. - KpComma, - /// Used on AS/400 keyboard - KpEqualSign, - Intl1, - Intl2, - Intl3, - Intl4, - Intl5, - Intl6, - Intl7, - Intl8, - Intl9, - Lang1, // 0x90 - Lang2, - Lang3, - Lang4, - Lang5, - Lang6, - Lang7, - Lang8, - Lang9, - AltErase, - SysReq, - Cancel, - Clear, - Prior, - Return, - Separator, - Out, // 0xA0 - Oper, - ClearAgain, - CrSel, - ExSel, - - // According to QMK, 0xA5-0xDF are not usable on modern keyboards - - // Modifiers - /// Left Control. - LCtrl = 0xE0, - /// Left Shift. - LShift, - /// Left Alt. - LAlt, - /// Left GUI (the Windows key). - LGui, - /// Right Control. - RCtrl, - /// Right Shift. - RShift, - /// Right Alt (or Alt Gr). - RAlt, - /// Right GUI (the Windows key). - RGui, // 0xE7 - - // Unofficial - MediaPlayPause = 0xE8, - MediaStopCD, - MediaPreviousSong, - MediaNextSong, - MediaEjectCD, - MediaVolUp, - MediaVolDown, - MediaMute, - MediaWWW, // 0xF0 - MediaBack, - MediaForward, - MediaStop, - MediaFind, - MediaScrollUp, - MediaScrollDown, - MediaEdit, - MediaSleep, - MediaCoffee, - MediaRefresh, - MediaCalc, // 0xFB -} - #[embassy_executor::main] async fn main(_spawner: Spawner) { let p = embassy_rp::init(Default::default());