diff --git a/intro/blinkLED.rst b/intro/blinkLED.rst index d995372bb2f8b470fb61b88aa5ec9dc481905232..d78e01c5a05c4d56ad08d38bd9838cd60e3932de 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 0000000000000000000000000000000000000000..61cba5087a2e5d23c8a2cee7be292b78b490732d --- /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 0000000000000000000000000000000000000000..f79a2869d9f64e5b55e74a5205b55ff35e59f28a --- /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