Keypad mapping

This commit is contained in:
eraden 2024-09-23 20:31:51 +02:00 committed by Adrian Woźniak
parent 2fb089fd03
commit b5da325946

View File

@ -115,37 +115,17 @@ fn main() -> ! {
// .unwrap();
driver.clear_frame(&mut spi, &mut delay).unwrap();
let mut gpio2 = Input::new(io.pins.gpio2, Pull::Up);
let mut gpio3 = Input::new(io.pins.gpio3, Pull::Up);
let mut gpio4 = Input::new(io.pins.gpio4, Pull::Up);
let mut gpio5 = Input::new(io.pins.gpio5, Pull::Up);
let mut gpio6 = OutputOpenDrain::new(io.pins.gpio6, Level::Low, Pull::Up);
let mut gpio7 = OutputOpenDrain::new(io.pins.gpio7, Level::Low, Pull::Up);
let mut gpio8 = OutputOpenDrain::new(io.pins.gpio8, Level::Low, Pull::Up);
let mut gpio10 = OutputOpenDrain::new(io.pins.gpio10, Level::Low, Pull::Up);
let mut kbd = Keypad((
(&mut gpio2, &mut gpio6),
(&mut gpio3, &mut gpio6),
(&mut gpio4, &mut gpio6),
(&mut gpio5, &mut gpio6),
// --------------------------------------
(&mut gpio2, &mut gpio7),
(&mut gpio3, &mut gpio7),
(&mut gpio4, &mut gpio7),
(&mut gpio5, &mut gpio7),
// --------------------------------------
(&mut gpio2, &mut gpio8),
(&mut gpio3, &mut gpio8),
(&mut gpio4, &mut gpio8),
(&mut gpio5, &mut gpio8),
// --------------------------------------
(&mut gpio2, &mut gpio10),
(&mut gpio3, &mut gpio10),
(&mut gpio4, &mut gpio10),
(&mut gpio5, &mut gpio10),
));
let mut kbd = Keypad {
i1: Input::new(io.pins.gpio2, Pull::Up),
i2: Input::new(io.pins.gpio3, Pull::Up),
i3: Input::new(io.pins.gpio4, Pull::Up),
i4: Input::new(io.pins.gpio5, Pull::Up),
//---------------------------
o1: OutputOpenDrain::new(io.pins.gpio6, Level::Low, Pull::Up),
o2: OutputOpenDrain::new(io.pins.gpio7, Level::Low, Pull::Up),
o3: OutputOpenDrain::new(io.pins.gpio8, Level::Low, Pull::Up),
o4: OutputOpenDrain::new(io.pins.gpio10, Level::Low, Pull::Up),
};
let mut ctx = Context {
button_pressed: None,
@ -227,101 +207,62 @@ pub struct Context<'d> {
pub spi: SPI<'d>,
pub delay: Delay,
}
struct Rows<'d> {
gpio2: Input<'d, Gpio2>, //
gpio3: Input<'d, Gpio3>, //
gpio4: Input<'d, Gpio4>, //
gpio5: Input<'d, Gpio5>, //
struct Keypad<'d> {
o1: OutputOpenDrain<'d, Gpio6>, //
o2: OutputOpenDrain<'d, Gpio7>, //
o3: OutputOpenDrain<'d, Gpio8>, //
o4: OutputOpenDrain<'d, Gpio10>, //
// --------------------------------------
i1: Input<'d, Gpio2>, //
i2: Input<'d, Gpio3>, //
i3: Input<'d, Gpio4>, //
i4: Input<'d, Gpio5>, //
}
struct Cols<'d> {
gpio6: OutputOpenDrain<'d, Gpio6>, //
gpio7: OutputOpenDrain<'d, Gpio7>, //
gpio8: OutputOpenDrain<'d, Gpio8>, //
gpio10: OutputOpenDrain<'d, Gpio10>, //
}
pub type OOD<'d, T> = OutputOpenDrain<'d, T>;
struct Keypad<'d>(
pub (
// --------------------------------------
(&'d mut Input<'d, Gpio2>, &'d mut OOD<'d, Gpio6>),
(&'d mut Input<'d, Gpio3>, &'d mut OOD<'d, Gpio6>),
(&'d mut Input<'d, Gpio4>, &'d mut OOD<'d, Gpio6>),
(&'d mut Input<'d, Gpio5>, &'d mut OOD<'d, Gpio6>),
// --------------------------------------
(&'d mut Input<'d, Gpio2>, &'d mut OOD<'d, Gpio7>),
(&'d mut Input<'d, Gpio3>, &'d mut OOD<'d, Gpio7>),
(&'d mut Input<'d, Gpio4>, &'d mut OOD<'d, Gpio7>),
(&'d mut Input<'d, Gpio5>, &'d mut OOD<'d, Gpio7>),
// --------------------------------------
(&'d mut Input<'d, Gpio2>, &'d mut OOD<'d, Gpio8>),
(&'d mut Input<'d, Gpio3>, &'d mut OOD<'d, Gpio8>),
(&'d mut Input<'d, Gpio4>, &'d mut OOD<'d, Gpio8>),
(&'d mut Input<'d, Gpio5>, &'d mut OOD<'d, Gpio8>),
// --------------------------------------
(&'d mut Input<'d, Gpio2>, &'d mut OOD<'d, Gpio10>),
(&'d mut Input<'d, Gpio3>, &'d mut OOD<'d, Gpio10>),
(&'d mut Input<'d, Gpio4>, &'d mut OOD<'d, Gpio10>),
(&'d mut Input<'d, Gpio5>, &'d mut OOD<'d, Gpio10>),
),
);
impl<'d> Keypad<'d> {
pub fn pressed(&mut self) -> Option<Button> {
let col1 = [
self.0 .0.is_low(),
self.0 .1.is_low(),
self.0 .2.is_low(),
self.0 .3.is_low(),
];
let col2 = [
self.0 .4.is_low(),
self.0 .5.is_low(),
self.0 .6.is_low(),
self.0 .7.is_low(),
];
let col3 = [
self.0 .8.is_low(),
self.0 .9.is_low(),
self.0 .10.is_low(),
self.0 .11.is_low(),
];
let col4 = [
self.0 .12.is_low(),
self.0 .13.is_low(),
self.0 .14.is_low(),
self.0 .15.is_low(),
];
let res: [[bool; 4]; 4] = core::array::from_fn(|x| {
core::array::from_fn(|y| {
self.set_high(x as u8);
let v = self.is_low(y as u8);
self.set_low(x as u8);
v
})
});
if col1.iter().any(|b| !b)
|| col2.iter().any(|b| !b)
|| col3.iter().any(|b| !b)
|| col4.iter().any(|b| !b)
{
if res.iter().any(|a| a.iter().any(|b| !b)) {
println!("***************************************");
println!("col1 {col1:?}");
println!("col2 {col2:?}");
println!("col3 {col3:?}");
println!("col4 {col4:?}");
for a in res {
println!("col {a:?}");
}
}
None
}
}
trait KeyboardInput<'d> {
fn is_low(&mut self) -> bool;
fn is_high(&mut self) -> bool {
!self.is_low()
fn set_high(&mut self, o: u8) {
match o {
0 => self.o1.set_high(),
1 => self.o2.set_high(),
2 => self.o3.set_high(),
3 => self.o4.set_high(),
_ => {}
};
}
fn set_low(&mut self, o: u8) {
match o {
0 => self.o1.set_low(),
1 => self.o2.set_low(),
2 => self.o3.set_low(),
3 => self.o4.set_low(),
_ => {}
};
}
fn is_low(&mut self, o: u8) -> bool {
match o {
0 => self.i1.is_low(),
1 => self.i2.is_low(),
2 => self.i3.is_low(),
3 => self.i4.is_low(),
_ => false,
}
}
impl<'d, C: InputPin, R: OutputPin + InputPin> KeyboardInput<'d>
for (&'d mut Input<'d, C>, &'d mut OutputOpenDrain<'d, R>)
{
fn is_low(&mut self) -> bool {
self.1.set_low();
let s = self.0.is_low();
self.1.set_high();
s
}
}