First draft of the sender model
This commit is contained in:
parent
f3f17b7126
commit
18e298d7da
|
|
@ -13,7 +13,7 @@
|
|||
//! 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;
|
||||
use crate::{keysend::send_keypress, ChannelType};
|
||||
use enum_dispatch::enum_dispatch;
|
||||
|
||||
use crate::{keyboard::KeyCode, layout::KeypadKey};
|
||||
|
|
@ -47,7 +47,7 @@ enum ActionsTypes {
|
|||
|
||||
#[enum_dispatch(ActionTypes)]
|
||||
trait Act {
|
||||
async fn launch(&self);
|
||||
async fn launch(&self, sender: &ChannelType);
|
||||
}
|
||||
|
||||
struct Key {
|
||||
|
|
@ -55,13 +55,13 @@ struct Key {
|
|||
}
|
||||
|
||||
impl Act for Key {
|
||||
async fn launch(&self) {
|
||||
send_keypress(self.key);
|
||||
async fn launch(&self, sender: &ChannelType) {
|
||||
send_keypress(&[self.key], sender);
|
||||
}
|
||||
}
|
||||
|
||||
struct NoOp {}
|
||||
|
||||
impl Act for NoOp {
|
||||
async fn launch(&self) {}
|
||||
async fn launch(&self, _sender: &ChannelType) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,26 +18,40 @@
|
|||
|
||||
use core::sync::atomic::AtomicU32;
|
||||
|
||||
use defmt::info;
|
||||
use embassy_rp::{
|
||||
gpio::{Input, Pin, Pull},
|
||||
Peripherals,
|
||||
};
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
|
||||
use usbd_hid::descriptor::KeyboardReport;
|
||||
|
||||
use crate::{
|
||||
dispatch::{keydispatch_press, keydispatch_release},
|
||||
layout::KeypadKey,
|
||||
ChannelType, MutexType, CHANNEL_SIZE,
|
||||
};
|
||||
pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
pub(crate) async fn await_key<PINTYPE>(mut pin: PINTYPE, corresponding_key: KeypadKey)
|
||||
where
|
||||
PINTYPE: embedded_hal_async::digital::Wait,
|
||||
pub(crate) async fn await_key<PINTYPE>(
|
||||
mut pin: PINTYPE,
|
||||
corresponding_key: KeypadKey,
|
||||
sender: &ChannelType,
|
||||
) where
|
||||
PINTYPE: Pin,
|
||||
{
|
||||
// Set up the signal pin that will be used to trigger the keyboard.
|
||||
let mut signal_pin = Input::new(pin, Pull::None);
|
||||
|
||||
// Enable the schmitt trigger to slightly debounce.
|
||||
signal_pin.set_schmitt(true);
|
||||
loop {
|
||||
// Wait for keypress
|
||||
pin.wait_for_high().await;
|
||||
signal_pin.wait_for_high().await;
|
||||
// Call dispatch
|
||||
keydispatch_press(&corresponding_key).await;
|
||||
|
||||
// Wait for keyrelease
|
||||
pin.wait_for_low().await;
|
||||
signal_pin.wait_for_low().await;
|
||||
// Call dispatch
|
||||
keydispatch_release(&corresponding_key).await;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,22 @@
|
|||
//! keysend_manager.send_keypress('A'); // Simulate sending a keypress event for the letter 'A'
|
||||
//! ```
|
||||
|
||||
use crate::keyboard::KeyCode;
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::{CriticalSectionRawMutex, ThreadModeRawMutex},
|
||||
channel::Channel,
|
||||
};
|
||||
use usbd_hid::descriptor::KeyboardReport;
|
||||
|
||||
pub(crate) async fn send_keypress(list_key: &[KeyCode]) {
|
||||
for key in list_key.iter() {}
|
||||
use crate::{keyboard::KeyCode, ChannelType, CHANNEL_SIZE};
|
||||
|
||||
pub(crate) async fn send_keypress(list_key: &[KeyCode], sender: &ChannelType) {
|
||||
for key in list_key.iter() {
|
||||
let report = KeyboardReport {
|
||||
keycodes: [*key as u8, 0, 0, 0, 0, 0],
|
||||
leds: 0,
|
||||
modifier: 0,
|
||||
reserved: 0,
|
||||
};
|
||||
sender.send(report).await;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
28
src/main.rs
28
src/main.rs
|
|
@ -22,13 +22,18 @@ use embassy_usb::control::OutResponse;
|
|||
use embassy_usb::{Builder, Config, Handler};
|
||||
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};
|
||||
|
||||
use crate::keypress::await_key;
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
USBCTRL_IRQ => InterruptHandler<USB>;
|
||||
});
|
||||
|
||||
type WriterType = HidWriter<'static, Driver<'static, USB>, 8>;
|
||||
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>;
|
||||
// static LOCKED_WRITER: Mutex<CriticalSectionRawMutex, RefCell<Option<WriterType>>> =
|
||||
// Mutex::new(RefCell::new(None));
|
||||
|
||||
|
|
@ -85,14 +90,10 @@ async fn main(_spawner: Spawner) {
|
|||
|
||||
// Run the USB device.
|
||||
let usb_fut = usb.run();
|
||||
// Set up the signal pin that will be used to trigger the keyboard.
|
||||
let mut pin = Input::new(p.PIN_1, Pull::None);
|
||||
// Enable the schmitt trigger to slightly debounce.
|
||||
pin.set_schmitt(true);
|
||||
|
||||
let (reader, mut writer) = hid.split();
|
||||
let mut channel = Channel::<CriticalSectionRawMutex, KeyboardReport, 12>::new();
|
||||
|
||||
// let mut channel = Channel::<MutexType, KeyboardReport, CHANNEL_SIZE>::new();
|
||||
let mut channel = ChannelType::new();
|
||||
let senderloop = async {
|
||||
loop {
|
||||
info!("Awayting new messages");
|
||||
|
|
@ -107,11 +108,12 @@ async fn main(_spawner: Spawner) {
|
|||
|
||||
let in_fut = async {
|
||||
loop {
|
||||
info!("Waiting for HIGH on pin 1");
|
||||
pin.wait_for_high().await;
|
||||
info!("HIGH DETECTED");
|
||||
pin.wait_for_low().await;
|
||||
info!("LOW DETECTED");
|
||||
await_key(p.PIN_1, layout::KeypadKey::A1, &channel);
|
||||
// info!("Waiting for HIGH on pin 1");
|
||||
// pin.wait_for_high().await;
|
||||
// info!("HIGH DETECTED");
|
||||
// pin.wait_for_low().await;
|
||||
// info!("LOW DETECTED");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -121,7 +123,7 @@ async fn main(_spawner: Spawner) {
|
|||
|
||||
// Run everything concurrently.
|
||||
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
|
||||
join(usb_fut, join3(in_fut, out_fut, senderloop)).await;
|
||||
join(usb_fut, join3(senderloop, in_fut, out_fut)).await;
|
||||
}
|
||||
|
||||
struct MyRequestHandler {}
|
||||
|
|
|
|||
Reference in New Issue