aboutsummaryrefslogtreecommitdiff
path: root/recipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.sh
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.sh')
-rwxr-xr-xrecipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.sh24
1 files changed, 24 insertions, 0 deletions
diff --git a/recipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.sh b/recipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.sh
new file mode 100755
index 0000000..a71d366
--- /dev/null
+++ b/recipes-demo/de10-nano-gpio-apps/files/show_KEY0_pb_state.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+# locate the gpio controller for the KEY0 push button 'gpio@0x100005000'
+GPIO_DIRNAME="unknown"
+for NEXT in $(find /sys/class/gpio -name "gpiochip*" | sort)
+do
+ cat "${NEXT}/label" | grep "gpio@0x100005000" > /dev/null && {
+ GPIO_DIRNAME="${NEXT}"
+ break
+ }
+done
+
+[ "unknown" == "${GPIO_DIRNAME}" ] && {
+ echo "unable to locate gpio controller"
+ exit 1
+} || {
+ # the KEY0 gpio is the first gpio in this controller
+ GPIO_NUMBER="$(cat "${GPIO_DIRNAME}/base")"
+ echo "${GPIO_NUMBER}" > /sys/class/gpio/export
+ GPIO_VALUE="$(cat /sys/class/gpio/gpio${GPIO_NUMBER}/value)"
+ echo "KEY0 push button is at state '${GPIO_VALUE}'"
+ echo "${GPIO_NUMBER}" > /sys/class/gpio/unexport
+}
+