comparison resources/shutdown_switch.sh @ 274:cf98c06c72fd prerelease

merge
author Liam Donovan <l.b.donovan@qmul.ac.uk>
date Tue, 17 May 2016 16:42:02 +0100
parents 8d80eda512cd
children
comparison
equal deleted inserted replaced
273:0ee6eebb567a 274:cf98c06c72fd
1 #!/bin/bash
2 #
3 # shutdown_switch.sh: script for gracefully halting the BeagleBone Black
4 # when an onboard button is pressed.
5 #
6 # (c) 2016 Andrew McPherson, C4DM, QMUL
7 # Developed for Bela: http://bela.io
8
9
10 # Prepare P9.27 as input (will be configured with pullup)
11 # via BB-BONE-PRU-BELA overlay
12
13 BUTTON_PIN=115
14
15 echo $BUTTON_PIN > /sys/class/gpio/export
16 echo in > /sys/class/gpio/gpio"$BUTTON_PIN"/direction
17
18 if [ ! -r /sys/class/gpio/gpio"$BUTTON_PIN"/value ]
19 then
20 echo "$(basename $0): Unable to read GPIO pin $BUTTON_PIN for shutdown button."
21 exit
22 fi
23
24 # First, wait for pin to go high. If it starts at 0, that's more
25 # likely that the GPIO is not set correctly, so do not treat this
26 # as a button press
27
28 while [ $(cat /sys/class/gpio/gpio"$BUTTON_PIN"/value) -ne 1 ]; do
29 # Keep checking pin is readable in case it gets unexported
30 if [ ! -r /sys/class/gpio/gpio"$BUTTON_PIN"/value ]
31 then
32 echo "$(basename $0): Unable to read GPIO pin $BUTTON_PIN for shutdown button."
33 exit
34 fi
35 sleep 0.5
36 done
37
38 # Now for button press. Make sure the button is held at least
39 # 1 second before shutting down
40
41 PRESS_COUNT=0
42
43 while true; do
44 # Keep checking pin is readable in case it gets unexported
45 if [ ! -r /sys/class/gpio/gpio"$BUTTON_PIN"/value ]
46 then
47 echo "$(basename $0): Unable to read GPIO pin $BUTTON_PIN for shutdown button."
48 exit
49 fi
50
51 # Button pressed? (pressed = low)
52 if [ $(cat /sys/class/gpio/gpio"$BUTTON_PIN"/value) -eq 0 ]
53 then
54 PRESS_COUNT=$((PRESS_COUNT+1))
55 if [ "$PRESS_COUNT" -ge 4 ]
56 then
57 echo "$(basename $0): Shutdown button pressed. Halting system..."
58 halt
59 exit 1
60 fi
61 else
62 PRESS_COUNT=0
63 fi
64 sleep 0.5
65 done