Created
April 10, 2018 14:40
-
-
Save Frederick888/c9c1e2f13b0b53b6f324e10c530893a6 to your computer and use it in GitHub Desktop.
Read mode/value of a specific pin using WiringPi GPIO utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage() { | |
>&2 printf "Usage: $0 -n <phy|wpi> [-p <int>] [-f <mode|value>]\n" | |
>&2 printf "\t-n\tNumbering rule, physical or wPi\n" | |
>&2 printf "\t-p\tPin number\n" | |
>&2 printf "\t-f\tField to read\n" | |
exit 1 | |
} | |
while getopts ":p:n:f:" o; do | |
case "$o" in | |
n) | |
NUMBERING="${OPTARG,,}" | |
NUMBERING="${NUMBERING:0:1}" | |
[[ ("$NUMBERING" == "p") || ("$NUMBERING" == "w") ]] || usage | |
;; | |
p) | |
PIN="$OPTARG" | |
;; | |
f) | |
FIELD="${OPTARG,,}" | |
FIELD="${FIELD:0:1}" | |
[[ ("$FIELD" == "m") || ("$FIELD" == "v") ]] || usage | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[[ -z "$NUMBERING" ]] && usage | |
read_all_gpio() { | |
DATA=`/usr/bin/gpio readall | tail -n+4 | head -n-3 | tr -s '|' | tr -d ' ' | sed -r "s/^\|.*\|(.*)\|.*\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|.*\|(.*)\|.*\|$/\4\t\1\t\2\t\3\n\5\t\8\t\7\t\6/" | egrep -v $'^[0-9]+\t\t\t$'` | |
printf "$DATA" | while read line; do | |
IFS=$'\t' read -r -a TEMP <<< "$line" | |
[[ "$NUMBERING" == "p" ]] && printf "${TEMP[0]}" || printf "${TEMP[1]}" | |
printf "\t${TEMP[2]}\t${TEMP[3]}\n" | |
done | |
} | |
DATA=`read_all_gpio | sort -n` | |
if [[ -z "$PIN" ]]; then | |
printf "$DATA" | |
exit 0 | |
fi | |
PIN_DATA=`printf "$DATA" | egrep "^$PIN"$'\t'` | |
if [[ -z "$PIN_DATA" ]]; then | |
>&2 printf "No data for pin $PIN!\n" | |
exit 1 | |
fi | |
if [[ -z "$FIELD" ]]; then | |
printf "$PIN_DATA" | |
exit 0 | |
fi | |
IFS=$'\t' read -r -a PIN_DATA <<< "$PIN_DATA" | |
if [[ "$FIELD" == "m" ]]; then | |
printf "${PIN_DATA[1]}" | |
else | |
printf "${PIN_DATA[2]}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment