1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
//! # Passive Infrared (PIR) Sensor Module
//!
//! Provides an abstraction for interfacing with a PIR motion sensor. PIR
//! sensors are commonly used to detect movement within a certain range.
//!
//! ## Example
//! ```no_run
//! use esp_hal::gpio::GpioExt; // Import traits to split pins
//! use esp_hal::peripherals::Peripherals;
//! use your_crate::peripherals::PirSensor;
//!
//! let peripherals = Peripherals::take().unwrap();
//! let pins = peripherals.GPIO.split();
//!
//! // Suppose the PIR sensor is connected to GPIO5
//! let pir_pin = pins.gpio5.into_pull_up_input(); // Configure the pin as input with pull-up
//! let mut pir_sensor = PirSensor::new(pir_pin);
//!
//! // Now you can check for motion
//! if pir_sensor.motion_detected() {
//! println!("Motion detected!");
//! }
//! ```
use embedded_hal::digital::v2::InputPin;
use esp_hal::delay::Delay;
use super::{PeripheralError, UnifiedData};
/// Represents a PIR motion sensor connected to a single digital input pin.
pub struct PirSensor<PIN: InputPin> {
/// The digital input pin connected to the PIR sensor.
inner: PIN,
}
impl<PIN: InputPin<Error = core::convert::Infallible>> PirSensor<PIN> {
/// Constructs a new `PirSensor` with the given input pin.
///
/// # Arguments
/// * `pin` - The digital input pin connected to the PIR sensor.
/// # Returns
/// A new `PirSensor` instance.
pub fn create_on_pins(pin: PIN) -> Self {
PirSensor { inner: pin }
}
}
impl<PIN: InputPin<Error = core::convert::Infallible>> UnifiedData for PirSensor<PIN> {
type Output = bool;
/// Reads the current state of a PIR sensor data pin
///
/// # Returns
/// Returns an `Ok(true)' if motion is detected, `Ok(false)` otherwise
fn read(&mut self, _delay: Delay) -> Result<Self::Output, PeripheralError> {
Ok(self.inner.is_high().unwrap())
}
}