Enhance blink.sh

This commit is contained in:
Takumi Sueda 2023-07-31 22:09:11 +09:00
parent b561fdaec9
commit ce8d780b33

View File

@ -1,21 +1,58 @@
#!/bin/sh
set -u
if [ $# -ne 3 ]; then
echo "Usage: blink.sh RANGE_FROM RANGE_TO SLEEP"
VERBOSE=0
PIN=""
SLEEP=1
GPIOS=""
while getopts "hvr:p:s:" OPT; do
case "$OPT" in
h)
echo "Usage: blink.sh [-hv] [-r PIN_RANGE_FROM-PIN_RANGE_TO] [-p PIN] [-s SLEEP_SEC]"
echo "Example: blink.sh -r 0-10 -p 12"
echo " (blink from GPIO 0 to 10 and 12)"
exit 0
;;
v)
VERBOSE=1
;;
r)
RE='^([0-9]+)-([0-9]+)$'
if echo $OPTARG | grep -qvE $RE; then
echo "Error: invalid range: $OPTARG"
exit 1
fi
FROM=$(echo $OPTARG | sed -E "s/$RE/\\1/")
TO=$(echo $OPTARG | sed -E "s/$RE/\\2/")
GPIOS="$GPIOS$(seq -s " " $FROM $TO) "
;;
p)
if echo $OPTARG | grep -qvE "^[0-9]+$"; then
echo "Error: invalid pin number: $OPTARG"
exit 1
fi
GPIOS="$GPIOS$OPTARG "
;;
s)
if echo $OPTARG | grep -qvE "^[0-9]+$"; then
echo "Error: invalid sleep duration: $OPTARG"
exit 1
fi
SLEEP=$OPTARG
;;
esac
done
if [ $VERBOSE -eq 1 ]; then
echo "Pins to iterate over: $GPIOS"
fi
if [ "$(id -u)" -ne "0" ]; then
echo "Error: please run as root"
exit 1
fi
FROM=$1
TO=$2
SLEEP=$3
GPIOS=$(seq $FROM $TO)
AVAILABLE_GPIOS=""
export_gpio() {
@ -72,3 +109,4 @@ while [ 1 ]; do
sleep $SLEEP
done