|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# power_button.sh - Script to monitor power button (GPIO2) and shutdown if pressed. |
| 4 | +# Also controls LED to indicate shutdown (GPIO4) |
| 5 | +# |
| 6 | +# Copyright (C) 2020 PolarPiBerry |
| 7 | +# |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +echo "2" > /sys/class/gpio/export |
| 21 | +echo "in" > /sys/class/gpio/gpio2/direction |
| 22 | + |
| 23 | +echo "4" > /sys/class/gpio/export |
| 24 | +echo "out" > /sys/class/gpio/gpio4/direction |
| 25 | + |
| 26 | +echo "1" > /sys/class/gpio/gpio4/value; |
| 27 | +sleep 3; |
| 28 | +echo "0" > /sys/class/gpio/gpio4/value; |
| 29 | + |
| 30 | +sleep 20; #Wait at least 20 seconds before taking affect |
| 31 | + |
| 32 | +while true; do |
| 33 | + state=`cat /sys/class/gpio/gpio2/value`; |
| 34 | + if [ $state = "0" ]; then |
| 35 | + echo "1" > /sys/class/gpio/gpio4/value; |
| 36 | + sleep 3; |
| 37 | + state=`cat /sys/class/gpio/gpio2/value`; |
| 38 | + if [ $state = "0" ]; then |
| 39 | + echo "Shutting down now" |
| 40 | + sudo shutdown -h now |
| 41 | + fi |
| 42 | + fi |
| 43 | + sleep 1; |
| 44 | +done; |
| 45 | + |
0 commit comments