forked from rust-embedded/rust-sysfs-gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupt.rs
50 lines (46 loc) · 1.52 KB
/
interrupt.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
// Copyright 2015, Paul Osborne <osbpau@gmail.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
extern crate sysfs_gpio;
use sysfs_gpio::{Direction, Edge, Pin};
use std::env;
use std::io::prelude::*;
use std::io::stdout;
fn interrupt(pin: u64) -> sysfs_gpio::Result<()> {
let input = Pin::new(pin);
input.with_exported(|| {
try!(input.set_direction(Direction::In));
try!(input.set_edge(Edge::BothEdges));
let mut poller = try!(input.get_poller());
loop {
match try!(poller.poll(1000)) {
Some(value) => println!("{}", value),
None => {
let mut stdout = stdout();
try!(stdout.write(b"."));
try!(stdout.flush());
}
}
}
})
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("Usage: ./interrupt <pin>");
} else {
match args[1].parse::<u64>() {
Ok(pin) => {
match interrupt(pin) {
Ok(()) => println!("Interrupting Complete!"),
Err(err) => println!("Error: {}", err),
}
}
Err(_) => println!("Usage: ./interrupt <pin>"),
}
}
}