MobilePenBerry/Assets/x730-safeshutdown.sh
2021-12-27 12:39:28 +01:00

72 lines
1.8 KiB
Bash

#x730 Powering on /reboot /shutdown from hardware
#!/bin/bash
sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
echo '#!/bin/bash
SHUTDOWN=4
REBOOTPULSEMINIMUM=200
REBOOTPULSEMAXIMUM=600
echo "$SHUTDOWN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
BOOT=17
echo "$BOOT" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$BOOT/direction
echo "1" > /sys/class/gpio/gpio$BOOT/value
echo "X730 Shutting down..."
while [ 1 ]; do
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
if [ $shutdownSignal = 0 ]; then
/bin/sleep 0.2
else
pulseStart=$(date +%s%N | cut -b1-13)
while [ $shutdownSignal = 1 ]; do
/bin/sleep 0.02
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
echo "X730 Shutting down", SHUTDOWN, ", halting Rpi ..."
sudo poweroff
exit
fi
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
done
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then
echo "X730 Rebooting", SHUTDOWN, ", recycling Rpi ..."
sudo reboot
exit
fi
fi
done' > /etc/x730pwr.sh
sudo chmod +x /etc/x730pwr.sh
sudo sed -i '$ i /etc/x730pwr.sh &' /etc/rc.local
#X730 full shutdown through Software
#!/bin/bash
sudo sed -e '/button/ s/^#*/#/' -i /etc/rc.local
echo '#!/bin/bash
BUTTON=18
echo "$BUTTON" > /sys/class/gpio/export;
echo "out" > /sys/class/gpio/gpio$BUTTON/direction
echo "1" > /sys/class/gpio/gpio$BUTTON/value
SLEEP=${1:-4}
re='^[0-9\.]+$'
if ! [[ $SLEEP =~ $re ]] ; then
echo "error: sleep time not a number" >&2; exit 1
fi
echo "X730 Shutting down..."
/bin/sleep $SLEEP
#restore GPIO 18
echo "0" > /sys/class/gpio/gpio$BUTTON/value
' > /usr/local/bin/x730shutdown.sh
sudo chmod +x /usr/local/bin/x730shutdown.sh