From 7346a28b32ca9d7fb898a2133c3115490c836190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 16 Feb 2024 09:26:00 -0300 Subject: [PATCH 1/2] src: Add support to leak detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 00b98cae81..517a037d4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ use ads1x1x::{ }; use ak09915_rs::{Ak09915, Mode as mag_Mode}; use bmp280::{Bmp280, Bmp280Builder}; -use embedded_hal::prelude::_embedded_hal_blocking_delay_DelayMs; +use embedded_hal::{digital::v2::InputPin, prelude::_embedded_hal_blocking_delay_DelayMs}; use icm20689::{self, AccelRange, Builder as imu_Builder, GyroRange, SpiInterface, ICM20689}; use linux_embedded_hal::spidev::{self, SpidevOptions}; use linux_embedded_hal::sysfs_gpio::Direction; @@ -138,6 +138,7 @@ pub struct SensorData { pub accelerometer: AxisData, pub magnetometer: AxisData, pub gyro: AxisData, + pub leak: bool, } /// The `Led` struct represents the 3 LEDs on navigator board. @@ -164,6 +165,7 @@ pub struct Navigator { mag: Ak09915, led: Led, neopixel: Strip, + leak: Pin, } impl Deref for Pwm { @@ -332,6 +334,8 @@ impl NavigatorBuilder { let led = Led::new(); + let leak = Pin::new(27); + Navigator { adc: (adc), bmp: (bmp), @@ -340,6 +344,7 @@ impl NavigatorBuilder { imu: (imu), led: (led), neopixel: (neopixel), + leak: (leak), } } } @@ -384,6 +389,13 @@ impl Navigator { self.bmp.zero().unwrap(); self.led.set_led_all(false); + + self.leak + .export() + .expect("Error: Failed to export leak pin"); + self.leak + .set_direction(Direction::In) + .expect("Error: Failed to set leak pin as input"); } pub fn self_test(&mut self) -> bool { @@ -976,6 +988,31 @@ impl Navigator { } } + /// Reads the state of leak detector pin from [`Navigator`]. + /// + /// The value is true when a leak is detected. + /// + /// # Examples + /// + /// ```no_run + /// use navigator_rs::{Navigator}; + /// use std::thread::sleep; + /// use std::time::Duration; + /// + /// let mut nav = Navigator::new(); + /// nav.init(); + /// + /// loop { + /// println!("Leak: {}", nav.read_leak()); + /// sleep(Duration::from_millis(1000)); + /// } + /// ``` + pub fn read_leak(&self) -> bool { + self.leak + .is_high() + .expect("Failed to read state of leak pin") + } + /// Reads all sensors and stores on a single structure. /// /// # Examples @@ -1003,6 +1040,7 @@ impl Navigator { accelerometer: self.read_accel(), magnetometer: self.read_mag(), gyro: self.read_gyro(), + leak: self.read_leak(), } } @@ -1029,6 +1067,7 @@ impl Navigator { bmp: Bmp, imu: Imu, mag: AxisData, + leak: bool, } Navigator { @@ -1043,6 +1082,7 @@ impl Navigator { gyroscope: self.read_gyro(), }, mag: self.read_mag(), + leak: self.read_leak(), } } } From 46cbac01294c9c74f328ba3ae0570ada22d6f213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Fri, 16 Feb 2024 12:24:35 -0300 Subject: [PATCH 2/2] bemches: Add test for leak sensor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- benches/bench.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/benches/bench.rs b/benches/bench.rs index e1920479c9..dadf0e9955 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -40,6 +40,7 @@ fn navigator_benchmark(c: &mut Criterion) { bench!(set_led(UserLed::Led1, false)); bench!(set_led_toggle(UserLed::Led1)); bench!(get_led(UserLed::Led1)); + bench!(read_leak()); } criterion_group!(benches, navigator_benchmark);