Nice working with Dynamic Dispatch

This commit is contained in:
Dandaedre 2024-02-24 02:37:30 +01:00
parent 44c6c764a8
commit 440515cf47
6 changed files with 92 additions and 56 deletions

View File

@ -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;
}
}

View File

@ -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.

View File

@ -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<PINTYPE>(
mut pin: PINTYPE,
pin: PINTYPE,
corresponding_key: KeypadKey,
sender: &ChannelType,
) where
@ -48,12 +41,12 @@ pub(crate) async fn await_key<PINTYPE>(
// 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<PINTYPE>(pin: PINTYPE, corresponding_key: KeypadKey)
where
PINTYPE: embedded_hal_async::digital::Wait,
{
let _ = corresponding_key;
let _ = pin;
todo!()
// loop {
// // Wait for keypressed

View File

@ -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() {

View File

@ -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,

View File

@ -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<MutexType, KeyboardReport, CHANNEL_SIZE>;
pub(crate) type WriterType = HidWriter<'static, Driver<'static, USB>, 8>;
// pub(crate) type WriterType = HidWriter<'static, Driver<'static, USB>, 8>;
// static LOCKED_WRITER: Mutex<CriticalSectionRawMutex, RefCell<Option<WriterType>>> =
// Mutex::new(RefCell::new(None));
@ -93,7 +94,7 @@ async fn main(_spawner: Spawner) {
let (reader, mut writer) = hid.split();
// let mut channel = Channel::<MutexType, KeyboardReport, CHANNEL_SIZE>::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");