forked from Howchoo/pi-power-button
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdietpi_button.sh
More file actions
92 lines (80 loc) · 2.79 KB
/
dietpi_button.sh
File metadata and controls
92 lines (80 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# dietpi version, install gpiod
# The GPIO chip (usually 0 on a Pi) and line number (BCM number), timeout in seconds
GPIO_CHIP="gpiochip0"
GPIO_LINE=3
TIMEOUT=4
# --- Your commands ---
PAUSE_CMD="echo PAUSE_CMD"
STOP_CMD="echo STOP_CMD"
# --- LED Control Functions ---
set_led() {
# Arguments: 1=LED (PWR/ACT), 2=TRIGGER (heartbeat, timer, none, default-on)
if [ -n "$2" ]; then
echo "$2" | sudo tee "/sys/class/leds/$1/trigger" > /dev/null
fi
}
# --- Initial State ---
echo "Starting GPIO button handler for pin ${GPIO_LINE}..."
set_led PWR heartbeat
set_led ACT timer
push_count=0
# Use 'process substitution' to open the output of gpiomon as a file on file descriptor 3
exec 3< <(gpiomon -p 50 --edge falling -c "${GPIO_CHIP}" "${GPIO_LINE}")
# Ensure the file descriptor is closed when the script exits
trap "exec 3<&-" EXIT
while true; do
# Wait for an event from file descriptor 3 (-u 3), with a timeout
if read -r -u 3 -t $TIMEOUT event; then
# If an event is read, increment the count
push_count=$((push_count + 1))
echo "Button push detected. Count is now: ${push_count}"
# Visual feedback on each press: Green LED blinks once
set_led ACT default-on
set_led PWR default-on
sleep 0.2
set_led ACT heartbeat
set_led PWR timer
# Reset if count exceeds 8
if (( push_count > 8 )); then
echo "Push count exceeded 8. Resetting."
push_count=0
set_led PWR heartbeat
set_led ACT timer
fi
else
# If read times out, it means the user stopped pressing the button.
# Now we check the final count and execute the action.
if (( push_count > 0 )); then
echo "Timeout reached. Final push count: ${push_count}. Executing action..."
case $push_count in
2)
echo "Two pushes: PAUSE_CMD."
set_led PWR timer
set_led ACT default-on
eval $PAUSE_CMD
;;
4)
echo "Four pushes: STOP_CMD."
set_led PWR default-on
set_led ACT heartbeat
eval $STOP_CMD
;;
6)
echo "Six pushes: Powering off!"
set_led PWR none
set_led ACT heartbeat
sudo poweroff
;;
*)
echo "No action for ${push_count} pushes."
set_led PWR heartbeat
set_led ACT timer
;;
esac
# Reset for the next sequence
echo "Resetting push_count to 0."
push_count=0
fi
fi
done