-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshutdownbutton
executable file
·139 lines (124 loc) · 2.8 KB
/
shutdownbutton
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# required package:
# apt-get install raspi-gpio
##### edit area start
scriptver="V1.5"
gpionum=24
cnt4shutdown=3
logf="/var/log/shutdownbutton.log"
# cnt*1000ms 3sec -> 3
##### edit area end
#
function DO_shutdown {
echo "Shutting Down..."
sudo shutdown -h now
/bin/sleep 30
}
function Switch_GPIO {
# signal ATtiny to shutoff within next 20secs and do a shutdown
if [ "$gpionum" != "" ]; then
echo "Signal PWRcontroller to power off..."
/usr/bin/raspi-gpio set $gpionum op pn dl
/bin/sleep 4
/usr/bin/raspi-gpio set $gpionum op pn dh
/usr/bin/raspi-gpio set $gpionum ip pu
else
echo "no GPIO for PowerOff signaling defined..."
fi
}
function Poll_GPIO {
# poll GPIO for minimum 3 sec low signal and do shutdown
echo "Poll_GPIO START"
/usr/bin/raspi-gpio set $gpionum ip pu
counter=0
while true; do
gpiolvl=$(/usr/bin/raspi-gpio get $gpionum | awk '{print $3;}')
# echo "gpiolvl: $gpiolvl"
if [ "$gpiolvl" == "level=0" ] ; then
echo "Button pressed ($gpiolvl)..."
((counter = counter + 1))
if [ "$counter" -ge "$cnt4shutdown" ] ; then
# /bin/sleep 3
DO_shutdown
counter=0
fi
else
counter=0
fi
/bin/sleep 1
done
}
function Create_service_file1 {
fil="/lib/systemd/system/shutdownbutton.service"
cat <<EOF > "$fil"
[Unit]
Description=ShutdownButton Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/local/sbin/shutdownbutton pollgpio
ExecStop=/usr/local/sbin/shutdownbutton settletime
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=ShutdownButton
[Install]
WantedBy=multi-user.target
Alias=shutdownbutton.service
EOF
chmod 644 "$fil"
chown root:root "$fil"
}
function Create_service_file2 {
fil="/lib/systemd/system/shutdownbuttonswitch.service"
cat <<EOF > "$fil"
[Unit]
Description=ShutdownButtonSwitch Service
DefaultDependencies=no
Before=poweroff.target halt.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/shutdownbutton switchoff
RemainAfterExit=yes
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=ShutdownButtonSwitch
[Install]
WantedBy=poweroff.target halt.target
Alias=shutdownbuttonswitch.service
EOF
chmod 644 "$fil"
chown root:root "$fil"
}
function DO_install {
# apt -qy update
apt install -qy raspi-gpio
Create_service_file1
Create_service_file2
systemctl daemon-reload
systemctl enable shutdownbutton.service
systemctl enable shutdownbuttonswitch.service
}
# echo "start $0 $1 $2 $2"
case "$1" in
install)
DO_install
;;
switchoff)
Switch_GPIO
# DO_shutdown
;;
pollgpio)
Poll_GPIO
;;
settletime)
echo "settletime"
/bin/sleep 4
;;
version)
echo "$scriptver"
;;
*)
echo "$(basename $0) unknown option $1 $2 $3 $4"
echo "Usage: $(basename $0) switchoff | install | version"
exit 1
esac