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 gpio_cdev::{Chip, EventRequestFlags, EventType, LineRequestFlags};
10 use quicli::prelude::*;
11 use std::thread::sleep;
12 use std::time::Duration;
13 use structopt::StructOpt;
14 
15 #[derive(Debug, StructOpt)]
16 struct Cli {
17     /// The gpiochip device (e.g. /dev/gpiochip0)
18     chip: String,
19     /// The offset of the GPIO input line for the provided chip
20     inputline: u32,
21     /// The offset of the GPIO output line for the provided chip
22     outputline: u32,
23     /// Sleep time after each actuation in milliseconds
24     sleeptime: u64,
25 }
26 
do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error>27 fn do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error> {
28     let mut chip = Chip::new(args.chip)?;
29     let input = chip.get_line(args.inputline)?;
30     let output = chip.get_line(args.outputline)?;
31     let output_handle = output.request(LineRequestFlags::OUTPUT, 0, "tit_for_tat")?;
32 
33     // To show off the buffering characteristics of the new interface we introduce a delay
34     // after each change is handled.  When we fall behind, we will "replay" the input
35     // events
36     for event in input.events(
37         LineRequestFlags::INPUT,
38         EventRequestFlags::BOTH_EDGES,
39         "tit_for_tat",
40     )? {
41         let evt = event?;
42         println!("{:?}", evt);
43         match evt.event_type() {
44             EventType::RisingEdge => {
45                 output_handle.set_value(1)?;
46                 sleep(Duration::from_millis(args.sleeptime));
47             }
48             EventType::FallingEdge => {
49                 output_handle.set_value(0)?;
50                 sleep(Duration::from_millis(args.sleeptime));
51             }
52         }
53     }
54 
55     Ok(())
56 }
57 
main() -> CliResult58 fn main() -> CliResult {
59     let args = Cli::from_args();
60     do_main(args).or_else(|e| {
61         error!("{:?}", e);
62         Ok(())
63     })
64 }
65