Skip to content
This repository was archived by the owner on Sep 27, 2018. It is now read-only.

Commit 4fb580e

Browse files
authored
Merge pull request #3 from LakeMaps/poweroff-gpio
Add service for poweroff and restart via GPIO
2 parents 37a3048 + cf78715 commit 4fb580e

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ addons:
1010
- shellcheck
1111

1212
script:
13-
- shellcheck script/buildscript
13+
- (
14+
for file in $(find script -type f -exec awk -f script/has-shebang.awk {} +);
15+
do
16+
( set -x; shellcheck "${file}" );
17+
done
18+
)

script/buildscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ update_linux() {
5757
execute_in_container systemctl enable ssh
5858

5959
cp -r "$dir"/files/* .
60+
execute_in_container systemctl enable gpio-reboot
61+
execute_in_container systemctl enable gpio-poweroff
6062

6163
printf '%s\n' "$hostname" > etc/hostname
6264
{
@@ -69,6 +71,7 @@ update_bootp() {
6971
truncate -s0 config.txt
7072
{
7173
printf '%s\n' 'dtparam=spi=on'
74+
printf '%s\n' 'dtoverlay=gpio-poweroff,gpiopin=4'
7275
} >> config.txt
7376
}
7477

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Poweroff via GPIO PIN #23
3+
4+
[Service]
5+
User=root
6+
TimeoutStartSec=0
7+
Restart=on-failure
8+
ExecStart=/usr/local/bin/gpio-command 23 poweroff
9+
10+
[Install]
11+
WantedBy=multi-user.target
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Reboot via GPIO PIN #24
3+
4+
[Service]
5+
User=root
6+
TimeoutStartSec=0
7+
Restart=on-failure
8+
ExecStart=/usr/local/bin/gpio-command 24 reboot
9+
10+
[Install]
11+
WantedBy=multi-user.target
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -u
5+
set -o pipefail
6+
7+
# Takes a BCM pin number and a command to run and executes the
8+
# command when the GPIO pin first sees a falling edge. This
9+
# script doesn't handle signals correctly...so that should be
10+
# fixed probably—blame RPi.GPIO#wait_for_edge for ignoring 'em.
11+
#
12+
# $1 - The BCM GPIO pin number.
13+
# $@ - Commands to be executed.
14+
#
15+
# Examples
16+
#
17+
# gpio-command 23 bash -c 'echo Works?'
18+
# gpio-command 24 reboot
19+
#
20+
# Returns the exit code of the last command executed
21+
22+
PIN="${1}"
23+
shift
24+
25+
python -c "
26+
import RPi.GPIO as GPIO
27+
GPIO.setmode(GPIO.BCM)
28+
GPIO.setup(${PIN}, GPIO.IN, pull_up_down=GPIO.PUD_UP)
29+
GPIO.wait_for_edge(${PIN}, GPIO.FALLING)
30+
GPIO.cleanup()
31+
"
32+
33+
printf '%s\n' "+ '$*'"
34+
"$@"

script/has-shebang.awk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FNR == 1 && /#!\/usr\/bin\/env bash/ {
2+
print FILENAME
3+
}

0 commit comments

Comments
 (0)