Skip to content

Commit 8042f12

Browse files
committed
Add functionality for status sub command
The status command takes an optional pin argument. If specified it will print the GPIO number, export status, direction, active low status, names, and value. If the pin argument is not specified it will print all of the above for every pin that is configured.
1 parent 632a47f commit 8042f12

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

src/commands/gpio_status.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use options::GpioStatusOptions;
2+
use config::GpioConfig;
3+
use config::PinConfig;
4+
use sysfs_gpio::Direction;
5+
use std::process::exit;
6+
7+
pub fn main(config: &GpioConfig, opts: &GpioStatusOptions) {
8+
match opts.pin {
9+
Some(ref pin_name) => {
10+
let pin_config = match config.get_pin(&pin_name[..]) {
11+
Some(pin) => pin,
12+
None => {
13+
println!("Unable to find config entry for pin '{}'", pin_name);
14+
exit(1)
15+
}
16+
};
17+
print_pin_header();
18+
print_pin_row(&pin_config, true);
19+
},
20+
None => {
21+
print_pin_header();
22+
for (pos, pin) in config.get_pins().iter().enumerate() {
23+
print_pin_row(pin, pos == (config.get_pins().len() - 1));
24+
}
25+
}
26+
}
27+
}
28+
29+
30+
fn print_pin_header(){
31+
println!("| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
32+
"Number",
33+
"Exported",
34+
"Direction",
35+
"Active Low",
36+
"Names",
37+
"Value");
38+
print_row_sep(false);
39+
}
40+
41+
fn print_row_sep(is_last: bool){
42+
let col_sep = if is_last {"-"} else {"+"};
43+
println!("{}{:->13}{:->13}{:->13}{:->13}{:->13}{:->13}",
44+
col_sep,
45+
col_sep,
46+
col_sep,
47+
col_sep,
48+
col_sep,
49+
col_sep,
50+
col_sep);
51+
}
52+
53+
fn 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!("| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
72+
pin_config.num,
73+
pin_config.export,
74+
direction,
75+
pin_config.active_low,
76+
name,
77+
value);
78+
} else {
79+
println!("| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
80+
"",
81+
"",
82+
"",
83+
"",
84+
name,
85+
"");
86+
87+
}
88+
}
89+
print_row_sep(is_last);
90+
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod gpio_write;
77
pub mod gpio_unexport;
88
pub mod gpio_unexportall;
99
pub mod gpio_poll;
10+
pub mod gpio_status;

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ fn main() {
218218
};
219219
gpio_unexportall::main(&cfg, &unexportall_options);
220220
}
221-
("status", Some(_)) => {}
221+
("status", Some(m)) => {
222+
let status_options = GpioStatusOptions {
223+
gpio_opts: gpio_options,
224+
pin: m.value_of("pin").map(|pin| String::from(pin)),
225+
};
226+
gpio_status::main(&cfg, &status_options);
227+
}
222228
_ => {}
223229
}
224230
}

0 commit comments

Comments
 (0)