Compare commits

..

14 commits
master ... push

Author SHA1 Message Date
Martin Ness
a90280ff97 Arduino Code: auslagerung in neue Funktion printState 2016-05-01 14:03:34 +02:00
Martin Ness
c7c3152108 Syntax FIX 2016-05-01 13:53:10 +02:00
Martin Ness
8efaf7df01 Komentare hinzugefügt 2016-05-01 13:52:07 +02:00
Martin Ness
18d0aa878a Warte bis Getät vorhanden 2016-05-01 13:51:57 +02:00
Martin Ness
d77264ba9e whitespace fix 2015-10-07 01:47:23 +02:00
Martin Ness
573e1ec66f shellcheck SC2162 - https://github.com/koalaman/shellcheck/wiki/SC2162 2015-10-07 01:43:54 +02:00
Martin Ness
096bb37f05 use run-parts 2015-10-07 01:32:09 +02:00
Martin Ness
f8e447a4b8 remove .sh in the name from push.d/skeleton.sh 2015-10-07 01:31:32 +02:00
Martin Ness
e92963e932 merge bugfix 2015-09-28 23:55:28 +02:00
Martin Ness
aab9eb3700 script added 2015-09-28 23:06:01 +02:00
Martin Ness
e813711ad8 FIX only newline 2015-09-24 18:12:38 +02:00
Martin Ness
cddc1d675a Auf anfrage Status senden 2015-09-24 16:37:56 +02:00
Martin Ness
41cec53d48 cleanup 2015-09-24 16:36:45 +02:00
Martin Ness
9d34f7e60b push state changes 2015-09-24 15:13:55 +02:00
5 changed files with 92 additions and 88 deletions

View file

@ -1,45 +0,0 @@
SRC:=$(shell find status -regex '.*\.cpp')
OBJ:=$(patsubst %,%.o,$(SRC))
DEP:=$(patsubst %,%.d,$(SRC))
CC?=avr-gcc
CFLAGS?= -O2 -ggdb -std=gnu++11 -fomit-frame-pointer -fmerge-all-constants\
-faggressive-loop-optimizations -finline-functions -funsafe-loop-optimizations\
-ffreestanding -Wlogical-op -Wdouble-promotion -Wformat -Winit-self\
-Wmissing-include-dirs -Wswitch-default -Wpadded -Wswitch-enum -Wall\
-Wunused -Winline -Wuninitialized -Wstrict-overflow\
-Wfloat-equal -Wstack-protector -Wundef -Wvla\
-Wshadow -Wcast-align -Wpedantic -Wextra\
-Wpointer-arith -Wwrite-strings -Wtrampolines -Wpacked\
-Wconversion -Wdate-time -Waggregate-return\
-Wvector-operation-performance\
-Wredundant-decls -Wlong-long -Wvariadic-macros\
-Wdisabled-optimization -Wmissing-declarations -Wunsafe-loop-optimizations\
-pipe -Werror -fno-exceptions -fno-rtti\
-I/usr/share/arduino/hardware/arduino/cores/arduino\
-I/usr/share/arduino/hardware/arduino/variants/micro\
-I/usr/lib/avr/include/\
-DF_CPU=16000000L -MMD -DUSB_VID=0x2341 -DUSB_PID=0x8037 -DARDUINO=105 -D__PROG_TYPES_COMPAT__ -mmcu=atmega32u4
ARDUINO?=
PROGRAMMER?=
.PHONY: all clean check install build
all: build
# TODO : find a install commandline
#install: build
# avrdude
clean:
rm $(OBJ) $(DEP)
check:
$(CC) $(CFLAGS) -fsyntax-only $(SRC)
build: $(SRC)
$(CC) $(CFLAGS) $(SRC) -MMD -MP -o hackspace-status.sys
-include $(sort $(DEP))

View file

@ -1,4 +1,4 @@
hackerspace-status-arduino
==========================
see https://kraut.space/hswiki:projekte:2014:raumstatus_anzeige
see https://www.krautspace.de/hswiki:projekte:elektronikrunde:status_anzeige

13
push.d/skeleton Normal file
View file

@ -0,0 +1,13 @@
#!/bin/sh
case "$1" in
ON)
echo 'ON'
;;
HALF)
echo 'HALF'
;;
OFF)
echo 'OFF'
;;
esac

28
push.sh Executable file
View file

@ -0,0 +1,28 @@
#!/bin/sh -e
DEV='/dev/ttyUSB0'
DIR='push.d'
stty -F "$DEV" 9600 -brkint -icrnl -imaxbel -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
# Warte bis Device $DEV vorhanden ist
while [ ! -c "$DEV" ]; do
sleep 2
done
# sende beliebiges Zeichen an $DEV
echo '?' >> "$DEV" &
while true; do
# wenn $DEV vorhanden
if [ -c "$DEV" ]; then
# dann lese zustand
read -r state < "$DEV"
case "$state" in
ON|HALF|OFF)
run-parts --arg="$state" "$DIR"
;;
esac
else
# sonst warte
sleep 10
fi
done

View file

@ -1,23 +1,12 @@
#include <cstddef>
#include <climits>
#include "Arduino.h"
void setup();
void testLeds();
void loop();
void changeStateTo(char state_new);
bool transition();
void sendState();
unsigned long calcStateTime();
/*
* es gibt folgende Zustände:
* 0 - Aus
* 1 - An, aber auf dem weg zu aus
* 2 - An
*/
constexpr char STATE_OFF = 3;
constexpr char STATE_HALF = 1;
constexpr char STATE_ON = 2;
#define STATE_OFF 3
#define STATE_HALF 1
#define STATE_ON 2
/*
* Zeit wie lange in einem Zustände verharrt werden soll
@ -25,19 +14,22 @@ constexpr char STATE_ON = 2;
* TIME_HALF - Zeitspanne von Zustand 2 bis Wechsel zu Zustand 1
* TIME_OFF - Zeitspanne von Zustand 2 bis Wechsel zu Zustand 0
*/
constexpr int TIME_HALF = 5400000; // 1,5h
constexpr int TIME_OFF = 7200000; // 2h
#define TIME_HALF 5400000 // 1,5h
#define TIME_OFF 7200000 // 2h
// für Variablen Überlauf in calcStateTime
#define MAX_LONG 4294967295
// Ein-/Ausgänge Bezeichnen
constexpr int BTN_ON = 2; // Einschalter
constexpr int BTN_OFF = 3; // Ausschalter
constexpr int LED_G = 9; // grüne LED
constexpr int LED_Y = 8; // gelbe LED
constexpr int LED_R = 7; // rote LED
const int BTN_ON = 2; // Einschalter
const int BTN_OFF = 3; // Ausschalter
const int LED_G = 9; // grüne LED
const int LED_Y = 8; // gelbe LED
const int LED_R = 7; // rote LED
// hier wird der aktuelle und vorherige Zustand gespeichert
char state_current = STATE_OFF;
char state_previous = STATE_OFF;
byte state_current = NULL;
byte state_previous = NULL;
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
unsigned long stateBegan;
@ -47,7 +39,7 @@ class Debounce
{
public:
Debounce(int pin);
bool update();
boolean update();
int read();
private:
int _pin;
@ -80,17 +72,18 @@ void testLeds() {
}
// wechselt zu neuen Zustand
void changeStateTo(char state_new) {
void changeStateTo(byte state_new) {
state_previous = state_current;
state_current = state_new;
transition();
}
// behandelt die Zustandübergänge
bool transition() {
boolean transition() {
if (state_previous == STATE_OFF && state_current == STATE_ON) {
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
printState(state_current);
stateBegan = millis();
return true;
}
@ -101,16 +94,19 @@ bool transition() {
if (state_previous == STATE_ON && state_current == STATE_HALF) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_Y, HIGH);
printState(state_current);
return true;
}
if (state_previous == STATE_ON && state_current == STATE_OFF) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, HIGH);
printState(state_current);
return true;
}
if (state_previous == STATE_HALF && state_current == STATE_OFF) {
digitalWrite(LED_Y, LOW);
digitalWrite(LED_R, HIGH);
printState(state_current);
return true;
}
if (state_previous == STATE_HALF && state_current == STATE_ON) {
@ -123,28 +119,42 @@ bool transition() {
digitalWrite(LED_G, LOW);
digitalWrite(LED_Y, LOW);
digitalWrite(LED_R, HIGH);
printState(state_current);
return true;
}
return false;
}
// information über aktuellen Zustand auf die Serielle Verbindung schreiben
void sendState() {
if (state_current == STATE_ON || state_current == STATE_HALF) {
Serial.print("1");
} else {
Serial.print("0");
// schreibt den aktuellen Zustand auf die Serialeverbindung
void printState(byte state) {
switch (state) {
case STATE_OFF:
Serial.print("OFF\n");
break;
case STATE_HALF:
Serial.print("HALF\n");
break;
case STATE_ON:
Serial.print("ON\n");
}
}
void handleSerial() {
if (Serial.read() != -1) {
printState(state_current);
do {
delay(10);
} while (Serial.read() != -1);
}
}
unsigned long calcStateTime() {
// Variablen überlauf von millis erkennen
unsigned long current_uptime = millis();
// kein überlauf
if (current_uptime > stateBegan) {
return current_uptime - stateBegan;
if (millis() - stateBegan >= 0) {
return millis() - stateBegan;
} else {
return millis() + (MAX_LONG - stateBegan);
}
return current_uptime + (ULONG_MAX - stateBegan);
}
// wird nach dem Starten dauerhaft ausgeführt
@ -167,10 +177,8 @@ void loop() {
} else if (state_current == STATE_HALF && calcStateTime() >= TIME_OFF) {
changeStateTo(STATE_OFF);
}
// kommunizieren
sendState();
delay(10);
// auf Eingaben auf der Serialenverbindung reagieren
handleSerial();
}
// Debouncer Klasse
@ -182,7 +190,7 @@ Debounce::Debounce(int pin)
this->_state = LOW;
this->_delay = 50;
}
bool Debounce::update()
boolean Debounce::update()
{
if (millis() - this->_time >= this->_delay) {
int reading = digitalRead(this->_pin);