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 and value of each GPIO line for the provided chip
18     /// in the form "off=<0|1>"
19     line_values: Vec<String>,
20 }
21 
22 // Use like:
23 //   muiltioutput /dev/gpiochip0 0=1 1=1 2=0 3=1 4=0
24 //
25 // to set lines 0, 1, & 3 high
26 //              2 & 4 low
27 //
do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error>28 fn do_main(args: Cli) -> std::result::Result<(), gpio_cdev::Error> {
29     let mut chip = Chip::new(args.chip)?;
30     let mut offsets = Vec::new();
31     let mut values = Vec::new();
32 
33     for arg in args.line_values {
34         let lv: Vec<&str> = arg.split("=").collect();
35         offsets.push(lv[0].parse::<u32>().unwrap());
36         values.push(lv[1].parse::<u8>().unwrap());
37     }
38 
39     // NOTE: we set the default values to the desired states so
40     // setting them separately is not required
41     let _handle =
42         chip.get_lines(&offsets)?
43             .request(LineRequestFlags::OUTPUT, &values, "multioutput")?;
44 
45     println!("Output lines being driven... Enter to exit");
46     let mut buf = String::new();
47     ::std::io::stdin().read_line(&mut buf)?;
48 
49     Ok(())
50 }
51 
main() -> CliResult52 fn main() -> CliResult {
53     let args = Cli::from_args();
54     do_main(args).or_else(|e| {
55         error!("{:?}", e);
56         Ok(())
57     })
58 }
59