Chords and quickpresses

This commit is contained in:
Dandaedre 2024-02-26 04:12:31 +01:00
parent 54b07a6e45
commit 5777f09af0
2 changed files with 49 additions and 31 deletions

View File

@ -16,14 +16,16 @@
//! keypress_manager.capture_keypress('A'); // Captures a keypress event for the letter 'A'
//! ```
use core::sync::atomic::AtomicBool;
use embassy_rp::gpio::{AnyPin, Input, Pull};
use embassy_time::Timer;
use usbd_hid::descriptor::KeyboardReport;
use embassy_time::{Instant, Timer};
// use portable_atomic::AtomicBool;
use crate::{
dispatch::{keydispatch_press, keydispatch_release},
layout::{KeypadKey, A1},
ChannelType, CHORD_TIMER, DEBOUNCE_TIMER, RETRY_CHORD,
layout::KeypadKey,
ChannelType, CHORD_TIMER, DEBOUNCE_TIMER,
};
// pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0);
@ -47,33 +49,34 @@ pub(crate) async fn await_key(pin: AnyPin, corresponding_key: KeypadKey, sender:
}
}
pub(crate) async fn await_chord(pin: AnyPin, corresponding_key: KeypadKey, sender: &ChannelType) {
pub(crate) async fn await_chord(
pin: AnyPin,
corresponding_key: &KeypadKey,
corresponding_mod: &AtomicBool,
sender: &ChannelType,
) {
let mut signal_pin = Input::new(pin, Pull::None);
loop {
// Wait for keypress
signal_pin.wait_for_high().await;
// Update modkeys
A1.store(true, portable_atomic::Ordering::Relaxed);
let mut was_tap = false;
// Regulation loop
for _ in 0..RETRY_CHORD {
// The pin was released fastly, it's a tap
if signal_pin.is_low() {
keydispatch_press(&corresponding_key, sender).await;
Timer::after_millis(DEBOUNCE_TIMER).await;
keydispatch_release(&corresponding_key, sender).await;
was_tap = true;
break;
}
Timer::after_millis(CHORD_TIMER).await;
}
// keydispatch_press(&KeypadKey::A4, sender).await;
// keydispatch_release(&KeypadKey::A4, sender).await;
let start_time = Instant::now();
// Key is pressed
corresponding_mod.store(true, portable_atomic::Ordering::Relaxed);
Timer::after_millis(DEBOUNCE_TIMER).await;
// Wait keyrelease
if !(was_tap) {
signal_pin.wait_for_low().await;
}
// Wait for release
signal_pin.wait_for_low().await;
if let Some(delta) = Instant::now().checked_duration_since(start_time) {
if delta.as_millis() < CHORD_TIMER {
keydispatch_press(corresponding_key, sender).await;
keydispatch_release(corresponding_key, sender).await;
} // If not we actually don't have to do anythin
} // We don't have to do anything on overflow
A1.store(false, portable_atomic::Ordering::Relaxed);
// Key is not pressed anymore
corresponding_mod.store(false, portable_atomic::Ordering::Relaxed);
Timer::after_millis(DEBOUNCE_TIMER).await;
}
}

View File

@ -10,7 +10,7 @@ use core::sync::atomic::{AtomicBool, Ordering};
use defmt::*;
use embassy_executor::Spawner;
use embassy_futures::join::{join, join3, join4, join_array};
use embassy_futures::join::{join, join4, join_array};
use embassy_rp::bind_interrupts;
use embassy_rp::peripherals::USB;
use embassy_rp::usb::{Driver, InterruptHandler};
@ -37,8 +37,7 @@ pub(crate) type ChannelType = Channel<MutexType, KeyboardReport, CHANNEL_SIZE>;
// static LOCKED_WRITER: Mutex<CriticalSectionRawMutex, RefCell<Option<WriterType>>> =
// Mutex::new(RefCell::new(None));
const DEBOUNCE_TIMER: u64 = 5;
const CHORD_TIMER: u64 = 10;
const RETRY_CHORD: usize = 100;
const CHORD_TIMER: u64 = 200;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
@ -110,6 +109,7 @@ async fn main(_spawner: Spawner) {
};
let keys_fut = [
// await_key(p.PIN_1.into(), layout::KeypadKey::A1, &channel),
await_key(p.PIN_2.into(), layout::KeypadKey::A2, &channel),
await_key(p.PIN_3.into(), layout::KeypadKey::A3, &channel),
await_key(p.PIN_4.into(), layout::KeypadKey::A4, &channel),
@ -121,9 +121,24 @@ async fn main(_spawner: Spawner) {
await_key(p.PIN_9.into(), layout::KeypadKey::C4, &channel),
];
let chords_fut = [
await_chord(p.PIN_1.into(), layout::KeypadKey::A1, &channel),
await_chord(p.PIN_5.into(), layout::KeypadKey::B1, &channel),
await_chord(p.PIN_11.into(), layout::KeypadKey::C1, &channel),
await_chord(
p.PIN_1.into(),
&layout::KeypadKey::A1,
&layout::A1,
&channel,
),
await_chord(
p.PIN_5.into(),
&layout::KeypadKey::B1,
&layout::B1,
&channel,
),
await_chord(
p.PIN_11.into(),
&layout::KeypadKey::C1,
&layout::C1,
&channel,
),
];
let out_fut = async {
reader.run(false, &request_handler).await;