diff --git a/lib/pi-buttons/Makefile b/lib/pi-buttons/Makefile new file mode 100644 index 0000000..82e823c --- /dev/null +++ b/lib/pi-buttons/Makefile @@ -0,0 +1,9 @@ +LIBS=-lpthread -lrt + +buttons: buttons.o + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) + +.PHONY: clean + +clean: + rm -vf *.o buttons diff --git a/lib/pi-buttons/a.out b/lib/pi-buttons/a.out deleted file mode 100755 index b8e6cf4..0000000 Binary files a/lib/pi-buttons/a.out and /dev/null differ diff --git a/lib/pi-buttons/build.sh b/lib/pi-buttons/build.sh deleted file mode 100755 index cbe167a..0000000 --- a/lib/pi-buttons/build.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -# TODO convert to Makefile - -gcc buttons.c -lpthread -lrt diff --git a/lib/pi-buttons/setup.sh b/lib/pi-buttons/setup.sh index 14cf0a6..96ede12 100755 --- a/lib/pi-buttons/setup.sh +++ b/lib/pi-buttons/setup.sh @@ -1,15 +1,45 @@ #!/bin/sh -echo 17 > /sys/class/gpio/export -echo 22 > /sys/class/gpio/export -echo 27 > /sys/class/gpio/export +if [ ! -w /sys/class/gpio/export ]; then + echo "$0 must be run with GPIO permissions (typically gpio group membership or root/sudo)" + exit 1 +fi -sleep 3 +for gpionum in 17 22 27; do + if [ ! -d /sys/class/gpio/gpio${gpionum} ]; then + echo "exporting GPIO ${gpionum}" + echo ${gpionum} > /sys/class/gpio/export + else + echo "GPIO ${gpionum} already exported" + fi +done -echo "in" > /sys/class/gpio/gpio17/direction -echo "both" > /sys/class/gpio/gpio17/edge +for gpionum in 17 27; do + if [ "$(/bin/cat /sys/class/gpio/gpio${gpionum}/direction)" != "in" ]; then + echo "setting GPIO ${gpionum} direction to inwards" + echo in > /sys/class/gpio/gpio${gpionum}/direction + else + echo "GPIO ${gpionum} already direction inwards" + fi + if [ "$(/bin/cat /sys/class/gpio/gpio${gpionum}/edge)" != "both" ]; then + echo "setting GPIO ${gpionum} signal edge to both" + echo both > /sys/class/gpio/gpio${gpionum}/edge + else + echo "GPIO ${gpionum} signal edge already both" + fi +done -echo "in" > /sys/class/gpio/gpio27/direction -echo "both" > /sys/class/gpio/gpio27/edge -echo "out" > /sys/class/gpio/gpio22/direction +if [ "$(/bin/cat /sys/class/gpio/gpio22/direction)" != "out" ]; then + echo "setting GPIO 22 direction to outwards" + echo out > /sys/class/gpio/gpio22/direction +else + echo "GPIO 22 already direction outwards" +fi + +if [ ! -x $(dirname $0)/buttons ]; then + echo "building buttons executable" + /usr/bin/make -C $(dirname $0) +else + echo "buttons executable exists" +fi diff --git a/openaps-menu.sh b/openaps-menu.sh index ea3f3f0..553e1df 100755 --- a/openaps-menu.sh +++ b/openaps-menu.sh @@ -1,3 +1,12 @@ -#!/bin/bash +#!/bin/bash -e -(cd lib/pi-buttons/ && ./setup.sh && ./a.out) & node index.js +topdir=$(dirname $0) +buttonsdir=${topdir}/lib/pi-buttons + +# GPIO setup and buttons utility build +${buttonsdir}/setup.sh + +# needs to create a socket in cwd +(cd ${buttonsdir} && ./buttons &) + +exec node ${topdir}/index.js