Skip to content

Update to use const generics #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ description = "Generate generic key matrix drivers for use with embedded-hal"
keywords = ["no-std", "arm", "cortex-m", "embedded-hal-driver"]
repository = "https://github.com/zklapow/keymatrix"
license = "MIT"
edition = "2018"

[dependencies]
cortex-m = "~0.5"
generic-array = "~0.11"
embedded-hal = "~0.2"

[dev-dependencies]
cortex-m = "~0.5"
Expand All @@ -23,7 +21,7 @@ samd21g18a = "~0.2"
samd21_mini = "~0.1"
panic-abort = "~0.2"

[[print_matrix]]
[[example]]
name = "print_matrix"
required-features = ["samd21"]

Expand Down
147 changes: 68 additions & 79 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,55 @@
#![no_std]

extern crate cortex_m;
extern crate embedded_hal as hal;
extern crate generic_array;

use core::marker::PhantomData;
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<N: Unsigned> {
fn size(&self) -> N;
fn enable_column(&mut self, col: usize);
fn disable_column(&mut self, col: usize);
pub trait KeyColumns<const N: usize> {
fn size(&self) -> usize;
fn enable_column(&mut self, col: usize) -> Result<(), ()>;
fn disable_column(&mut self, col: usize) -> Result<(), ()>;
}

pub trait KeyRows<N: Unsigned> {
fn size(&self) -> N;
fn read_row(&mut self, col: usize) -> bool;
pub trait KeyRows<const N: usize> {
fn size(&self) -> usize;
fn read_row(&mut self, col: usize) -> Result<bool, ()>;
}

pub struct KeyMatrix<CN, RN, C, R> where RN: Unsigned + ArrayLength<bool> + ArrayLength<u8>,
CN: Unsigned + ArrayLength<GenericArray<bool, RN>> + ArrayLength<GenericArray<u8, RN>>,
C: KeyColumns<CN>,
R: KeyRows<RN> {
pub struct KeyMatrix<C, R, const CN: usize, const RN: usize>
where C: KeyColumns<CN>, R: KeyRows<RN> {
cols: C,
rows: R,
debounce_count: u8,
state: GenericArray<GenericArray<u8, RN>, CN>,

_cn: PhantomData<CN>,
_cr: PhantomData<RN>,
state: [[u8; RN]; CN],
}

impl<CN, RN, C, R> KeyMatrix<CN, RN, C, R> where RN: Unsigned + ArrayLength<bool> + ArrayLength<u8>,
CN: Unsigned + ArrayLength<GenericArray<bool, RN>> + ArrayLength<GenericArray<u8, RN>>,
C: KeyColumns<CN>,
R: KeyRows<RN>,
{
pub fn new<TU, CT, T>(counter: &mut CT,
freq: T,
debounce_count: u8,
cols: C,
rows: R) -> KeyMatrix<CN, RN, C, R>
where T: Into<TU>,
CT: CountDown<Time=TU> + Periodic,
C: KeyColumns<CN>,
R: KeyRows<RN>,
{
counter.start(freq.into());
impl<C, R, const CN: usize, const RN: usize> KeyMatrix<C, R, CN, RN>
where C: KeyColumns<CN>, R: KeyRows<RN> {
/// 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::<CN, RN, C, R>::init_state(),
_cn: PhantomData,
_cr: PhantomData,
state: Self::init_state(),
}
}

fn init_state() -> GenericArray<GenericArray<u8, RN>, CN> {
return GenericArray::generate(|_i| GenericArray::generate(|_j| 0u8));
fn init_state() -> [[u8; RN]; CN] {
[[0; RN]; CN]
}

pub fn poll(&mut self) {
for i in 0..<CN as Unsigned>::to_usize() {
self.cols.enable_column(i);

for j in 0..<RN as Unsigned>::to_usize() {
match self.rows.read_row(j) {
/// Scan the key matrix once.
///
/// If the matrix was created with a `debounce_count > 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..CN {
self.cols.enable_column(i)?;

for j in 0..RN {
match self.rows.read_row(j)? {
true => {
let cur: u8 = self.state[i][j];
// Saturating add to prevent overflow
Expand All @@ -82,33 +61,40 @@ impl<CN, RN, C, R> KeyMatrix<CN, RN, C, R> where RN: Unsigned + ArrayLength<bool
}
}

self.cols.disable_column(i);
self.cols.disable_column(i)?;
}
Ok(())
}

pub fn current_state(&self) -> GenericArray<GenericArray<bool, RN>, CN> {
self.state.clone()
.map(|col| {
col.map(|elem| {
elem > self.debounce_count
})
})
/// Return a 2-dimensional array of the last polled state of the matrix.
pub fn current_state(&self) -> [[bool; RN]; CN] {
let mut state = [[false; RN]; CN];
for (i, row) in self.state.iter().enumerate() {
for (j, &elem) in row.iter().enumerate() {
if elem > self.debounce_count {
state[i][j] = true;
}
}
}
state
}

/// Return the number of rows that the matrix was created with.
pub fn row_size(&self) -> usize {
<RN as Unsigned>::to_usize()
RN
}

/// Return the number of columns that the matrix was created with.
pub fn col_size(&self) -> usize {
<CN as Unsigned>::to_usize()
CN
}
}

#[macro_export]
macro_rules! key_columns {
(
$Type:ident,
$size_type:ty,
$size:literal,
[$(
$col_name:ident : ($index:expr , $pintype:ty)
),+]) => {
Expand All @@ -134,27 +120,29 @@ impl $Type {
}
}

impl KeyColumns<$size_type> for $Type {
fn size(&self) -> $size_type {
<$size_type>::new()
impl KeyColumns<$size> for $Type {
fn size(&self) -> usize {
$size
}

fn enable_column(&mut self, col: usize) {
fn enable_column(&mut self, col: usize) -> Result<(), ()> {
use embedded_hal::digital::OutputPin;
match col {
$(
$index => self.$col_name.set_high(),
$index => OutputPin::set_low(&mut self.$col_name).map_err(drop),
)+
_ => unreachable!()
};
}
}

fn disable_column(&mut self, col: usize) {
fn disable_column(&mut self, col: usize) -> Result<(), ()> {
use embedded_hal::digital::OutputPin;
match col {
$(
$index => self.$col_name.set_low(),
$index => OutputPin::set_high(&mut self.$col_name).map_err(drop),
)+
_ => unreachable!()
};
}
}
}

Expand All @@ -166,7 +154,7 @@ impl KeyColumns<$size_type> for $Type {
macro_rules! key_rows {
(
$Type:ident,
$size_type:ty,
$size:literal,
[$(
$row_name:ident : ($index:expr , $pintype:ty)
),+]) => {
Expand All @@ -192,15 +180,16 @@ impl $Type {
}
}

impl KeyRows<$size_type> for $Type {
fn size(&self) -> $size_type {
<$size_type>::new()
impl KeyRows<$size> for $Type {
fn size(&self) -> usize {
$size
}

fn read_row(&mut self, row: usize) -> bool {
fn read_row(&mut self, row: usize) -> Result<bool, ()> {
use embedded_hal::digital::InputPin;
match row {
$(
$index => self.$row_name.is_high(),
$index => InputPin::is_low(&mut self.$row_name).map_err(drop),
)+
_ => unreachable!()
}
Expand Down