Regulation loop

This commit is contained in:
Dandaedre 2024-02-26 03:36:19 +01:00
parent 0c37b271fc
commit 54b07a6e45
2 changed files with 52 additions and 26 deletions

View File

@ -18,11 +18,12 @@
use embassy_rp::gpio::{AnyPin, Input, Pull};
use embassy_time::Timer;
use usbd_hid::descriptor::KeyboardReport;
use crate::{
dispatch::{keydispatch_press, keydispatch_release},
layout::KeypadKey,
ChannelType, DEBOUNCE_TIMER,
layout::{KeypadKey, A1},
ChannelType, CHORD_TIMER, DEBOUNCE_TIMER, RETRY_CHORD,
};
// pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0);
@ -46,18 +47,33 @@ pub(crate) async fn await_key(pin: AnyPin, corresponding_key: KeypadKey, sender:
}
}
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
// // Update modkeys
// // Regulation loop
// // If released within: Call dispatch
// // Wait keyreleased
// }
pub(crate) async fn await_chord(pin: AnyPin, corresponding_key: KeypadKey, 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;
}
// Wait keyrelease
if !(was_tap) {
signal_pin.wait_for_low().await;
}
A1.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, join_array};
use embassy_futures::join::{join, join3, join4, join_array};
use embassy_rp::bind_interrupts;
use embassy_rp::peripherals::USB;
use embassy_rp::usb::{Driver, InterruptHandler};
@ -19,6 +19,7 @@ use embassy_sync::channel::Channel;
use embassy_usb::class::hid::{HidReaderWriter, ReportId, RequestHandler, State};
use embassy_usb::control::OutResponse;
use embassy_usb::{Builder, Config, Handler};
use keypress::await_chord;
use usbd_hid::descriptor::{KeyboardReport, SerializedDescriptor};
use crate::keypress::await_key;
@ -36,6 +37,8 @@ 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;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
@ -106,29 +109,36 @@ async fn main(_spawner: Spawner) {
}
};
let in_fut = [
await_key(p.PIN_1.into(), layout::KeypadKey::A1, &channel),
let keys_fut = [
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),
await_key(p.PIN_5.into(), layout::KeypadKey::B1, &channel),
await_key(p.PIN_6.into(), layout::KeypadKey::B2, &channel),
await_key(p.PIN_7.into(), layout::KeypadKey::B3, &channel),
await_key(p.PIN_8.into(), layout::KeypadKey::B4, &channel),
await_key(p.PIN_11.into(), layout::KeypadKey::C1, &channel),
await_key(p.PIN_12.into(), layout::KeypadKey::C2, &channel),
await_key(p.PIN_10.into(), layout::KeypadKey::C3, &channel),
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),
];
let out_fut = async {
reader.run(false, &request_handler).await;
};
// Run everything concurrently.
// If we had made everything `'static` above instead, we could do this using separate tasks instead.
// join(usb_fut, join3(senderloop, join_array(list_keys), out_fut)).await;
join(usb_fut, join3(senderloop, join_array(in_fut), out_fut)).await;
join(
usb_fut,
join4(
senderloop,
join_array(keys_fut),
join_array(chords_fut),
out_fut,
),
)
.await;
}
struct MyRequestHandler {}