-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.sh
382 lines (364 loc) · 11.4 KB
/
gpio.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/bin/bash
####################################################################################################################
# Raspberry Pi GPIO interface script by Thomas Galea. #
# #
# Feel free to use this script however you want. Take it, use it, modify it, improve it, learn from it. #
# I simply ask that you keep my name in here if you plan to further share it! #
# #
# If you find any problems with this script, please let me know! I'll try to solve any issues as soon as possible. #
####################################################################################################################
# If you want to disable the compatibility check, change the below variable to 0:
sysCheck=1
# Shorthand GPIO path. Change if your system has the GPIO files in another location.
gpio="/sys/class/gpio"
# This string gives me nightmares. Colourisation is fun.
helpCmdList=" \e[1;37m$0\e[0m [\e[1;32mlist\e[0m/\e[1;32mexport\e[0m/\e[1;32munexport\e[0m/\e[1;32medge\e[0m/\e[1;32minv\e[0m/\e[1;32mdir\e[0m/\e[1;32mset\e[0m/\e[1;32mget\e[0m] [\e[1;33mGPIO#\e[0m] [\e[1;36mvalue\e[0m]"
# Show basic help if parameter 1 is blank.
if [ "$1" = "" ];then
echo "Raspberry Pi GPIO interface script by Thomas Galea."
echo "Usage:"
echo -e "$helpCmdList"
echo
echo "Use '$0 help' for more usage information."
exit 0
fi
# Ensure parameter 1 matches one of the valid commands.
invalid=1
case $1 in
help) invalid=0;;
list) invalid=0;;
export) invalid=0;;
unexport) invalid=0;;
edge) invalid=0;;
inv) invalid=0;;
dir) invalid=0;;
set) invalid=0;;
get) invalid=0;;
esac
if [ "$invalid" = "1" ];then
echo "Invalid command."
echo "Use '$0 help' for more usage information."
exit 2
fi
# Help
if [ "$1" = "help" ];then
function displayHelp {
echo "Raspberry Pi GPIO interface script by Thomas Galea."
echo "Usage:"
echo -e "$helpCmdList"
echo
echo -e " \e[1;32mlist\e[0m Lists the currently enabled GPIO pins."
echo -e " \e[1;37m$0\e[0m \e[1;32mlist\e[0m"
echo
echo -e " \e[1;32mexport\e[0m Enables a GPIO pin for use."
echo -e " \e[1;37m$0\e[0m \e[1;32mexport \e[1;33m21\e[0m"
echo
echo -e " \e[1;32munexport\e[0m Disables a GPIO pin, preventing use."
echo -e " \e[1;37m$0\e[0m \e[1;32munexport \e[1;33m21\e[0m"
echo
echo -e " \e[1;32medge\e[0m Sets a GPIO pin's edge. Valid options are 'none', 'rising', 'falling', or 'both'."
echo -e " \e[1;37m$0\e[0m \e[1;32medge \e[1;33m21 \e[1;36mnone\e[0m"
echo -e " \e[1;37m$0\e[0m \e[1;32medge \e[1;33m21 \e[1;36mrising\e[0m"
echo -e " \e[1;37m$0\e[0m \e[1;32medge \e[1;33m21 \e[1;36mfalling\e[0m"
echo -e " \e[1;37m$0\e[0m \e[1;32medge \e[1;33m21 \e[1;36mboth\e[0m"
echo
echo -e " \e[1;32minv\e[0m Sets a GPIO pin's 'active_low' value. If this is 1, the pin's value becomes inverted."
echo -e " \e[1;37m$0\e[0m \e[1;32minv \e[1;33m21 \e[1;36m0\e[0m"
echo -e " \e[1;37m$0\e[0m \e[1;32minv \e[1;33m21 \e[1;36m1\e[0m"
echo
echo -e " \e[1;32mdir\e[0m Sets a GPIO pin's direction as either input or output."
echo -e " \e[1;37m$0\e[0m \e[1;32mdir \e[1;33m21 \e[1;36min\e[0m"
echo -e " \e[1;37m$0\e[0m \e[1;32mdir \e[1;33m21 \e[1;36mout\e[0m"
echo
echo -e " \e[1;32mset\e[0m Sets the value of a GPIO pin either high (1) or low (0)."
echo -e " \e[1;37m$0\e[0m \e[1;32mset \e[1;33m21 \e[1;36m0\e[0m"
echo -e " \e[1;37m$0\e[0m \e[1;32mset \e[1;33m21 \e[1;36m1\e[0m"
echo
echo -e " \e[1;32mget\e[0m Displays the current value of a GPIO pin."
echo -e " \e[1;37m$0\e[0m \e[1;32mget \e[1;33m21\e[0m"
echo
}
displayHelp | more
exit 0
fi
# Beyond here are functional commands, so now we should check that this script will actually work on this system.
#
# This check can be disabled by setting the above variable (sysCheck) to 0.
if [ "$sysCheck" -gt "0" ];then
if ! ls $gpio >/dev/null 2>&1;then
echo "This script is incompatible with your system."
echo -e "'\e[1;37m"$gpio"\e[0m' could not be found)."
echo
echo "If you know this is false, please let me know."
echo "If you know that the GPIO can be accessed elsewhere from within Bash, you can modify the script"
echo -e "and replace the line '\e[1;37mgpio=\""$gpio"\"\e[0m' with the correct path."
echo
echo "If you'd rather disable this check altogether, you can edit this script,"
echo -e "and change the line '\e[1;37msysCheck=1\e[0m' to '\e[1;37msysCheck=0\e[0m'."
exit 1
fi
fi
# List
if [ "$1" = "list" ];then
function listPins {
for pin in $gpio/gpio*;do
# Ensure this file isn't a GPIO chip.
if [ "$(echo $pin | grep chip)" = "" ];then
if [ "$(cat $pin/active_low)" = "0" ];then
invert="No"
else
invert="Yes"
fi
echo " Pin: ${pin:20:2}"
echo " Direction: $(cat $pin/direction)"
echo " Edge: $(cat $pin/edge)"
echo " Inverted: $invert"
echo " Value: $(cat $pin/value)"
echo ""
fi
done
}
listPins | more
exit 0
fi
# Export
if [ "$1" = "export" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "Export what GPIO pin?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Check that this pin isn't already exported.
if [ -d "$gpio/gpio$2" ];then
echo "That GPIO pin is already exported."
echo "If you want to reset it, please unexport it first, then you can export it again."
exit 1
else
# Export pin.
echo "$2" > $gpio/export;err=$?
# Check if success. If not, report.
if [ "$err" = "0" ];then
echo "Success."
exit 0
else
echo "Failed to export pin. Ensure you've entered the command correctly, and that you have the required permissions."
exit $err
fi
fi
fi
fi
# Unexport
if [ "$1" = "unexport" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "Unexport what GPIO pin?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure that this pin is currently exported.
if [ ! -d "$gpio/gpio$2" ];then
echo "That GPIO pin isn't currently exported."
exit 1
else
# Unexport pin.
echo "$2" > $gpio/unexport;err=$?
# Check if success. If not, report.
if [ "$err" = "0" ];then
echo "Success."
exit 0
else
echo "Failed to unexport pin. Ensure you've entered the command correctly, and that you have the required permissions."
exit $err
fi
fi
fi
fi
# Edge
if [ "$1" = "edge" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "What GPIO pin do you want to change?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure parameter 3 is present.
if [ "$3" = "" ];then
echo "What egde should this pin be?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure that GPIO pin is exported.
if [ ! -d "$gpio/gpio$2" ];then
echo "That pin is not exported. Please export it first."
exit 1
else
# Check if parameter 3 is a valid edge input.
validEdge=0
case $3 in
none) validEdge=1;;
rising) validEdge=1;;
falling) validEdge=1;;
both) validEdge=1;;
esac
if [ "$validEdge" = "0" ];then
echo "'$3' is not a valid egde."
echo "(Use '$0 help' for full command usage help)."
exit 1
else
# Set pin egde.
echo "$3" >$gpio/gpio$2/edge;err=$?
# Check if success. Report if not.
if [ "$err" = "0" ];then
echo "Success."
exit 0
else
echo "Failed to set pin edge. Please ensure you have the required permissions."
exit $err
fi
fi
fi
fi
fi
fi
# Invert
if [ "$1" = "inv" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "What GPIO pin do you want to change?"
echo "(use '$0 help' for full command usage help)."
exit 2
else
# Ensure parameter 3 is present.
if [ "$3" = "" ];then
echo "Should this pin be inverted or not?"
echo "(use '$0 help' for full command usage help)."
exit 2
else
# Ensure that GPIO pin is exported.
if [ ! -d "$gpio/gpio$2" ];then
echo "That pin is not exported. Please export it first."
exit 1
else
# Set pin active_low value.
echo "$3" >$gpio/gpio$2/active_low;err=$?
# Check if success. If not, report.
if [ "$err" = "0" ];then
echo "Success."
exit 0
else
echo "Failed to modify pin. Please ensure you have the required permissions."
exit $err
fi
fi
fi
fi
fi
# Direction
if [ "$1" = "dir" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "What GPIO pin do you want to change?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure parameter 3 is present.
if [ "$3" = "" ];then
echo "What direction should this pin be?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure that GPIO pin is exported.
if [ ! -d "$gpio/gpio$2" ];then
echo "That pin is not exported. Please export it first."
exit 1
else
# Ensure parameter 3 is a valid direction.
validDir=0
case $3 in
in) validDir=1;;
out) validDir=1;;
esac
if [ "$validDir" = "0" ];then
echo "'$3' is not a valid direction."
echo "(Use '$0 help' for full command usage help)."
exit 1
else
# Set pin direction.
echo "$3" > $gpio/gpio$2/direction;err=$?
# Check if success. If not, report.
if [ "$err" = "0" ];then
echo "Success."
exit 0
else
echo "Failed to set pin direction. Please ensure you have the required permissions."
exit $err
fi
fi
fi
fi
fi
fi
# Set Value
if [ "$1" = "set" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "Set what pin?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure parameter 3 is present.
if [ "$3" = "" ];then
echo "Set to what value?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure that GPIO pin is exported.
if [ ! -d "$gpio/gpio$2" ];then
echo "That pin is not exported. Please export it first."
exit 1
else
# Ensure that GPIO pin is set as OUTPUT.
if [ "$(cat $gpio/gpio$2/direction)" != "out" ];then
echo "That pin is not set to be an output. You cannot modify the value of input pins from here."
echo "If you absolutely must modify the value from here, use the 'inv' command instead."
echo "(Use '$0 help' for full command usage help)."
exit 1
else
# Set pin value.
echo "$3" > /sys/class/gpio/gpio$2/value;err=$?
# Check if success. If not, report.
if [ "$err" = "0" ];then
echo "Success."
exit 0
else
echo "Failed to set pin value. Please ensure you have the required permissions."
exit $err
fi
fi
fi
fi
fi
fi
# Get Value
if [ "$1" = "get" ];then
# Ensure parameter 2 is present.
if [ "$2" = "" ];then
echo "Get what pin?"
echo "(Use '$0 help' for full command usage help)."
exit 2
else
# Ensure that GPIO pin is exported.
if [ ! -d "$gpio/gpio$2" ];then
echo "That pin is not exported."
exit 0
else
# Get pin value.
cat /sys/class/gpio/gpio$2/value;err=$?
exit $err
fi
fi
fi
# The script should exit before this point.
echo "Script reached end of file. This shouldn't be possible, so please inform me!"
exit 255