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