Skip to content

Commit

Permalink
Try #49:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Sep 24, 2021
2 parents 0296e8e + 348ddda commit f8b169f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/mio.rs
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!"),
}
}
}
}

0 comments on commit f8b169f

Please sign in to comment.