1 // Copyright (c) 2018 The rust-gpio-cdev Project Developers.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 use futures::stream::StreamExt;
10 use gpio_cdev::{AsyncLineEventHandle, Chip, EventRequestFlags, LineRequestFlags};
11 use structopt::StructOpt;
12
13 #[derive(Debug, StructOpt)]
14 struct Cli {
15 /// The gpiochip device (e.g. /dev/gpiochip0)
16 chip: String,
17 /// The offset of the GPIO line for the provided chip
18 line: u32,
19 }
20
do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error>21 async fn do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error> {
22 let mut chip = Chip::new(args.chip)?;
23 let line = chip.get_line(args.line)?;
24 let mut events = AsyncLineEventHandle::new(line.events(
25 LineRequestFlags::INPUT,
26 EventRequestFlags::BOTH_EDGES,
27 "gpioevents",
28 )?)?;
29
30 loop {
31 match events.next().await {
32 Some(event) => println!("{:?}", event?),
33 None => break,
34 };
35 }
36
37 Ok(())
38 }
39
40 #[tokio::main]
main()41 async fn main() {
42 let args = Cli::from_args();
43 do_main(args).await.unwrap();
44 }
45