From 597d2584b1caa0adf5d63245bc195902f5cd34ad Mon Sep 17 00:00:00 2001 From: Zac Wilson Date: Mon, 22 Dec 2025 17:17:34 +0000 Subject: [PATCH] Wrote MatrixConfig type --- src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d0135ae..157d396 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,3 +8,46 @@ pub struct TCA8418 { i2c: I2C, } +#[derive(Clone, Copy)] +pub struct MatrixConfig { + rows: u8, + columns: u16, +} + +impl MatrixConfig { + /// A new config where the matrix has no rows or columns and all of the pins are + /// allocated for GPIO. + pub const fn new_empty() -> Self { + MatrixConfig { + rows: 0, + columns: 0, + } + } + + /// Add a row to the keyboard matrix, meaning its pin will not be available for GPIO. + /// + /// `row_number` must be in the range `0..=7` + pub const fn with_row(self, row_number: u8) -> Self { + if row_number >= 8 { + panic!("row_number out of bounds"); + } + MatrixConfig { + rows: self.rows | (1 << row_number), + columns: self.columns, + } + } + + /// Add a column to the keyboard matrix, meaning its pin will not be available for GPIO. + /// + /// `column_number` must be in the range `0..=9` + pub const fn with_column(self, column_number: u8) -> Self { + if column_number >= 10 { + panic!("column_number out of bounds"); + } + MatrixConfig { + rows: self.rows, + columns: self.columns | (1 << column_number), + } + } +} +