use std::error::Error;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use rppal::gpio::Gpio;
const GPIO_LED: u8 = 23;
const NUM_THREADS: usize = 3;
fn main() -> Result<(), Box<dyn Error>> {
let output_pin = Arc::new(Mutex::new(Gpio::new()?.get(GPIO_LED)?.into_output_low()));
let mut threads = Vec::with_capacity(NUM_THREADS);
(0..NUM_THREADS).for_each(|thread_id| {
let output_pin_clone = Arc::clone(&output_pin);
threads.push(thread::spawn(move || {
let mut pin = output_pin_clone.lock().unwrap();
println!("Blinking the LED from thread {}.", thread_id);
pin.set_high();
thread::sleep(Duration::from_millis(250));
pin.set_low();
thread::sleep(Duration::from_millis(250));
}));
});
let mut pin = output_pin.lock().unwrap();
println!("Blinking the LED from the main thread.");
pin.set_high();
thread::sleep(Duration::from_millis(250));
pin.set_low();
thread::sleep(Duration::from_millis(250));
drop(pin);
threads
.into_iter()
.for_each(|thread| thread.join().unwrap());
Ok(())
}