added led5

This commit is contained in:
Simon Wörner
2017-03-30 21:25:26 +02:00
parent d7c15c5c77
commit d877ec774f
4 changed files with 205 additions and 0 deletions

128
V1/led5/src/main.rs Normal file
View File

@@ -0,0 +1,128 @@
#[macro_use]
extern crate chan;
extern crate chan_signal;
use std::io::prelude::*;
use std::fs::File;
use std::thread;
use std::path::Path;
use std::time::Duration;
use chan_signal::Signal;
// static GPIO_BASE_PATH: &'static str = "/sys/class/gpio";
// static GPIO_PATH: &'static str = "gpio{}/";
// static GPIO_EXPORT: &'static str = "export";
// static GPIO_UNEXPORT: &'static str = "unexport";
// static GPIO_VALUE: &'static str = "value";
// static GPIO_DIRECTION: &'static str = "direction";
// static GPIO_BASE_PATH: &'static Path = Path::new("/sys/class/gpio");
// static GPIO_EXPORT_PATH: &'static Path = GPIO_BASE_PATH.join("export").as_path();
// static GPIO_UNEXPORT_PATH: &'static Path = GPIO_BASE_PATH.join("unexport").as_path();
// static GPIO_PATH: &'static Path = GPIO_BASE_PATH.join("gpio{}").as_path();
// static GPIO_VALUE_PATH: &'static Path = GPIO_PATH.join("value").as_path();
// static GPIO_DIRECTION_PATH: &'static Path = GPIO_PATH.join("direction").as_path();
// str0 = "asd"
// str1 = concat!("Test", "Test");
// str2 concat!(str1, "test");
static GPIO_PORT_BTN: &'static str = "17";
static GPIO_PORT_LED: &'static str = "18";
static GPIO_DIRECTION_IN: &'static str = "in";
static GPIO_DIRECTION_OUT: &'static str = "out";
static GPIO_BTN_ON: &'static str = "0";
//static GPIO_BTN_OFF: &'static str = "1";
static GPIO_LED_ON: &'static str = "0";
static GPIO_LED_OFF: &'static str = "1";
fn open(path: &str) -> File {
return File::open(Path::new(path))
.expect(format!("Open file '{}' failed", path).as_str());
}
fn write(path: &str, value: &str) {
let mut file = open(path);
file.write_all(value.as_bytes())
.expect(format!("Write value '{}' to '{}' file failed", value, path).as_str());
}
fn read(path: &str) -> String {
let mut file = open(path);
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect(format!("Read from '{}' file failed", path).as_str());
contents
}
fn export(port: &str) {
write("/sys/class/gpio/export", port)
}
fn unexport(port: &str) {
write("/sys/class/gpio/unexport", port)
}
fn set_direction(port: &str, direction: &str) {
write(format!("/sys/class/gpio/gpio{}/direction", port).as_str(), direction);
}
fn set_value(port: &str, value: &str) {
write(format!("/sys/class/gpio/gpio{}/value", port).as_str(), value);
}
fn get_value(port: &str) -> String {
read(format!("/sys/class/gpio/gpio{}/value", port).as_str())
}
fn main() {
let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);
let (sdone, rdone) = chan::sync(0);
thread::spawn(move || run(sdone));
// Wait for a signal or for work to be done.
chan_select! {
signal.recv() -> signal => {
println!("received signal: {:?}", signal);
unexport(GPIO_PORT_BTN);
unexport(GPIO_PORT_LED);
},
rdone.recv() => {
println!("Program completed normally.");
}
}
}
fn run(_sdone: chan::Sender<()>) {
// init
export(GPIO_PORT_BTN);
export(GPIO_PORT_LED);
set_direction(GPIO_PORT_BTN, GPIO_DIRECTION_IN);
set_direction(GPIO_PORT_LED, GPIO_DIRECTION_OUT);
let mut on = false;
loop {
let btn = get_value(GPIO_PORT_BTN);
if btn == GPIO_BTN_ON {
on = !on;
} else {
on = false;
}
if on {
set_value(GPIO_PORT_LED, GPIO_LED_ON);
}
else {
set_value(GPIO_PORT_LED, GPIO_LED_OFF);
}
thread::sleep(Duration::from_secs(1 / 5));
}
}