Skip to content

Commit 532ef9f

Browse files
committed
feat: ATX_RASPI_* env vars for configuration
1 parent 722a057 commit 532ef9f

2 files changed

Lines changed: 82 additions & 29 deletions

File tree

shutdowncheck.sh

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,78 @@ header() {
3232
echo "=========================================================================================="
3333
}
3434

35-
f2msec() {
35+
f2nsec() {
3636
case "${1?}" in
3737
*.*.*)
38+
echo 1>&2 "Not a valid decimal number: ${1}"
3839
return 1
3940
;;
4041
*.*)
41-
__f2msec_whole="${1%%.*}"
42-
__f2msec_fractional="${1#*.}"
43-
44-
__f2msec_exp="${#__f2msec_fractional}"
45-
__f2msec_scale=1
46-
while [ "$__f2msec_exp" -gt 0 ]; do
47-
__f2msec_scale="$(( __f2msec_scale * 10 ))"
48-
__f2msec_exp="$(( __f2msec_exp - 1 ))"
42+
__f2nsec_whole="${1%%.*}"
43+
__f2nsec_fractional="${1#*.}"
44+
45+
__f2nsec_exp="${#__f2nsec_fractional}"
46+
__f2nsec_scale=1
47+
48+
while [ "$__f2nsec_exp" -gt 0 ]; do
49+
__f2nsec_scale="$(( __f2nsec_scale * 10 ))"
50+
__f2nsec_exp="$(( __f2nsec_exp - 1 ))"
4951
done
5052

51-
echo "$(( (__f2msec_whole * 1000) + ((__f2msec_fractional * 1000) / __f2msec_scale) ))"
53+
echo "$(( (__f2nsec_whole * 1000000000) + ((__f2nsec_fractional * 1000000000) / __f2nsec_scale) ))"
5254
;;
5355
*)
54-
echo "$(( "$1" * 1000 ))"
56+
echo "$(( "$1" * 1000000000 ))"
5557
;;
5658
esac
5759
}
5860

61+
ATX_RASPI_PULSE_MIN="${ATX_RASPI_PULSE_MIN:-0.2}"
62+
ATX_RASPI_PULSE_MAX="${ATX_RASPI_PULSE_MAX:-0.6}"
63+
64+
REBOOTPULSEMINIMUM="$(f2nsec "$ATX_RASPI_PULSE_MIN")" #reboot pulse signal should be at least this long
65+
REBOOTPULSEMAXIMUM="$(f2nsec "$ATX_RASPI_PULSE_MAX")" #reboot pulse signal should be at most this long
66+
5967
#This is GPIO 7 (pin 26 on the pinout diagram).
6068
#This is an input from ATXRaspi to the Pi.
61-
#When button is held for ~3 seconds, this pin will become HIGH signalling to this script to poweroff the Pi.
62-
SHUTDOWN=7
63-
REBOOTPULSEMINIMUM=200 #reboot pulse signal should be at least this long
64-
REBOOTPULSEMAXIMUM=600 #reboot pulse signal should be at most this long
69+
#When button is held for ~3 seconds, this pin will become HIGH signaling to this script to poweroff the Pi.
70+
SHUTDOWN="${ATX_RASPI_SHUTDOWN_PIN:-7}"
6571

6672
#Added reboot feature (with ATXRaspi R2.6 (or ATXRaspi 2.5 with blue dot on chip)
6773
#Hold ATXRaspi button for at least 500ms but no more than 2000ms and a reboot HIGH pulse of 500ms length will be issued
6874
#This is GPIO 8 (pin 24 on the pinout diagram).
6975
#This is an output from Pi to ATXRaspi and signals that the Pi has booted.
7076
#This pin is asserted HIGH as soon as this script runs (by writing "1" to /sys/class/gpio/gpio8/value)
71-
BOOT=8
77+
BOOT="${ATX_RASPI_BOOT_PIN:-8}"
78+
79+
CHIP="${ATX_RASPI_CHIP:-/dev/gpiochip0}"
80+
81+
failed=''
82+
83+
if [ "$REBOOTPULSEMINIMUM" -le 0 ]; then
84+
failed="${failed:+"${failed}, "}ATX_RASPI_PULSE_MIN (${ATX_RASPI_PULSE_MIN}) must be greater than 0"
85+
fi
86+
87+
if [ "$REBOOTPULSEMAXIMUM" -le "$REBOOTPULSEMINIMUM" ]; then
88+
failed="${failed:+"${failed}, "}ATX_RASPI_PULSE_MAX (${ATX_RASPI_PULSE_MAX}) must be greater than ATX_RASPI_PULSE_MIN (${ATX_RASPI_PULSE_MIN})"
89+
fi
90+
91+
if [ "$SHUTDOWN" -lt 0 ]; then
92+
failed="${failed:+"${failed}, "}ATX_RASPI_SHUTDOWN_PIN (${SHUTDOWN}) must be greater than 0"
93+
fi
94+
95+
if [ "$BOOT" -lt 0 ]; then
96+
failed="${failed:+"${failed}, "}ATX_RASPI_BOOT_PIN (${BOOT}) must be greater than 0"
97+
fi
7298

73-
CHIP="${CHIP:-/dev/gpiochip0}"
99+
if [ "$SHUTDOWN" -eq "$BOOT" ]; then
100+
failed="${failed:+"${failed}, "}ATX_RASPI_SHUTDOWN_PIN (${SHUTDOWN}) must be distinct from ATX_RASPI_BOOT_PIN (${BOOT})"
101+
fi
102+
103+
if [ -n "${failed:-}" ]; then
104+
echo 1>&2 "ERROR: $failed"
105+
exit 1
106+
fi
74107

75108
if { command -v gpioset && command -v gpiomon ; } 1>/dev/null 2>&1; then
76109
init_shutdown_pin() {
@@ -87,18 +120,18 @@ if { command -v gpioset && command -v gpiomon ; } 1>/dev/null 2>&1; then
87120
case "$event" in
88121
# Rising
89122
1)
90-
pulseStart="$(f2msec "$seconds")"
123+
pulseStart="$(f2nsec "$seconds")"
91124
;;
92125
# Falling
93126
2)
94-
pulseEnd="$(f2msec "$seconds")"
127+
pulseEnd="$(f2nsec "$seconds")"
95128
pulseStart="${pulseStart:-"$pulseEnd"}"
96129
pulseDuration="$(( pulseEnd - pulseStart ))"
97130

98-
if [ "$pulseDuration" -gt $REBOOTPULSEMAXIMUM ]; then
131+
if [ "$pulseDuration" -gt "$REBOOTPULSEMAXIMUM" ]; then
99132
header 12 "SHUTDOWN request on chip ${CHIP?} from GPIO${SHUTDOWN}, halting Rpi ..."
100133
return
101-
elif [ "$pulseDuration" -gt $REBOOTPULSEMINIMUM ]; then
134+
elif [ "$pulseDuration" -gt "$REBOOTPULSEMINIMUM" ]; then
102135
header 12 "REBOOT request on chip ${CHIP?} from GPIO${SHUTDOWN?}, recycling Rpi ..."
103136
reboot
104137
return
@@ -133,18 +166,18 @@ elif [ -e /sys/class/gpio/export ]; then
133166
if [ "$shutdownSignal" = 0 ]; then
134167
sleep 0.2
135168
else
136-
pulseStart=$(date +%s%N | cut -b1-13) # mark the time when Shutoff signal went HIGH (milliseconds since epoch)
169+
pulseStart="$(date +%s%N)" # mark the time when Shutoff signal went HIGH (milliseconds since epoch)
137170
while [ "$shutdownSignal" = 1 ]; do
138171
sleep 0.02
139-
if [ $(($(date +%s%N | cut -b1-13)-pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
172+
if [ "$(( "$(date +%s%N)" - pulseStart ))" -gt "$REBOOTPULSEMAXIMUM" ]; then
140173
header 12 "SHUTDOWN request from GPIO${SHUTDOWN}, halting Rpi ..."
141174
poweroff
142175
return
143176
fi
144177
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
145178
done
146179
#pulse went LOW, check if it was long enough, and trigger reboot
147-
if [ $(($(date +%s%N | cut -b1-13)-pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then
180+
if [ "$(( "$(date +%s%N)" - pulseStart ))" -gt "$REBOOTPULSEMINIMUM" ]; then
148181
header 12 "REBOOT request from GPIO${SHUTDOWN}, recycling Rpi ..."
149182
reboot
150183
return

shutdownirq.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,36 @@
88
import time
99

1010
# Reboot pulse signal should be at least this long (seconds).
11-
REBOOTPULSEMINIMUM = 0.2
11+
REBOOTPULSEMINIMUM = float(os.environ.get("ATX_RASPI_PULSE_MIN", 0.2))
1212

1313
# Reboot pulse signal should be at most this long (seconds).
14-
REBOOTPULSEMAXIMUM = 1.0
14+
REBOOTPULSEMAXIMUM = float(os.environ.get("ATX_RASPI_PULSE_MAX", 1.0))
1515

1616
# GPIO pin used for shutdown signal.
17-
SHUTDOWN = 7
17+
SHUTDOWN = int(os.environ.get("ATX_RASPI_SHUTDOWN_PIN", 7))
1818

1919
# GPIO pin used for boot signal.
20-
BOOT = 8
20+
BOOT = int(os.environ.get("ATX_RASPI_BOOT_PIN", 8))
21+
22+
failed = [msg for (cond, msg) in [
23+
(REBOOTPULSEMINIMUM > 0, "ATX_RASPI_PULSE_MIN ({0}) must be greater than 0".format(REBOOTPULSEMINIMUM)),
24+
(REBOOTPULSEMAXIMUM > REBOOTPULSEMINIMUM, "ATX_RASPI_PULSE_MAX ({0} must be greater than ATX_RASPI_PULSE_MIN ({1})".format(REBOOTPULSEMAXIMUM, REBOOTPULSEMINIMUM)),
25+
(SHUTDOWN >= 0, "ATX_RASPI_SHUTDOWN_PIN ({0}) must be greater than 0".format(SHUTDOWN)),
26+
(BOOT >= 0, "ATX_RASPI_BOOT_PIN ({0}) must be greater than 0".format(BOOT)),
27+
(SHUTDOWN != 0, "ATX_RASPI_SHUTDOWN_PIN ({0}) must be distinct from ATX_RASPI_BOOT_PIN ({1})".format(SHUTDOWN, BOOT)),
28+
] if not cond]
29+
30+
if len(failed) > 0:
31+
raise ValueError(", ".join(failed))
32+
33+
if os.environ.get("ATX_RASPI_DRY_RUN", "") == "":
34+
def handle_press(*command):
35+
os.system(*command)
36+
sys.exit()
37+
else:
38+
def handle_press(*command):
39+
print("[dry-run] ", " ".join(command))
40+
sys.exit()
2141

2242
def diag(*msgs):
2343
linelen = max([len(msg) for msg in msgs]) + 2
@@ -43,7 +63,7 @@ def announce():
4363

4464
if have_gpiod:
4565

46-
CHIP = "/dev/gpiochip0"
66+
CHIP = os.environ.get("ATX_RASPI_CHIP", "/dev/gpiochip0")
4767
CONSUMER = "atx-raspi"
4868
CONFIG = {
4969
SHUTDOWN: gpiod.LineSettings(direction=Direction.INPUT, edge_detection=Edge.BOTH),

0 commit comments

Comments
 (0)