adding basic program
This commit is contained in:
+45
-4
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user