-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgpio-parse
executable file
·47 lines (41 loc) · 984 Bytes
/
gpio-parse
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
#!/bin/bash
set -e
shopt -s extglob
fail() {
local line
for line in "$@"; do
echo "$line" >&2
done
exit 1
}
usage() {
fail "Usage: gpio-parse [OPT] [--] GPIOS..." \
"where OPT is one of:" \
" -k | --kernel kernel gpio number (default)" \
" -p | --path sysfs path" \
" -h | --human formatted identifier"
}
output=kernel
case "$1" in
--kernel | -k ) output=kernel ; shift ;;
--path | -p ) output=path ; shift ;;
--human | -h ) output=human ; shift ;;
esac
if [[ $1 == -- ]]; then
shift
fi
for arg in "$@"; do
arg="${arg##@(?(*/)gpio|GPIO)?([ _-])}"
if [[ "$arg" == [0-9][_.]@(?([0-2])[0-9]|3[01]) ]]; then
arg=$(( ${arg##*[_.]*(0)} + 32 * ${arg%%[_.]*} ))
elif [[ "$arg" == +([0-9]) ]]; then
arg=$(( ${arg##*(0)} + 0 ))
else
fail "Can't parse as gpio: '$arg'"
fi
case $output in
kernel ) echo $arg ;;
path ) echo "/sys/class/gpio/gpio$arg" ;;
human ) printf '%d.%02d\n' $(( arg / 32 )) $(( arg % 32 )) ;;
esac
done