-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
extern crate sysfs_gpio; | ||
use sysfs_gpio::{Direction, Edge, Pin}; | ||
|
||
extern crate mio; | ||
use mio::{Events, Poll, PollOpt, Ready, Token}; | ||
|
||
const GPIO_EDGE: Token = Token(0); | ||
|
||
fn get_evented_pin(pin: u16) -> Result<sysfs_gpio::AsyncPinPoller, sysfs_gpio::Error> { | ||
let pin = Pin::new(pin.into()); | ||
pin.set_direction(Direction::In)?; | ||
pin.set_edge(Edge::BothEdges)?; | ||
pin.get_async_poller() | ||
} | ||
|
||
fn main() { | ||
// Construct a new `Poll` handle as well as the `Events` we'll store into | ||
let poll = Poll::new().unwrap(); | ||
let mut events = Events::with_capacity(8); | ||
|
||
let evented_pin = get_evented_pin(49).unwrap(); | ||
|
||
// Register the Pin's Evented interface with `Poll` | ||
poll.register( | ||
&evented_pin, | ||
GPIO_EDGE, | ||
Ready::readable() | Ready::writable(), | ||
PollOpt::edge(), | ||
).unwrap(); | ||
|
||
// Linux gives you an event by default so disarm the first one | ||
poll.poll(&mut events, None).unwrap(); | ||
|
||
loop { | ||
// wait for events, which all should be pin toggles | ||
poll.poll(&mut events, None).unwrap(); | ||
|
||
for event in &events { | ||
match event.token() { | ||
GPIO_EDGE => println!("Edge Detected"), | ||
_ => println!("Unhandled event!"), | ||
} | ||
} | ||
} | ||
} |