@@ -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=" $(( __f2 msec_scale * 10 )) "
48- __f2msec_exp=" $(( __f2 msec_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=" $(( __f2 nsec_scale * 10 )) "
50+ __f2nsec_exp=" $(( __f2 nsec_exp - 1 )) "
4951 done
5052
51- echo " $(( (__f 2 msec_whole * 1000 ) + ((__f 2 msec_fractional * 1000 ) / __f 2 msec_scale ) )) "
53+ echo " $(( (__f 2 nsec_whole * 1000000000 ) + ((__f 2 nsec_fractional * 1000000000 ) / __f 2 nsec_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
75108if { 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 - b 1 - 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 - b 1 - 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
0 commit comments