-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.py
48 lines (40 loc) · 1.14 KB
/
gpio.py
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
import os
def export(gpio):
# is GPIO already exported?
if os.path.isdir("/sys/class/gpio/gpio" + str(gpio)):
return
# no, export it
f = open("/sys/class/gpio/export", "w")
f.write(str(gpio))
f.close()
def unexport(gpio):
f = open("/sys/class/gpio/unexport", "w")
f.write(str(gpio))
f.close()
# set direction
# gpio is a string or integer (e.g. "21" or 21)
# direction is "in" or "out"
def set_dir(gpio, direction):
filename = "/sys/class/gpio/gpio%s/direction" % str(gpio)
try:
f = open(filename, "w")
except:
print "error opening %s (need to export first?)" % filename
return
f.write(direction)
f.close()
# set output value
# gpio is a string or integer (e.g. "21" or 21)
# value is a string or integer ("0", "1", 0, 1)
def set(gpio, value):
filename = "/sys/class/gpio/gpio%s/value" % str(gpio)
try:
f = open(filename, "w")
except:
print "error opening %s (need to export first?)" % filename
return
try:
f.write(str(value))
f.close()
except:
print "error setting %s (is it an output?)" % filename