From 70526c44ebe5245ac2c787e8ea6fce7261b8eec3 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Thu, 18 Apr 2024 20:56:39 -0400 Subject: [PATCH] TransparentPins: total pin number only accessible through getter now --- src/bin/pin_scanner.rs | 8 ++++---- src/bin/pin_test.rs | 2 +- src/pins.rs | 9 +++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bin/pin_scanner.rs b/src/bin/pin_scanner.rs index d50d263..871cf1c 100644 --- a/src/bin/pin_scanner.rs +++ b/src/bin/pin_scanner.rs @@ -45,7 +45,7 @@ struct Connection { #[embassy_executor::task] async fn scanner_task(mut pin_driver: pins::TransparentPins) { log::info!("scanner_task: setting pins as input"); - for i in 0..pin_driver.n_total_pins { + for i in 0..pin_driver.n_usable_pins() { unwrap(pin_driver.set_input(i as u8)).await; unwrap(pin_driver.set_pull(i as u8, gpio::Pull::Up)).await; } @@ -61,15 +61,15 @@ async fn scanner_task(mut pin_driver: pins::TransparentPins) { log::info!(""); log::info!("---"); log::info!("STARTING SCAN..."); - for gnd_pin in 0..pin_driver.n_total_pins { + for gnd_pin in 0..pin_driver.n_usable_pins() { let gnd_pin = gnd_pin as u8; unwrap(pin_driver.set_output(gnd_pin)).await; let input = unwrap(pin_driver.read_all()).await; unwrap(pin_driver.set_input(gnd_pin)).await; // this represents the pins that are different from expected - let mask = input ^ (((1 << pin_driver.n_total_pins) - 1) ^ (1 << gnd_pin)); - for input_pin in 0..pin_driver.n_total_pins { + let mask = input ^ (((1 << pin_driver.n_usable_pins()) - 1) ^ (1 << gnd_pin)); + for input_pin in 0..pin_driver.n_usable_pins() { let input_pin = input_pin as u8; if ((1 << input_pin) & mask) != 0 && n_connections < MAX_CONNECTIONS { connections[n_connections] = Some(Connection { gnd_pin, input_pin }); diff --git a/src/bin/pin_test.rs b/src/bin/pin_test.rs index 6618f71..2bee652 100644 --- a/src/bin/pin_test.rs +++ b/src/bin/pin_test.rs @@ -60,7 +60,7 @@ async fn main(_spawner: Spawner) { .await; log::info!("main: setting pins as input"); - for i in 0..pin_driver.n_total_pins { + for i in 0..pin_driver.n_usable_pins() { unwrap(pin_driver.set_input(i as u8)).await; unwrap(pin_driver.set_pull(i as u8, gpio::Pull::Up)).await; } diff --git a/src/pins.rs b/src/pins.rs index a5123ff..8b557b9 100644 --- a/src/pins.rs +++ b/src/pins.rs @@ -95,8 +95,8 @@ pub struct TransparentPins { pins: [Flex<'static, AnyPin>; N_REGULAR_PINS], i2c_bus: I2cBus, disable_unsafe_pins: bool, - /// Number of total usable pins. Transparent pins all have an address from `0..n_total_pins`. - pub n_total_pins: usize, + /// Number of total usable pins. + n_total_pins: usize, /// Usable pins per extender. Depends on `disable_unsafe_pins`. usable_pins_per_extender: usize, /// Usable pin count on all extenders. Depends on `disable_unsafe_pins`. @@ -122,6 +122,11 @@ macro_rules! extender { } impl TransparentPins { + /// Get amount of usable pins. Transparent pins all have an address from `0..n_usable_pins()`. + pub fn n_usable_pins(&self) -> usize { + self.n_total_pins + } + /// Transform addresses into a transparent pin number, taking into account pins that aren't being used. fn addr_to_pin(&self, addr: u8) -> u8 { if self.disable_unsafe_pins {