adding basic program
This commit is contained in:
Generated
+18
@@ -2,6 +2,24 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.144"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rppal"
|
||||||
|
version = "0.14.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "612e1a22e21f08a246657c6433fe52b773ae43d07c9ef88ccfc433cc8683caba"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-simple-gpio"
|
name = "rust-simple-gpio"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rppal",
|
||||||
|
]
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
rppal = "0.14.1"
|
||||||
|
|||||||
+45
-4
@@ -1,6 +1,47 @@
|
|||||||
fn main() {
|
use std::thread;
|
||||||
println!("Hello, world!");
|
|
||||||
|
|
||||||
let x = 5;
|
use rppal::gpio::{Gpio, Level, Pin};
|
||||||
println!("The value of x is: {}", x);
|
|
||||||
|
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