Skip to content

Commit

Permalink
src: Add support to leak detection
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Feb 16, 2024
1 parent 8261178 commit 50a01a4
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -164,6 +165,7 @@ pub struct Navigator {
mag: Ak09915<I2cdev>,
led: Led,
neopixel: Strip,
leak: Pin,
}

impl Deref for Pwm {
Expand Down Expand Up @@ -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),
Expand All @@ -340,6 +346,7 @@ impl NavigatorBuilder {
imu: (imu),
led: (led),
neopixel: (neopixel),
leak: (leak),
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1003,6 +1033,7 @@ impl Navigator {
accelerometer: self.read_accel(),
magnetometer: self.read_mag(),
gyro: self.read_gyro(),
leak: self.read_leak(),
}
}

Expand All @@ -1029,6 +1060,7 @@ impl Navigator {
bmp: Bmp,
imu: Imu,
mag: AxisData,
leak: bool,
}

Navigator {
Expand All @@ -1043,6 +1075,7 @@ impl Navigator {
gyroscope: self.read_gyro(),
},
mag: self.read_mag(),
leak: self.read_leak(),
}
}
}

0 comments on commit 50a01a4

Please sign in to comment.