use rppal::gpio::{Event, Gpio, Trigger};
use std::error::Error;
use std::sync::{Arc, Mutex};
use std::time::Duration;
const INPUT_PIN_GPIO: u8 = 27;
const STOP_AFTER_N_CHANGES: u8 = 5;
fn input_callback(event: Event, my_data: Arc<Mutex<u8>>) {
println!("Event: {:?}", event);
*my_data.lock().unwrap() += 1;
}
fn main() -> Result<(), Box<dyn Error>> {
let shared_state = Arc::new(Mutex::new(0));
let mut input_pin = Gpio::new()?.get(INPUT_PIN_GPIO)?.into_input_pulldown();
let shared_state_hold = shared_state.clone();
input_pin.set_async_interrupt(
Trigger::FallingEdge,
Some(Duration::from_millis(50)),
move |event| {
input_callback(event, shared_state_hold.clone());
},
)?;
loop {
if *shared_state.lock().unwrap() >= STOP_AFTER_N_CHANGES {
println!("Reached {STOP_AFTER_N_CHANGES} events, exiting...");
break;
}
println!("Still waiting...");
std::thread::sleep(Duration::from_secs(1));
}
Ok(())
}