RKTK API Docs RKTK Home Repo

rktk_drivers_common/keyscan/
mod.rs

1//! Key scanning driver implementations.
2
3use embassy_time::Duration;
4use embedded_hal::digital::{InputPin, OutputPin};
5use rktk::config::Hand;
6
7pub mod duplex_matrix;
8pub mod flex_pin;
9pub mod matrix;
10mod pressed;
11pub mod shift_register_matrix;
12
13/// Utility function that takes an output pin and an input pin and determines the move based on the result of the input pin when the output pin is high.
14///
15/// * `output`: Output pin
16/// * `input`: Input pin
17/// * `input_delay`: Time from output pin high to read (default: 10ms)
18/// * `high_hand`: Which move is judged when the input pin is high? (default: Left)
19pub async fn detect_hand_from_matrix<E, O: OutputPin<Error = E>, I: InputPin<Error = E>>(
20    mut output: O,
21    mut input: I,
22    input_delay: Option<Duration>,
23    high_hand: Option<Hand>,
24) -> Result<Hand, E> {
25    let high_hand = high_hand.unwrap_or(Hand::Left);
26
27    output.set_high()?;
28    embassy_time::Timer::after(input_delay.unwrap_or(Duration::from_millis(10))).await;
29    let hand = if input.is_high()? {
30        high_hand
31    } else {
32        high_hand.other()
33    };
34    output.set_low()?;
35
36    Ok(hand)
37}