#!/usr/bin/env cargo run
use jetsongpio::{Direction, Edge, GPIO, Level, Mode};
use std::thread;
use std::time::Duration;
const LED1_PIN: u32 = 12; const LED2_PIN: u32 = 13; const BUTTON_PIN: u32 = 18;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let gpio = GPIO::new();
gpio.setmode(Mode::BOARD)?;
gpio.setup(
vec![LED1_PIN, LED2_PIN],
Direction::OUT,
Some(Level::LOW),
None,
)?;
gpio.setup(vec![BUTTON_PIN], Direction::IN, None, None)?;
gpio.output(vec![LED1_PIN, LED2_PIN], vec![Level::LOW, Level::LOW])?;
gpio.add_event_detect(
BUTTON_PIN,
Edge::Falling,
Some(Box::new(|ch| {
println!("Button pressed on pin {ch}! Blink LED2");
for _ in 0..5 {
println!("LED2 HIGH");
thread::sleep(Duration::from_millis(500));
println!("LED2 LOW");
thread::sleep(Duration::from_millis(500));
}
})),
Some(Duration::from_millis(200)), None, )?;
println!("Starting demo now! Press CTRL+C to exit");
loop {
gpio.output(vec![LED1_PIN], vec![Level::HIGH])?;
thread::sleep(Duration::from_secs(2));
gpio.output(vec![LED1_PIN], vec![Level::LOW])?;
thread::sleep(Duration::from_secs(2));
}
}