feat: Create GPIO monitor and basic callback functionality to print

This commit is contained in:
Jet Pham 2025-06-03 08:48:24 -07:00
parent 9b74fb2b8c
commit 18aa3dfaa8
No known key found for this signature in database
7 changed files with 616 additions and 0 deletions

49
src/gpio.rs Normal file
View file

@ -0,0 +1,49 @@
use std::time::Duration;
use anyhow::Result;
use rppal::gpio::{Gpio, InputPin, Level};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitEvent {
Open,
Closed,
}
pub struct GpioMonitor {
pin: InputPin,
poll_interval: Duration,
}
impl GpioMonitor {
pub fn new(pin_number: u8, poll_interval: Duration) -> Result<Self> {
let gpio = Gpio::new()?;
let pin = gpio.get(pin_number)?.into_input_pullup();
Ok(Self { pin, poll_interval })
}
pub async fn monitor<F>(&mut self, mut callback: F) -> Result<()>
where
F: FnMut(CircuitEvent) + Send + 'static,
{
let mut previous_state = self.get_current_state();
loop {
let current_state = self.get_current_state();
if current_state != previous_state {
callback(current_state);
previous_state = current_state;
}
tokio::time::sleep(self.poll_interval).await;
}
}
pub fn get_current_state(&self) -> CircuitEvent {
match self.pin.read() {
Level::High => CircuitEvent::Open,
Level::Low => CircuitEvent::Closed,
}
}
}