diff --git a/src/keypress.rs b/src/keypress.rs index 27c3b0e..d36f8f8 100644 --- a/src/keypress.rs +++ b/src/keypress.rs @@ -16,14 +16,36 @@ //! keypress_manager.capture_keypress('A'); // Captures a keypress event for the letter 'A' //! ``` -use embassy_rp::gpio::{Input, Pin, Pull}; +use embassy_rp::{ + gpio::{Input, Pin, Pull}, + Peripherals, +}; +use embassy_time::Timer; use crate::{ dispatch::{keydispatch_press, keydispatch_release}, layout::KeypadKey, - ChannelType, + ChannelType, DEBOUNCE_TIMER, }; // pub(crate) static KEYMASK: AtomicU32 = AtomicU32::new(0); +pub(crate) async fn init_key(p: &Peripherals, corresponding_key: KeypadKey, sender: &ChannelType) { + match corresponding_key { + KeypadKey::A1 => { + await_key(p.PIN_1, KeypadKey::A1, sender); + } + KeypadKey::A2 => todo!(), + KeypadKey::A3 => todo!(), + KeypadKey::A4 => todo!(), + KeypadKey::B1 => todo!(), + KeypadKey::B2 => todo!(), + KeypadKey::B3 => todo!(), + KeypadKey::B4 => todo!(), + KeypadKey::C1 => todo!(), + KeypadKey::C2 => todo!(), + KeypadKey::C3 => todo!(), + KeypadKey::C4 => todo!(), + } +} pub(crate) async fn await_key( pin: PINTYPE, @@ -36,17 +58,18 @@ pub(crate) async fn await_key( let mut signal_pin = Input::new(pin, Pull::None); // Enable the schmitt trigger to slightly debounce. - signal_pin.set_schmitt(true); + // signal_pin.set_schmitt(true); loop { // Wait for keypress signal_pin.wait_for_high().await; // Call dispatch keydispatch_press(&corresponding_key, sender).await; - + Timer::after_millis(DEBOUNCE_TIMER).await; // Wait for keyrelease signal_pin.wait_for_low().await; // Call dispatch keydispatch_release(&corresponding_key, sender).await; + Timer::after_millis(DEBOUNCE_TIMER).await; } } diff --git a/src/main.rs b/src/main.rs index a2416c5..960c4be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use core::sync::atomic::{AtomicBool, Ordering}; use defmt::*; use embassy_executor::Spawner; -use embassy_futures::join::{join, join3, join5, join_array}; +use embassy_futures::join::{join, join3, join4, join5, join_array}; use embassy_rp::bind_interrupts; use embassy_rp::peripherals::USB; use embassy_rp::usb::{Driver, InterruptHandler}; @@ -35,6 +35,7 @@ pub(crate) type ChannelType = Channel; // pub(crate) type WriterType = HidWriter<'static, Driver<'static, USB>, 8>; // static LOCKED_WRITER: Mutex>> = // Mutex::new(RefCell::new(None)); +const DEBOUNCE_TIMER: u64 = 5; #[embassy_executor::main] async fn main(_spawner: Spawner) { @@ -105,37 +106,20 @@ async fn main(_spawner: Spawner) { } }; - // let list_keys = [ - // await_key(p.PIN_4, layout::KeypadKey::A4, &channel).await, - // 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_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, - // ]; - // let in_fut = async { - // await_key(p.PIN_4, layout::KeypadKey::A4, &channel).await; - // 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_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"); - // // pin.wait_for_low().await; - // // info!("LOW DETECTED"); - // // } - // }; + let in_fut = [ + await_key(p.PIN_4, layout::KeypadKey::A4, &channel), + await_key(p.PIN_1, layout::KeypadKey::A1, &channel), + await_key(p.PIN_2, layout::KeypadKey::A2, &channel), + await_key(p.PIN_3, layout::KeypadKey::A3, &channel), + await_key(p.PIN_5, layout::KeypadKey::B1, &channel), + await_key(p.PIN_6, layout::KeypadKey::B2, &channel), + await_key(p.PIN_7, layout::KeypadKey::B3, &channel), + await_key(p.PIN_8, layout::KeypadKey::B4, &channel), + await_key(p.PIN_9, layout::KeypadKey::C1, &channel), + await_key(p.PIN_10, layout::KeypadKey::C2, &channel), + await_key(p.PIN_11, layout::KeypadKey::C3, &channel), + await_key(p.PIN_12, layout::KeypadKey::C4, &channel), + ]; let out_fut = async { reader.run(false, &request_handler).await; @@ -148,13 +132,13 @@ async fn main(_spawner: Spawner) { usb_fut, join3( senderloop, - // join5( - await_key(p.PIN_2, layout::KeypadKey::A2, &channel), - // await_key(p.PIN_3, layout::KeypadKey::A3, &channel), - // await_key(p.PIN_4, layout::KeypadKey::A4, &channel), - // await_key(p.PIN_6, layout::KeypadKey::B2, &channel), - // await_key(p.PIN_7, layout::KeypadKey::B3, &channel), - // ), + join4( + await_key(p.PIN_2, layout::KeypadKey::A2, &channel), + await_key(p.PIN_3, layout::KeypadKey::A3, &channel), + await_key(p.PIN_4, layout::KeypadKey::A4, &channel), + await_key(p.PIN_1, layout::KeypadKey::B2, &channel), + // await_key(p.PIN_7, layout::KeypadKey::B3, &channel), + ), out_fut, ), )