diff --git a/push.d/skeleton b/push.d/skeleton new file mode 100644 index 0000000..86a9ac3 --- /dev/null +++ b/push.d/skeleton @@ -0,0 +1,13 @@ +#!/bin/sh + +case "$1" in + ON) + echo 'ON' + ;; + HALF) + echo 'HALF' + ;; + OFF) + echo 'OFF' + ;; +esac diff --git a/push.sh b/push.sh new file mode 100755 index 0000000..ac0128d --- /dev/null +++ b/push.sh @@ -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 diff --git a/status/status.ino b/status/status.ino index 0c902eb..40d4302 100644 --- a/status/status.ino +++ b/status/status.ino @@ -83,6 +83,7 @@ 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; } @@ -93,16 +94,19 @@ boolean 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) { @@ -115,17 +119,32 @@ boolean 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); } } @@ -158,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