-
Notifications
You must be signed in to change notification settings - Fork 19
/
gpio
executable file
·83 lines (69 loc) · 1.65 KB
/
gpio
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Script to perform various operations on GPIO pins using sysfs
# Run this as root in /sys/class/gpio
# Provided as-is for any purpose
# Usage:
# gpio OP [N1 [N2]]
# OP is one of:
# ls - list direction & value
# exp - export pin(s)
# unex - unexport pin(s)
# in|out - set direction
# 0|1 - set value
#
# N1 is starting GPIO #
# N2 is ending GPIO # (N1 and N2 are inclusive)
# if N2 is omitted then only N1 is used
# if N1 is omitted then all currently exported GPIOs are used
# if no arguments are supplied then OP is taken as ls
gpdir="/sys/class/gpio"
# Sleazy way to get this to work from anywhere
pushd $gpdir
if [ -$2 != - -a -$3 != - ] ; then
list=`seq $2 $3`
elif [ -$2 != - ] ; then
list=gpio$2
else
list=gpio*
fi
#echo Using list: $list
for file in $list ; do
if [[ $file == gpiochip* ]] ; then
continue
fi
if [[ $file != gpio* ]] ; then
file=gpio$file
fi
if [[ -$1 != -ex* ]] ; then
if [ ! -d $file ] ; then
echo "$file: not present"
continue
fi
fi
case "-$1" in
-|-ls)
echo "$file: `cat $file/direction` `cat $file/value`"
;;
-in|-out)
echo $1 > $file/direction
;;
-0|-1)
echo $1 > $file/value
;;
-ex*)
num=${file#gpio}
echo "Exporting $num"
echo $num > export
;;
-unex*)
num=${file#gpio}
echo "Unexporting $num"
echo $num > unexport
;;
*)
echo "Unrecognized operation $1"
exit
esac
done
popd