Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple mio example #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!"),
}
}
}
}