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..24ebb85 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,17 +7,16 @@ 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; - 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, @@ -40,22 +37,17 @@ 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()); + /// 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, rows, debounce_count, - state: KeyMatrix::::init_state(), + state: Self::init_state(), _cn: PhantomData, _cr: PhantomData, } @@ -65,12 +57,17 @@ 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); + 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 @@ -82,10 +79,12 @@ impl KeyMatrix where RN: Unsigned + ArrayLength GenericArray, CN> { self.state.clone() .map(|col| { @@ -95,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() } @@ -139,22 +140,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!() - }; + } } } @@ -197,10 +198,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!() }