adding basic program

This commit is contained in:
talksik
2023-05-28 19:35:38 -07:00
parent 26c315294a
commit 9d7e6e311e
3 changed files with 64 additions and 4 deletions
+45 -4
View File
@@ -1,6 +1,47 @@
fn main() {
println!("Hello, world!");
use std::thread;
let x = 5;
println!("The value of x is: {}", x);
use rppal::gpio::{Gpio, Level, Pin};
const BUTTON_PIN: u8 = 17;
// with internal pull up resistor, we get a low reading as
// the electrons flow to ground due to the voltage difference and less resistance path.
// so the mcu reads low when the button is pressed.
fn is_button_pressed(pin: &rppal::gpio::InputPin) -> bool {
pin.is_low()
}
fn is_button_pressed2(pin: &rppal::gpio::InputPin) -> bool {
match pin.read() {
Ok(Level::Low) => true,
Ok(Level::High) => false,
Err(_) => {
println!("Error reading pin");
false
},
}
}
fn read_loop(pin: &rppal::gpio::InputPin) {
loop {
if is_button_pressed(pin) {
println!("Button is pressed mate!");
}
thread::sleep(Duration::from_secs(1));
}
}
fn main() -> Result<(), rppal::gpio::Error> {
println!("Hello world");
let gpio = Gpio::new()?;
let pin = gpio.get(BUTTON_PIN)?.into_input_pullup();
// spawn a thread to read the pin
thread::spawn(|| {
read_loop(&pin);
});
Ok(())
}