RKTK API Docs RKTK Home Repo

rktk_drivers_nrf/keyscan/
flex_pin.rs

1use embassy_nrf::{
2    gpio::{Flex, OutputDrive, Pin, Pull as NrfPull},
3    Peripheral,
4};
5pub use rktk_drivers_common::keyscan::duplex_matrix::ScanDir;
6use rktk_drivers_common::keyscan::flex_pin::{FlexPin, Pull};
7
8/// Wrapper over flex pin that implements rktk_drivers_common's [`FlexPin`] trait.
9pub struct NrfFlexPin<'a> {
10    pin: Flex<'a>,
11    pull: NrfPull,
12    drive: OutputDrive,
13}
14
15impl<'a> NrfFlexPin<'a> {
16    pub fn new(pin: impl Peripheral<P = impl Pin> + 'a) -> Self {
17        Self {
18            pin: Flex::new(pin),
19            pull: NrfPull::None,
20            drive: OutputDrive::Standard,
21        }
22    }
23}
24
25impl FlexPin for NrfFlexPin<'_> {
26    fn set_as_input(&mut self) {
27        #[allow(clippy::needless_match)]
28        let pull = match self.pull {
29            NrfPull::Up => NrfPull::Up,
30            NrfPull::Down => NrfPull::Down,
31            NrfPull::None => NrfPull::None,
32        };
33        self.pin.set_as_input(pull);
34    }
35
36    fn set_as_output(&mut self) {
37        self.pin.set_as_output(self.drive);
38    }
39
40    fn set_low(&mut self) {
41        self.pin.set_low();
42    }
43
44    fn set_high(&mut self) {
45        self.pin.set_high();
46    }
47
48    fn is_high(&self) -> bool {
49        self.pin.is_high()
50    }
51
52    fn is_low(&self) -> bool {
53        self.pin.is_low()
54    }
55
56    async fn wait_for_high(&mut self) {
57        self.pin.wait_for_high().await;
58    }
59
60    async fn wait_for_low(&mut self) {
61        self.pin.wait_for_low().await;
62    }
63
64    fn set_pull(&mut self, pull: Pull) {
65        self.pull = match pull {
66            Pull::Up => NrfPull::Up,
67            Pull::Down => NrfPull::Down,
68        };
69    }
70}