-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOmegaGPIOHelper.py
106 lines (83 loc) · 2.85 KB
/
OmegaGPIOHelper.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
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
import subprocess
import platform
import os
"""
From user Dan L
https://community.onion.io/topic/40/simple-python-wrapper-and-demo
Enhanced to handle:
* pin 8: for some reason pin 8 did not respond without using fast-gpio
* added platform support
"""
class OmegaGPIOHelper(object):
exportPath = "/sys/class/gpio/gpiochip0/subsystem/export"
pinDirectionPath = "/sys/class/gpio/gpio$/direction"
pinValuePath = "/sys/class/gpio/gpio$/value"
pins = [0, 1, 6, 7, 8, 12, 13, 14, 23, 26, 21, 20, 19, 18]
def _set_output(self, pin_number):
if not pin_number is None:
subprocess.call(['fast-gpio', 'set-output', str(pin_number)])
def _set_input(self, pin_number):
if not pin_number is None:
subprocess.call(['fast-gpio', 'set-input', str(pin_number)])
def _write(self, pin_number, state):
if not pin_number is None:
subprocess.call(['fast-gpio', 'set', str(pin_number), str(state)])
def __init__(self):
if platform.system() == 'Linux':
for pin in self.pins:
fd = open(self.exportPath, 'w')
fd.write(str(pin))
fd.close()
def on(self, pin):
self.setPin(pin, 1)
def off(self, pin):
self.setPin(pin, 0)
def setPin(self, pin, value):
if platform.system() != "Linux":
# then we are not on the Omega, so simulate GPIO
pin_file = "./gpio/{0}.txt".format(str(pin))
f = open(pin_file, 'w')
f.write(str(value))
f.close()
return
# for some reason pin8 does not seem to work
if pin == 8:
self._set_output(value)
self._write(8, value)
else:
# Set direction as out
fd = open(self.pinDirectionPath.replace("$", str(pin)), 'w')
fd.write("out")
fd.close()
# Set value
fd = open(self.pinValuePath.replace("$", str(pin)), 'w')
fd.write(str(value))
fd.close()
def getPin(self, pin):
if platform.system() != "Linux":
# then we are not on the Omega, so simulate GPIO
pin_file = "./gpio/{0}.txt".format(str(pin))
f = open(pin_file, 'r')
value = f.readline()
f.close()
return int(value)
# Set direction as in
try:
fd = open(self.pinDirectionPath.replace("$", str(pin)), 'w')
fd.write("in")
fd.close()
except:
pass
# Get value
fd = open(self.pinValuePath.replace("$", str(pin)), 'r')
out = fd.read()
fd.close()
return int(out)
if __name__ == "__main__":
pin = 8
gpio = GPIOHelper()
gpio.setPin(pin, 1)
import time
time.sleep(2)
print("getPin: " + str(gpio.getPin(pin)))
gpio.setPin(pin, 0)