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 aa5567e
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 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,11 @@ 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 +347,7 @@ impl NavigatorBuilder {
imu: (imu),
led: (led),
neopixel: (neopixel),
leak: (leak),
}
}
}
Expand Down Expand Up @@ -976,6 +984,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
Expand Down Expand Up @@ -1003,6 +1036,7 @@ impl Navigator {
accelerometer: self.read_accel(),
magnetometer: self.read_mag(),
gyro: self.read_gyro(),
leak: self.read_leak(),
}
}

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

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

0 comments on commit aa5567e

Please sign in to comment.