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 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     /// The value to write
20     value: u8,
21 }
22 
do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error>23 fn do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error> {
24     let mut chip = Chip::new(args.chip)?;
25 
26     // NOTE: we set the default value to the desired state so
27     // setting it separately is not required. The LineHandle
28     // instance that is returned by request must be owned by a
29     // variable for the duration of the time that the line will
30     // be used. If the instance is not assigned to a variable,
31     // then the LineHandle will be immediately dropped after
32     // request returns and the pin will appear to do nothing.
33     let _handle =
34         chip.get_line(args.line)?
35             .request(LineRequestFlags::OUTPUT, args.value, "driveoutput")?;
36 
37     println!("Output being driven... Enter to exit");
38     let mut buf = String::new();
39     ::std::io::stdin().read_line(&mut buf)?;
40 
41     Ok(())
42 }
43 
main() -> CliResult44 fn main() -> CliResult {
45     let args = Cli::from_args();
46     do_main(args).or_else(|e| {
47         error!("{:?}", e);
48         Ok(())
49     })
50 }
51