-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
52 lines (41 loc) · 1.48 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//basis of this try is the examples from sysfs_gpio crate here: https://crates.io/crates/sysfs_gpio
extern crate sysfs_gpio;
use std::env;
use sysfs_gpio::Pin;
use sysfs_gpio::Direction::High;
use std::time::Duration;
use std::thread::sleep;
use std::process::Command;
fn poll (pin: u64) -> sysfs_gpio::Result<()> {
let input = Pin::new(pin);
input.with_exported(|| {
input.set_direction(High)?; //set pin to High in case it starts at Low
let mut prev_val: u8 = 255;
loop {
let val = input.get_value()?; // get the value of the pin
if val != prev_val { //if the value changes then print the state of the pins
println!("Pin State: {}", if val == 0 { "Low" } else { "High" });
if val == 0 {
Command::new("./pushtotext") //if the pin value is low then execute rust app to send a text message
.spawn()
.expect("pushtotext command failed to start");
}
prev_val = val;
}
sleep(Duration::from_millis(1000));
}
})
}
fn main() {
let args: Vec<String> = env::args().collect();
loop {
println!("arg length: {}", args.len());
match args[1].parse::<u64>() {
Ok(pin) => match poll(pin) {
Ok(()) => println!("polling complete!"),
Err(err) => println!("Error: {}", err),
},
Err(_) => println!("Usage: ./interrupt <pin>MatchErr"),
}
}
}