18 Commits

Author SHA1 Message Date
Simon Wörner
15de785ee4 Revert "V1: switch.c" 2017-04-03 16:05:41 +02:00
Siegfried Kienzle
1a7e62f5e2 Merge pull request #3 from sikienzl/v1-switch
V1: switch.c
2017-04-02 09:01:35 +02:00
Siegfried Kienzle
2289644aec Merge pull request #2 from SWW13/v1-led5
V1: led5
2017-04-01 15:38:10 +02:00
Siegfried Kienzle
fbbb82740a Merge pull request #1 from SWW13/v1-led-sh
V1: led.sh
2017-04-01 14:53:24 +02:00
Simon Wörner
48b4310a67 faster tick rate for button check 2017-03-31 18:26:00 +02:00
Simon Wörner
54a27c508a blink led only if enabled 2017-03-31 18:18:37 +02:00
Simon Wörner
b4cc1c9b09 on/off on button press 2017-03-31 18:16:49 +02:00
Simon Wörner
ce16cf7ab1 fixed watchdog 2017-03-31 18:09:29 +02:00
Simon Wörner
8443acb129 trim read value 2017-03-31 17:57:09 +02:00
Simon Wörner
ae814dbe08 send panic event 2017-03-31 12:40:52 +02:00
Simon Wörner
4186c025fa fixed signale handling 2017-03-31 12:38:36 +02:00
Simon Wörner
6d5cc0354c fixed read, code clean uo 2017-03-31 10:23:42 +02:00
Simon Wörner
f399a80bea fixed sync and sleep, added debug output, clean up 2017-03-31 10:16:51 +02:00
Simon Wörner
70e994eef9 fixed file write 2017-03-30 21:55:12 +02:00
Simon Wörner
80934a5c70 added ci scripts for rust 2017-03-30 21:55:00 +02:00
Simon Wörner
d877ec774f added led5 2017-03-30 21:25:26 +02:00
Simon Wörner
d7c15c5c77 set gpio direction 2017-03-30 17:09:04 +02:00
Simon Wörner
69fc6e0797 added led.sh and gitlab-ci shellcheck 2017-03-30 16:40:56 +02:00
11 changed files with 339 additions and 121 deletions

3
.gitignore vendored
View File

@@ -31,3 +31,6 @@
# Debug files
*.dSYM/
*.su
# Rust
target/

26
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,26 @@
image: scorpil/rust:stable
stages:
- test
cargo:test:
stage: test
script:
- ci/run-cargo-test.sh
rustfmt:
stage: test
script:
- ci/run-rustfmt.sh
shellcheck:
stage: test
image: ubuntu:yakkety
before_script:
- apt-get update && apt-get install -y shellcheck
- locale-gen en_US.UTF-8
- export LANG=en_US.UTF-8
- export LANGUAGE=en_US:en
- export LC_ALL=en_US.UTF-8
script:
- ci/run-shellcheck.sh

View File

@@ -1,2 +1,2 @@
# resy-ss17-grp1
# resy-ss17-template
Template for grp repositories

51
V1/led.sh Executable file
View File

@@ -0,0 +1,51 @@
#!/usr/bin/env bash
# usage: led.sh <port>
set -o xtrace
# config default
GPIO_PORT="${1:-18}"
function export_port() {
port="${1}"
echo "${port}" > /sys/class/gpio/export
}
function unexport_port() {
port="${1}"
echo "${port}" > /sys/class/gpio/unexport
}
function set_direction() {
port="${1}"
value="${2}"
echo "${value}" > "/sys/class/gpio/gpio${port}/direction"
}
function set_value() {
port="${1}"
value="${2}"
echo "${value}" > "/sys/class/gpio/gpio${port}/value"
}
function on() {
port="${1}"
set_value "${port}" "0"
}
function off() {
port="${1}"
set_value "${port}" "1"
}
# set exit trap
trap 'unexport_port "${GPIO_PORT}"' EXIT
# init GPIO port
export_port "${GPIO_PORT}"
set_direction "${GPIO_PORT}" "out"
# loop led on/off
while true; do
on "${GPIO_PORT}"
sleep 1
off "${GPIO_PORT}"
sleep 1
done

66
V1/led5/Cargo.lock generated Normal file
View File

@@ -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"

8
V1/led5/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "led5"
version = "0.1.0"
authors = ["Simon Wörner <git@simon-woerner.de>"]
[dependencies]
chan = ">=0.1.19"
chan-signal = ">=0.2.0"

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

@@ -0,0 +1,152 @@
#[macro_use]
extern crate chan;
extern crate chan_signal;
use std::io::prelude::*;
use std::fs::File;
use std::thread;
use std::thread::JoinHandle;
use std::path::Path;
use std::time::Duration;
use chan_signal::Signal;
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 write(path: &str, value: &str) {
let mut file = File::create(Path::new(path))
.expect(format!("Open file '{}' failed", path).as_str());
file.write_all(value.as_bytes())
.expect(format!("Write value '{}' to '{}' file failed", value, path).as_str());
#[cfg(debug_assertions)]
println!("Wrote value '{}' to '{}'.", value, path);
}
fn read(path: &str) -> String {
let mut file = File::open(Path::new(path))
.expect(format!("Open file '{}' failed", path).as_str());
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect(format!("Read from '{}' file failed", path).as_str());
#[cfg(debug_assertions)]
println!("Read value '{}' from '{}'.", contents, path);
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 {
String::from(read(format!("/sys/class/gpio/gpio{}/value", port).as_str()).trim())
}
fn main() {
let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);
let (sdone, rdone) = chan::sync(0);
let (spanic, rpanic) = chan::sync(0);
let worker = thread::Builder::new()
.name("worker".to_string())
.spawn(move || run(rdone))
.expect("Create worker thread failed");
let watchdog = thread::Builder::new()
.name("watchdog".to_string())
.spawn(move || watchdog(worker, spanic))
.expect("Create watchdog thread failed");
chan_select! {
rpanic.recv() => { },
signal.recv() -> signal => {
#[cfg(debug_assertions)]
println!("received signal: {:?}", signal);
sdone.send(());
}
}
watchdog.join().expect("Watchdog thread paniced");
}
fn watchdog(thread: JoinHandle<()>, _spanic: chan::Sender<()>) {
thread.join().expect("Thread paniced");
}
fn run(rdone: chan::Receiver<()>) {
// 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 btn_tick = chan::tick(Duration::from_millis(50));
let led_tick = chan::tick(Duration::from_millis(1000 / 5));
let mut btn_last = false;
let mut led_on = false;
let mut enabled = true;
loop {
chan_select! {
btn_tick.recv() => {
//#[cfg(debug_assertions)]
//println!("btn_tick");
// switch enabled if button state changed
let btn = get_value(GPIO_PORT_BTN) == GPIO_BTN_ON;
if btn && btn != btn_last {
enabled = !enabled;
}
btn_last = btn;
},
led_tick.recv() => {
#[cfg(debug_assertions)]
println!("led_tick");
// blink led if enabled
if enabled {
led_on = !led_on;
} else {
led_on = false;
}
// set led state
if led_on {
set_value(GPIO_PORT_LED, GPIO_LED_ON);
}
else {
set_value(GPIO_PORT_LED, GPIO_LED_OFF);
}
},
rdone.recv() => {
// unexport
unexport(GPIO_PORT_BTN);
unexport(GPIO_PORT_LED);
return;
},
}
}
}

View File

@@ -1,120 +0,0 @@
/*
* switch.c
* How to register an button and count this
* author: Simon Wörner, Manuel Vögele,
* Siegfried Kienzle
* 24-March-2017
*
* */
#define BUTTON "17"
#define IN "in\n"
#define OUT "out\n"
#define HIGH "1\n"
#define LOW "0\n"
#define MAXBYTES 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int writeIntoFile(char* value, char* path)
{
int fd;
char buffer[3];
strcpy(buffer, value);
if((fd=open(path, O_RDWR | O_CREAT | O_TRUNC)) == -1) {
perror(NULL);
return EXIT_FAILURE;
}
if((write(fd, &buffer, sizeof(buffer))) == -1) {
perror("Cannot write into file");
return EXIT_FAILURE;
}
printf("Success");
close(fd);
return EXIT_SUCCESS;
}
int reserve(char* gpioPort)
{
char str[3];
strcpy(str, gpioPort);
strcat(str, "\n");
return writeIntoFile(str,"/sys/class/gpio/export");
}
int setDirection(char* gpioPort, char* inOut)
{
char *direc = "/direction";
char *gpioPath = "/sys/class/gpio/gpio";
char path[32];
strcpy(path, gpioPath);
strcat(path, gpioPort);
strcat(path, direc);
return writeIntoFile(inOut, path);
}
int setOutput(char* gpioPort, char* volt)
{
char *val = "/value";
char *gpioPath = "/sys/class/gpio/gpio";
char path[28];
strcpy(path, gpioPath);
strcat(path, gpioPort);
strcat(path, val);
return writeIntoFile(volt, path);
}
int mainLoop(char* gpioPort)
{
int fid, portValue, count, countButton;
char *val = "/value";
char *gpioPath = "/sys/class/gpio/gpio";
char path[28];
strcpy(path, gpioPath);
strcat(path, gpioPort);
strcat(path, val);
while(true)
{
if( (fid=open(path, O_RDONLY)) == -1)
perror("Fehler ");
if(readInput(fid) == 1)
{
countButton++;
printf("%d", countButton);
}
close(fid);
}
}
char[] readInput(int fid)
{
char buffer[MAXBYTES];
read(fid, &buffer, MAXBYTES);
return buffer;
}
int unexport(char* gpioport)
{
char str[3];
strcpy(str, gpioPort);
strcat(str, "\n");
return writeIntoFile(str,"/sys/class/gpio/unexport");
}
int main()
{
reserve(BUTTON);
setDirection(BUTTON, IN);
mainLoop(BUTTON);
unexport(BUTTON);
return 0;
}

16
ci/run-cargo-test.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
ERROR=0
while IFS= read -r -d '' f; do
dir="$(dirname "${f}")"
echo "run 'cargo test' in ${dir}"
cd "${dir}" && cargo test
if [[ $? -ne 0 ]]; then
ERROR=1
fi
done < <(find . -type f -name 'Cargo.toml' -print0)
exit ${ERROR}

13
ci/run-rustfmt.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
ERROR=0
while IFS= read -r -d '' f; do
echo "${f}"
if [ "$(rustfmt --write-mode=diff "$f")" != $'' ] ; then
ERROR=1
fi
done < <(find . -type f -name '*.rs' -print0)
exit ${ERROR}

3
ci/run-shellcheck.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env bash
find . -type f -name '*.sh' -print0 | xargs -n 1 -0 shellcheck --color