From 2c1e83a67af2362cb0fe45091135cdc5088372ea Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 11 Jan 2020 16:43:16 +0100 Subject: [PATCH 1/3] Remove need to pass a downcounter to KeyMatrix. This is only used once when `new` is called. The caller should do that by themselves if needed, but it is quite reasonable to use the keypad without it, calling poll() at strategic points. Also remove dependencies: * cortex_m is not needed at all * embedded_hal was only needed for the counter Also fix a small typo in Cargo.toml. --- Cargo.toml | 6 ++---- src/lib.rs | 17 ++--------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e1c1534..afa2386 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,7 @@ repository = "https://github.com/zklapow/keymatrix" license = "MIT" [dependencies] -cortex-m = "~0.5" -generic-array = "~0.11" -embedded-hal = "~0.2" +generic-array = "~0.13" [dev-dependencies] cortex-m = "~0.5" @@ -23,7 +21,7 @@ samd21g18a = "~0.2" samd21_mini = "~0.1" panic-abort = "~0.2" -[[print_matrix]] +[[example]] name = "print_matrix" required-features = ["samd21"] diff --git a/src/lib.rs b/src/lib.rs index a03d541..43299b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ #![no_std] -extern crate cortex_m; -extern crate embedded_hal as hal; extern crate generic_array; use core::marker::PhantomData; @@ -9,7 +7,6 @@ use generic_array::{ArrayLength, GenericArray}; use generic_array::sequence::GenericSequence; use generic_array::functional::FunctionalSequence; use generic_array::typenum::Unsigned; -use hal::timer::{CountDown, Periodic}; pub trait KeyColumns { fn size(&self) -> N; @@ -40,22 +37,12 @@ impl KeyMatrix where RN: Unsigned + ArrayLength, R: KeyRows, { - pub fn new(counter: &mut CT, - freq: T, - debounce_count: u8, - cols: C, - rows: R) -> KeyMatrix - where T: Into, - CT: CountDown + Periodic, - C: KeyColumns, - R: KeyRows, - { - counter.start(freq.into()); + pub fn new(debounce_count: u8, cols: C, rows: R) -> Self { KeyMatrix { cols, rows, debounce_count, - state: KeyMatrix::::init_state(), + state: Self::init_state(), _cn: PhantomData, _cr: PhantomData, } From 2004f11200462609e4e380a7ec2ac03fe573fe7e Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 11 Jan 2020 16:58:53 +0100 Subject: [PATCH 2/3] Update to fallible input/output pins. Since the errors from pins are usually not very useful, I opted to throw the details away, avoiding more associated types etc. --- src/lib.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 43299b9..6df77e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,13 +10,13 @@ use generic_array::typenum::Unsigned; pub trait KeyColumns { fn size(&self) -> N; - fn enable_column(&mut self, col: usize); - fn disable_column(&mut self, col: usize); + fn enable_column(&mut self, col: usize) -> Result<(), ()>; + fn disable_column(&mut self, col: usize) -> Result<(), ()>; } pub trait KeyRows { fn size(&self) -> N; - fn read_row(&mut self, col: usize) -> bool; + fn read_row(&mut self, col: usize) -> Result; } pub struct KeyMatrix where RN: Unsigned + ArrayLength + ArrayLength, @@ -52,12 +52,12 @@ impl KeyMatrix where RN: Unsigned + ArrayLength Result<(), ()> { for i in 0..::to_usize() { - self.cols.enable_column(i); + self.cols.enable_column(i)?; for j in 0..::to_usize() { - match self.rows.read_row(j) { + match self.rows.read_row(j)? { true => { let cur: u8 = self.state[i][j]; // Saturating add to prevent overflow @@ -69,8 +69,9 @@ impl KeyMatrix where RN: Unsigned + ArrayLength GenericArray, CN> { @@ -126,22 +127,22 @@ impl KeyColumns<$size_type> for $Type { <$size_type>::new() } - fn enable_column(&mut self, col: usize) { + fn enable_column(&mut self, col: usize) -> Result<(), ()> { match col { $( - $index => self.$col_name.set_high(), + $index => self.$col_name.set_high().map_err(drop), )+ _ => unreachable!() - }; + } } - fn disable_column(&mut self, col: usize) { + fn disable_column(&mut self, col: usize) -> Result<(), ()> { match col { $( - $index => self.$col_name.set_low(), + $index => self.$col_name.set_low().map_err(drop), )+ _ => unreachable!() - }; + } } } @@ -184,10 +185,10 @@ impl KeyRows<$size_type> for $Type { <$size_type>::new() } - fn read_row(&mut self, row: usize) -> bool { + fn read_row(&mut self, row: usize) -> Result { match row { $( - $index => self.$row_name.is_high(), + $index => self.$row_name.is_high().map_err(drop), )+ _ => unreachable!() } From 7274954a7352b044e2c5f4dc153da706373ab13d Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 11 Jan 2020 17:03:46 +0100 Subject: [PATCH 3/3] Add some docstrings for KeyMatrix methods. --- src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 6df77e3..24ebb85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,11 @@ impl KeyMatrix where RN: Unsigned + ArrayLength, R: KeyRows, { + /// Create a new key matrix with the given column and row structs. + /// + /// The debounce parameter specifies in how many subsequent calls of + /// `poll()` a key has to be registered as pressed, in order to be + /// considered pressed by `current_state()`. pub fn new(debounce_count: u8, cols: C, rows: R) -> Self { KeyMatrix { cols, @@ -52,6 +57,11 @@ impl KeyMatrix where RN: Unsigned + ArrayLength 0`, this must be + /// called at least that number of times + 1 to actually show a key as + /// pressed. pub fn poll(&mut self) -> Result<(), ()> { for i in 0..::to_usize() { self.cols.enable_column(i)?; @@ -74,6 +84,7 @@ impl KeyMatrix where RN: Unsigned + ArrayLength GenericArray, CN> { self.state.clone() .map(|col| { @@ -83,10 +94,12 @@ impl KeyMatrix where RN: Unsigned + ArrayLength usize { ::to_usize() } + /// Return the number of columns that the matrix was created with. pub fn col_size(&self) -> usize { ::to_usize() }