Added shell script
authorCamil Staps <info@camilstaps.nl>
Sat, 17 Jun 2017 14:39:22 +0000 (14:39 +0000)
committerCamil Staps <info@camilstaps.nl>
Sat, 17 Jun 2017 14:39:22 +0000 (14:39 +0000)
terminal.sh [new file with mode: 0755]

diff --git a/terminal.sh b/terminal.sh
new file mode 100755 (executable)
index 0000000..4b7efea
--- /dev/null
@@ -0,0 +1,83 @@
+#!/bin/bash
+PGM="$0"
+PORT="$1"
+CMD="$2"
+
+BAUD=115200
+
+chr () {
+       printf \\$(printf '%03o' $1)
+}
+
+led () {
+       LED="$1"
+       OPT="$2"
+
+       case "$LED" in
+               1|a|A) LED=0;;
+               2|b|B) LED=16;;
+               3|c|C) LED=32;;
+               4|d|D) LED=48;;
+               *)
+                       echo "Unknown LED '$LED'"
+                       echo "Usage: $PGM $PORT led [1aA2bB3cC4dD] {off|green|red|green+blink|red+blink}"
+                       exit 1
+       esac
+
+       case "$OPT" in
+               off) OPT=0;;
+               green) OPT=1;;
+               red) OPT=2;;
+               green+blink) OPT=5;;
+               red+blink) OPT=6;;
+               *)
+                       echo "Unknown option '$OPT'"
+                       echo "Usage: $PGM $PORT led [1aA2bB3cC4dD] {off|green|red|green+blink|red+blink}"
+                       exit 1
+       esac
+
+       OPT=$(($LED+$OPT))
+
+       (echo -ne '\x11'; chr $OPT) > "$PORT"
+}
+
+backlight () {
+       OPT="$1"
+
+       case "$OPT" in
+               off)
+                       echo -ne '\x12\x00' > "$PORT"
+                       ;;
+               on)
+                       echo -ne '\x12\x01' > "$PORT"
+                       ;;
+               flash)
+                       echo -ne '\x12\x02' > "$PORT"
+                       ;;
+               *)
+                       echo "Unknown option '$OPT'"
+                       echo "Usage: $PGM $PORT $CMD {on|off|flash}"
+                       exit 1
+       esac
+}
+
+write () {
+       echo -n "${@:1}" > "$PORT"
+}
+
+stty -F "$PORT" -crtscts -hupcl "$BAUD"
+
+case "$CMD" in
+       backlight|bl)
+               backlight "$3"
+               ;;
+       led)
+               led "$3" "$4"
+               ;;
+       write)
+               write "${@:3}"
+               ;;
+       *)
+               echo "Usage: $PGM PORT {backlight|bl|led|write}"
+               exit 1
+esac