gpio_utils/commands/
gpio_write.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::GpioWriteOptions;
11use std::process::exit;
12use sysfs_gpio::Direction;
13
14pub fn main(config: &GpioConfig, opts: &GpioWriteOptions) {
15    let pin_config = match config.get_pin(opts.pin) {
16        Some(pin) => pin,
17        None => {
18            println!("Unable to find config entry for pin '{}'", opts.pin);
19            exit(1);
20        }
21    };
22
23    let pin = pin_config.get_pin();
24    pin.set_direction(Direction::Out).unwrap_or_else(|e| {
25        println!("Error setting GPIO direction: {:?}", e);
26        exit(1)
27    });
28    pin.set_value(opts.value).unwrap_or_else(|e| {
29        println!("There was an error writing to the gpio: {:?}", e);
30        exit(1);
31    });
32}