From 440515cf47c2c332c2db5418d5f925511c8b956f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 24 Feb 2024 02:37:30 +0100 Subject: [PATCH] Nice working with Dynamic Dispatch --- src/dispatch.rs | 60 +++++++++++++++++++++++++++++++++++++------------ src/keyboard.rs | 30 ++++++++++++------------- src/keypress.rs | 21 +++++++---------- src/keysend.rs | 6 +---- src/layout.rs | 11 ++++----- src/main.rs | 20 +++++++++++++---- 6 files changed, 92 insertions(+), 56 deletions(-) diff --git a/src/dispatch.rs b/src/dispatch.rs index ce7f7ae..96dbf93 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -13,38 +13,75 @@ //! let mut dispatch_manager = DispatchManager::new(); //! let macro_action = dispatch_manager.process_keypresses(); // Process captured keypresses and get the corresponding macro action //! ``` -use crate::{keysend::send_keypress, layout::A1, layout::B1, layout::C1, ChannelType}; + +use crate::{ + keysend::send_keypress, + layout::{A1, B1, C1}, + ChannelType, +}; use enum_dispatch::enum_dispatch; use portable_atomic::Ordering; use crate::{keyboard::KeyCode, layout::KeypadKey}; -pub(crate) async fn keydispatch_press(pressed_key: &KeypadKey) { +pub(crate) async fn keydispatch_press(pressed_key: &KeypadKey, sender: &ChannelType) { // Big switchcase + let m1 = ActionsTypes::from(Key { key: KeyCode::A }); + let void = ActionsTypes::from(NoOp {}); match ( A1.load(Ordering::Relaxed), B1.load(Ordering::Relaxed), C1.load(Ordering::Relaxed), - pressed_key, + &pressed_key, ) { - (_, _, _, _) => (), + (_, _, _, &KeypadKey::A2) => m1.launch(sender), // Use a macro creating this function + + // (_, _, _, &KeypadKey::A3) => Key { key: KeyCode::B }.launch(sender), + // (_, _, _, &KeypadKey::A4) => Key { key: KeyCode::C }.launch(sender), + // (_, _, _, &KeypadKey::B2) => Key { key: KeyCode::D }.launch(sender), + // (_, _, _, &KeypadKey::B3) => Key { key: KeyCode::E }.launch(sender), + // (_, _, _, &KeypadKey::B4) => Key { key: KeyCode::F }.launch(sender), + // (_, _, _, &KeypadKey::C2) => Key { key: KeyCode::G }.launch(sender), + // (_, _, _, &KeypadKey::C3) => Key { key: KeyCode::H }.launch(sender), + // (_, _, _, &KeypadKey::C4) => Key { key: KeyCode::I }.launch(sender), + // (_, _, _, _) => Key { key: KeyCode::I }.launch(sender), + (_, _, _, _) => void.launch(sender), } + .await; // Return } -pub(crate) async fn keydispatch_release(pressed_key: &KeypadKey) { - // Big switchcase +// static MACRO1: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::A }); +// static MACRO2: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::B }); +// static MACRO3: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::C }); +// static MACRO4: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::A }); +// static MACRO5: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::B }); +// static MACRO6: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::C }); +// static MACRO7: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::A }); +// static MACRO8: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::B }); +// static MACRO9: ActionsTypes = ActionsTypes::from(Key { key: KeyCode::C }); +// static VOIDMACRO: ActionsTypes = ActionsTypes::from(NoOp {}); + +pub(crate) async fn keydispatch_release(released_key: &KeypadKey, sender: &ChannelType) { + let _ = released_key; + // Big switchcase + Key { key: KeyCode::No }.launch(sender).await; // Return } #[enum_dispatch] +trait Act { + async fn launch(&self, sender: &ChannelType); +} + +#[enum_dispatch(Act)] enum ActionsTypes { /// Placeholder no-op action NoOp, // /// List of actions to be performed // Macro, - // /// Just one keypress - // Key, + /// 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) @@ -53,18 +90,13 @@ enum ActionsTypes { // ChordKey, } -#[enum_dispatch(ActionTypes)] -trait Act { - async fn launch(&self, sender: &ChannelType); -} - struct Key { key: KeyCode, } impl Act for Key { async fn launch(&self, sender: &ChannelType) { - send_keypress(&[self.key], sender); + send_keypress(&[self.key], sender).await; } } diff --git a/src/keyboard.rs b/src/keyboard.rs index fe0de3f..616e3e4 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -14,23 +14,23 @@ //! ``` /// A keypress, with modifiers -pub struct RichKey { - key: KeyCode, - modifiers: [KeyCode; 8], -} +// 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), -} +// /// 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]; +// /// 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. diff --git a/src/keypress.rs b/src/keypress.rs index efad5eb..27c3b0e 100644 --- a/src/keypress.rs +++ b/src/keypress.rs @@ -16,24 +16,17 @@ //! keypress_manager.capture_keypress('A'); // Captures a keypress event for the letter 'A' //! ``` -use core::sync::atomic::AtomicU32; - -use embassy_rp::{ - gpio::{Input, Pin, Pull}, - Peripherals, -}; -use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel}; -use usbd_hid::descriptor::KeyboardReport; +use embassy_rp::gpio::{Input, Pin, Pull}; use crate::{ dispatch::{keydispatch_press, keydispatch_release}, layout::KeypadKey, - ChannelType, MutexType, CHANNEL_SIZE, + ChannelType, }; -pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0); +// pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0); pub(crate) async fn await_key( - mut pin: PINTYPE, + pin: PINTYPE, corresponding_key: KeypadKey, sender: &ChannelType, ) where @@ -48,12 +41,12 @@ pub(crate) async fn await_key( // Wait for keypress signal_pin.wait_for_high().await; // Call dispatch - keydispatch_press(&corresponding_key).await; + keydispatch_press(&corresponding_key, sender).await; // Wait for keyrelease signal_pin.wait_for_low().await; // Call dispatch - keydispatch_release(&corresponding_key).await; + keydispatch_release(&corresponding_key, sender).await; } } @@ -61,6 +54,8 @@ pub(crate) fn await_chord(pin: PINTYPE, corresponding_key: KeypadKey) where PINTYPE: embedded_hal_async::digital::Wait, { + let _ = corresponding_key; + let _ = pin; todo!() // loop { // // Wait for keypressed diff --git a/src/keysend.rs b/src/keysend.rs index ad5fcd8..e647535 100644 --- a/src/keysend.rs +++ b/src/keysend.rs @@ -13,13 +13,9 @@ //! keysend_manager.send_keypress('A'); // Simulate sending a keypress event for the letter 'A' //! ``` -use embassy_sync::{ - blocking_mutex::raw::{CriticalSectionRawMutex, ThreadModeRawMutex}, - channel::Channel, -}; use usbd_hid::descriptor::KeyboardReport; -use crate::{keyboard::KeyCode, ChannelType, CHANNEL_SIZE}; +use crate::{keyboard::KeyCode, ChannelType}; pub(crate) async fn send_keypress(list_key: &[KeyCode], sender: &ChannelType) { for key in list_key.iter() { diff --git a/src/layout.rs b/src/layout.rs index ecfb6b7..d2010ae 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -12,11 +12,12 @@ use core::sync::atomic::AtomicBool; pub(crate) static A1: AtomicBool = AtomicBool::new(false); pub(crate) static B1: AtomicBool = AtomicBool::new(false); pub(crate) static C1: AtomicBool = AtomicBool::new(false); -pub(crate) enum MODKEYS { - A1, - B1, - C1, -} + +// pub(crate) enum Modkeys { +// A1, +// B1, +// C1, +// } pub(crate) enum KeypadKey { A1, A2, diff --git a/src/main.rs b/src/main.rs index fee559a..f9a94b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![feature(const_trait_impl)] +#![feature(effects)] #![no_std] #![no_main] mod dispatch; @@ -12,12 +14,11 @@ use defmt::*; use embassy_executor::Spawner; use embassy_futures::join::{join, join3}; use embassy_rp::bind_interrupts; -use embassy_rp::gpio::{Input, Pull}; use embassy_rp::peripherals::USB; use embassy_rp::usb::{Driver, InterruptHandler}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::channel::Channel; -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}; @@ -33,7 +34,7 @@ bind_interrupts!(struct Irqs { pub(crate) const CHANNEL_SIZE: usize = 12; pub(crate) type MutexType = CriticalSectionRawMutex; pub(crate) type ChannelType = Channel; -pub(crate) type WriterType = HidWriter<'static, Driver<'static, USB>, 8>; +// pub(crate) type WriterType = HidWriter<'static, Driver<'static, USB>, 8>; // static LOCKED_WRITER: Mutex>> = // Mutex::new(RefCell::new(None)); @@ -93,7 +94,7 @@ async fn main(_spawner: Spawner) { let (reader, mut writer) = hid.split(); // let mut channel = Channel::::new(); - let mut channel = ChannelType::new(); + let channel = ChannelType::new(); let senderloop = async { loop { info!("Awayting new messages"); @@ -109,6 +110,17 @@ async fn main(_spawner: Spawner) { let in_fut = async { // loop { await_key(p.PIN_1, layout::KeypadKey::A1, &channel).await; + await_key(p.PIN_2, layout::KeypadKey::A2, &channel).await; + await_key(p.PIN_3, layout::KeypadKey::A3, &channel).await; + await_key(p.PIN_4, layout::KeypadKey::A4, &channel).await; + await_key(p.PIN_5, layout::KeypadKey::B1, &channel).await; + await_key(p.PIN_6, layout::KeypadKey::B2, &channel).await; + await_key(p.PIN_7, layout::KeypadKey::B3, &channel).await; + await_key(p.PIN_8, layout::KeypadKey::B4, &channel).await; + await_key(p.PIN_9, layout::KeypadKey::C1, &channel).await; + await_key(p.PIN_10, layout::KeypadKey::C2, &channel).await; + await_key(p.PIN_11, layout::KeypadKey::C3, &channel).await; + await_key(p.PIN_12, layout::KeypadKey::C4, &channel).await; // info!("Waiting for HIGH on pin 1"); // pin.wait_for_high().await; // info!("HIGH DETECTED");