extern crate gpio_cdev;
extern crate nix;
#[macro_use] extern crate quicli;
use gpio_cdev::*;
use quicli::prelude::*;
use nix::poll::*;
use std::os::unix::io::{AsRawFd};
type PollEventFlags = nix::poll::EventFlags;
#[derive(Debug, StructOpt)]
struct Cli {
chip: String,
lines: Vec<u32>,
}
fn do_main(args: Cli) -> errors::Result<()> {
let mut chip = Chip::new(args.chip)?;
let mut evt_handles: Vec<LineEventHandle> = args.lines.into_iter().map(|off| {
let line = chip.get_line(off).unwrap();
line.events(LineRequestFlags::INPUT, EventRequestFlags::BOTH_EDGES,
"monitor").unwrap()
}).collect();
let mut pollfds: Vec<PollFd> = evt_handles.iter().map(|h| {
PollFd::new(h.as_raw_fd(), PollEventFlags::POLLIN | PollEventFlags::POLLPRI)
}).collect();
loop {
if poll(&mut pollfds, -1)? == 0 {
println!("Timeout?!?");
}
else {
for i in 0..pollfds.len() {
if let Some(revts) = pollfds[i].revents() {
let h = &mut evt_handles[i];
if revts.contains(PollEventFlags::POLLIN) {
let event = h.get_event()?;
println!("[{}] {:?}", h.line().offset(), event);
let val = h.get_value()?;
println!(" {}", val);
}
else if revts.contains(PollEventFlags::POLLPRI) {
println!("[{}] Got a POLLPRI", h.line().offset());
}
}
}
}
}
}
main!(|args: Cli| {
match do_main(args) {
Ok(()) => {},
Err(e) => {
println!("Error: {:?}", e);
}
}
});