gpio_utils/commands/
gpio_poll.rs1use config::GpioConfig;
10use options::GpioPollOptions;
11use std::process::exit;
12use sysfs_gpio::Edge;
13
14pub fn main(config: &GpioConfig, opts: &GpioPollOptions) {
15 let timeout = opts.timeout.unwrap_or(-1);
16 let pin_config = match config.get_pin(opts.pin) {
17 Some(pin) => pin,
18 None => {
19 println!("Unable to find config entry for pin '{}'", opts.pin);
20 exit(1)
21 }
22 };
23 let pin = pin_config.get_pin();
24 let edge = match opts.edge {
25 "rising" => Edge::RisingEdge,
26 "falling" => Edge::FallingEdge,
27 "both" => Edge::BothEdges,
28 other => {
29 println!("Unexpected edge value: {}", other);
30 exit(1);
31 }
32 };
33
34 pin.set_edge(edge).unwrap_or_else(|e| {
36 println!("Error setting edge on pin: {:?}", e);
37 exit(1);
38 });
39
40 let mut poller = pin.get_poller().unwrap_or_else(|e| {
41 println!("Error creating pin poller: {:?}", e);
42 exit(1);
43 });
44 match poller.poll(timeout) {
45 Ok(Some(value)) => {
46 println!("{}", value);
47 exit(0);
48 }
49 Ok(None) => {
50 println!("TIMEOUT");
51 exit(2)
52 }
53 Err(e) => {
54 println!("Error on Poll: {:?}", e);
55 exit(1);
56 }
57 }
58}