gpio_utils/commands/
gpio_poll.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 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    // set the pin direction
35    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}