gpio_utils/commands/
gpio_status.rs

1// Copyright (c) 2016, The gpio-utils Authors.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/license/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
9use config::GpioConfig;
10use config::PinConfig;
11use options::GpioStatusOptions;
12use std::process::exit;
13use sysfs_gpio::Direction;
14
15pub fn main(config: &GpioConfig, opts: &GpioStatusOptions) {
16    match opts.pin {
17        Some(ref pin_name) => {
18            let pin_config = match config.get_pin(pin_name) {
19                Some(pin) => pin,
20                None => {
21                    println!("Unable to find config entry for pin '{}'", pin_name);
22                    exit(1)
23                }
24            };
25            print_pin_header();
26            print_pin_row(&pin_config, true);
27        }
28        None => {
29            print_pin_header();
30            for (pos, pin) in config.get_pins().iter().enumerate() {
31                print_pin_row(pin, pos == (config.get_pins().len() - 1));
32            }
33        }
34    }
35}
36
37fn print_pin_header() {
38    println!(
39        "| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
40        "Number", "Exported", "Direction", "Active Low", "Names", "Value"
41    );
42    print_row_sep(false);
43}
44
45fn print_row_sep(is_last: bool) {
46    let col_sep = if is_last { "-" } else { "+" };
47    println!(
48        "{}{:->13}{:->13}{:->13}{:->13}{:->13}{:->13}",
49        col_sep, col_sep, col_sep, col_sep, col_sep, col_sep, col_sep
50    );
51}
52
53fn print_pin_row(pin_config: &PinConfig, is_last: bool) {
54    let direction = match pin_config.direction {
55        Direction::In => "In",
56        Direction::Out => "Out",
57        Direction::High => "High",
58        Direction::Low => "Low",
59    };
60
61    let value = match pin_config.get_pin().get_value() {
62        Ok(value) => value,
63        Err(e) => {
64            println!("ERROR: {:?}", e);
65            exit(1);
66        }
67    };
68
69    for (pos, name) in pin_config.names.iter().enumerate() {
70        if pos == 0 {
71            println!(
72                "| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
73                pin_config.num, pin_config.export, direction, pin_config.active_low, name, value
74            );
75        } else {
76            println!(
77                "| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
78                "", "", "", "", name, ""
79            );
80        }
81    }
82    print_row_sep(is_last);
83}