From 7ce15aca15c1f14f7ed084eac3cde37ffa89d183 Mon Sep 17 00:00:00 2001
From: "Mark A. Yoder" <Mark.A.Yoder@Rose-Hulman.edu>
Date: Fri, 25 Aug 2023 16:35:25 -0400
Subject: [PATCH] Added evtest examples

---
 intro/blinkLED.rst   | 65 ++++++++++++++++++++++++++++++++++++++++++++
 intro/buttonEvent.sh | 24 ++++++++++++++++
 intro/buttonLED.sh   | 21 ++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100755 intro/buttonEvent.sh
 create mode 100755 intro/buttonLED.sh

diff --git a/intro/blinkLED.rst b/intro/blinkLED.rst
index d995372b..d78e01c5 100644
--- a/intro/blinkLED.rst
+++ b/intro/blinkLED.rst
@@ -306,4 +306,69 @@ Try it for the other LEDs.
     This may not work on all Beagles since it depends on which 
     version of Debian you are running.
 
+Blinking in response  to a button
+---------------------------------
 
+Some Beagles have a USR button that can be used  to control the LEDs. 
+You can test the USR button with ``evtest`` 
+
+.. code-block:: shell-session
+
+    bone:~$ evtest
+    No device specified, trying to scan all of /dev/input/event*
+    Not running as root, no devices may be available.
+    Available devices:
+    /dev/input/event0:	tps65219-pwrbutton
+    /dev/input/event1:	gpio-keys
+    Select the device event number [0-1]: 1
+
+We want to use ``gpio-keys``, so enter ``1``. Press and release 
+the USR button and you'll see:
+
+ .. code-block:: shell-session
+
+    Input driver version is 1.0.1
+    Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
+    Input device name: "gpio-keys"
+    Supported events:
+    Event type 0 (EV_SYN)
+    Event type 1 (EV_KEY)
+        Event code 256 (BTN_0)
+    Key repeat handling:
+    Repeat type 20 (EV_REP)
+        Repeat code 0 (REP_DELAY)
+        Value    250
+        Repeat code 1 (REP_PERIOD)
+        Value     33
+    Properties:
+    Testing ... (interrupt to exit)
+    Event: time 1692994988.305846, type 1 (EV_KEY), code 256 (BTN_0), value 1
+    Event: time 1692994988.305846, -------------- SYN_REPORT ------------
+    Event: time 1692994988.561786, type 1 (EV_KEY), code 256 (BTN_0), value 2
+    Event: time 1692994988.561786, -------------- SYN_REPORT ------------
+    Event: time 1692994988.601883, type 1 (EV_KEY), code 256 (BTN_0), value 2
+    Event: time 1692994988.601883, -------------- SYN_REPORT ------------
+    Event: time 1692994988.641754, type 1 (EV_KEY), code 256 (BTN_0), value 2
+    Event: time 1692994988.641754, -------------- SYN_REPORT ------------
+    Event: time 1692994988.641754, type 1 (EV_KEY), code 256 (BTN_0), value 0
+    Event: time 1692994988.641754, -------------- SYN_REPORT ------------
+    Ctrl+c 
+
+The following script uses evtesst to wait for the USR button to be pressed and 
+then turns on the LED.
+
+.. literalinclude:: buttonEvent.sh
+    :caption: buttonEvent.sh
+    :linenos:
+
+:download:`buttonEvent.sh<buttonEvent.sh>`
+
+Try running it and pressing the USR button. 
+
+The next script polls the USR button and toggles the LED.
+
+.. literalinclude:: buttonLED.sh
+    :caption: buttonLED.sh
+    :linenos:
+
+:download:`buttonLED.sh<buttonLED.sh>`
diff --git a/intro/buttonEvent.sh b/intro/buttonEvent.sh
new file mode 100755
index 00000000..61cba508
--- /dev/null
+++ b/intro/buttonEvent.sh
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+#//////////////////////////////////////
+#	buttonEvent.sh
+#	Blinks a USR LED when USR button is pressed
+#       Waits for a button event
+#	Wiring:	
+#	Setup:	
+#	See:	https://unix.stackexchange.com/questions/428399/how-can-i-run-a-shell-script-on-input-device-event
+#//////////////////////////////////////
+
+device='/dev/input/by-path/platform-gpio-keys-event'
+
+LED="3"
+LEDPATH='/sys/class/leds/beaglebone:green:usr'
+
+key_off='*value 0*'
+ key_on='*value 1*'
+
+evtest --grab "$device" | while read line; do
+    case $line in
+        ($key_off) echo 0 > ${LEDPATH}${LED}/brightness ;;
+        ($key_on)  echo 1 > ${LEDPATH}${LED}/brightness ;;
+    esac
+done
diff --git a/intro/buttonLED.sh b/intro/buttonLED.sh
new file mode 100755
index 00000000..f79a2869
--- /dev/null
+++ b/intro/buttonLED.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+#//////////////////////////////////////
+#	buttonLED.sh
+#	Blinks a USR LED when USR button is pressed
+#       Polls the button
+#	Wiring:	
+#	Setup:	
+#	See:	
+#//////////////////////////////////////
+
+LED="3"
+ 
+BUTTONPATH='/dev/input/by-path/platform-gpio-keys-event'
+LEDPATH='/sys/class/leds/beaglebone:green:usr'
+ 
+while true ; do
+    # evtest returns 0 if not pressed and a non-0 value if pressed.
+    evtest --query $BUTTONPATH EV_KEY BTN_0
+    echo $? > ${LEDPATH}${LED}/brightness
+    sleep 0.1
+done
-- 
GitLab