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, LineRequestFlags};
10 use quicli::prelude::*;
11 use std::thread::sleep;
12 use std::time::{Duration, Instant};
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 line for the provided chip
20     line: u32,
21     /// Period in milliseconds
22     period_ms: u64,
23     /// Duration over which to blink in milliseconds
24     duration_ms: 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 
30     // NOTE: we set the default value to the desired state so
31     // setting it separately is not required
32     let handle = chip
33         .get_line(args.line)?
34         .request(LineRequestFlags::OUTPUT, 1, "blinky")?;
35 
36     let duration = Duration::from_millis(args.duration_ms);
37     let start_time = Instant::now();
38     while start_time.elapsed() < duration {
39         sleep(Duration::from_millis(args.period_ms));
40         handle.set_value(0)?;
41         sleep(Duration::from_millis(args.period_ms));
42         handle.set_value(1)?;
43     }
44 
45     Ok(())
46 }
47 
main() -> CliResult48 fn main() -> CliResult {
49     let args = Cli::from_args();
50     do_main(args).or_else(|e| {
51         error!("{:?}", e);
52         Ok(())
53     })
54 }
55