forked from koenkooi/validation-scripts
-
Notifications
You must be signed in to change notification settings - Fork 24
/
readgpio
59 lines (46 loc) · 1 KB
/
readgpio
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
48
49
50
51
52
53
54
55
56
57
58
#!/bin/sh
# From http://wh1t3s.com/2009/05/14/reading-beagleboard-gpio/
#
# Read a GPIO input
GPIO=$1
cleanup() { # Release the GPIO port
echo $GPIO > /sys/class/gpio/unexport
echo ""
echo ""
exit
}
# Open the GPIO port
#
echo "$GPIO" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio${GPIO}/direction
trap cleanup SIGINT # call cleanup on Ctrl-C
THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value`
LAST_VALUE=$THIS_VALUE
NEWLINE=0
# Read forever
while [ "1" = "1" ]; do
# next three lines detect state transition
if [ "$THIS_VALUE" != "$LAST_VALUE" ]; then
EV="|"
else
EV=""
fi
# "^" for high, '_' for low
if [ "1" = "$THIS_VALUE" ]; then
EV="${EV}^"
else
EV="${EV}_"
fi
echo -n $EV
# sleep for a while
sleep 0.05
# wrap line every 72 samples
LAST_VALUE=$THIS_VALUE
THIS_VALUE=`cat /sys/class/gpio/gpio${GPIO}/value`
NEWLINE=`expr $NEWLINE + 1`
if [ "$NEWLINE" = "72" ]; then
echo ""
NEWLINE=0
fi
done
cleanup # call the cleanup routine