From 50a01a4afe14382d9decad343c3373f73e19d9a3 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] 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 | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 00b98cae81..7c464b33d0 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,10 @@ impl NavigatorBuilder { let led = Led::new(); + let leak = Pin::new(27); + leak.export().expect("Error: Failed to export leak pin"); + leak.set_direction(Direction::In).expect("Error: Failed to set leak pin as input"); + Navigator { adc: (adc), bmp: (bmp), @@ -340,6 +346,7 @@ impl NavigatorBuilder { imu: (imu), led: (led), neopixel: (neopixel), + leak: (leak), } } } @@ -976,6 +983,29 @@ 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 { + return self.leak.is_high().expect("Failed to read state of leak pin") + } + /// Reads all sensors and stores on a single structure. /// /// # Examples @@ -1003,6 +1033,7 @@ impl Navigator { accelerometer: self.read_accel(), magnetometer: self.read_mag(), gyro: self.read_gyro(), + leak: self.read_leak(), } } @@ -1029,6 +1060,7 @@ impl Navigator { bmp: Bmp, imu: Imu, mag: AxisData, + leak: bool, } Navigator { @@ -1043,6 +1075,7 @@ impl Navigator { gyroscope: self.read_gyro(), }, mag: self.read_mag(), + leak: self.read_leak(), } } }