-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsailtrack-x708_pwr
executable file
·40 lines (36 loc) · 1.46 KB
/
sailtrack-x708_pwr
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
#!/bin/bash
# `sailtrack-x708_pwr` - Shell script that performs a software shutdown or reboot depending on an impulse coming from
# the Geekworm X708 power HAT. This is needed to perform a full shutdown or reboot using a physical power button (the
# HAT sends the signal to this script, which performs the software shutdown/reboot, and then the HAT cuts the power).
shutdown_pin=5
boot_pin=12
reboot_pulse_min_ms=200
reboot_pulse_max_ms=600
# Set pin directions (input/output) and default values
echo $shutdown_pin > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$shutdown_pin/direction
echo $boot_pin > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$boot_pin/direction
echo "1" > /sys/class/gpio/gpio$boot_pin/value
while : ; do
signal=$(</sys/class/gpio/gpio$shutdown_pin/value)
# While the button is not pressed, do nothing
if [ "$signal" = 0 ]; then /bin/sleep 0.2
else
start=$(date +%s%N | cut -b1-13)
while [ "$signal" = 1 ]; do
/bin/sleep 0.02
# If the button is pressed for more than `$reboot_pulse_max_ms`, power off
if [ $(($(date +%s%N | cut -b1-13)-"$start")) -gt $reboot_pulse_max_ms ]; then
sudo /usr/sbin/poweroff
exit
fi
signal=$(</sys/class/gpio/gpio$shutdown_pin/value)
done
# If the button is pressed for more than `$reboot_pulse_min_ms`, reboot
if [ $(($(date +%s%N | cut -b1-13)-"$start")) -gt $reboot_pulse_min_ms ]; then
sudo /usr/sbin/reboot
exit
fi
fi
done