|
| 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 | +} |
0 commit comments