From d877ec774fa2a6ad573b793a7b5c973e88645e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=B6rner?= Date: Thu, 30 Mar 2017 21:25:26 +0200 Subject: [PATCH] added led5 --- .gitignore | 3 ++ V1/led5/Cargo.lock | 66 +++++++++++++++++++++++ V1/led5/Cargo.toml | 8 +++ V1/led5/src/main.rs | 128 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 V1/led5/Cargo.lock create mode 100644 V1/led5/Cargo.toml create mode 100644 V1/led5/src/main.rs diff --git a/.gitignore b/.gitignore index f805e81..958a081 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ # Debug files *.dSYM/ *.su + +# Rust +target/ diff --git a/V1/led5/Cargo.lock b/V1/led5/Cargo.lock new file mode 100644 index 0000000..152edd2 --- /dev/null +++ b/V1/led5/Cargo.lock @@ -0,0 +1,66 @@ +[root] +name = "led5" +version = "0.1.0" +dependencies = [ + "chan 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", + "chan-signal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bit-set" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bit-vec" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "chan" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "chan-signal" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "chan 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9bf6104718e80d7b26a68fdbacff3481cfc05df670821affc7e9cbc1884400c" +"checksum bit-vec 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5b97c2c8e8bbb4251754f559df8af22fb264853c7d009084a576cdf12565089d" +"checksum chan 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)" = "f93bfe971116428a9066c1c3c69a09ae3ef69432f8418be28ab50f96783e6a50" +"checksum chan-signal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0f3bb6c3bc387004ad914f0c5b7f33ace8bf7604bbec35f228b1a017f52cd3a0" +"checksum lazy_static 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4732c563b9a21a406565c4747daa7b46742f082911ae4753f390dc9ec7ee1a97" +"checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135" +"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" diff --git a/V1/led5/Cargo.toml b/V1/led5/Cargo.toml new file mode 100644 index 0000000..345cf6b --- /dev/null +++ b/V1/led5/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "led5" +version = "0.1.0" +authors = ["Simon Wörner "] + +[dependencies] +chan = ">=0.1.19" +chan-signal = ">=0.2.0" diff --git a/V1/led5/src/main.rs b/V1/led5/src/main.rs new file mode 100644 index 0000000..fea4313 --- /dev/null +++ b/V1/led5/src/main.rs @@ -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)); + } +}