-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2ccontroller.py
41 lines (31 loc) · 934 Bytes
/
i2ccontroller.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
#!/user/bin/python
from devices.MCP230xx.MCP230xx import MCP230XX
#import time
import threading
lock = threading.RLock()
class I2cController():
mcp = {}
#def doalongthing(self, id):
# with lock:
# for i in range(0, 10):
# print "{0}: {1}".format(id, i)
# time.sleep(1)
def setoutput(self, bus, pin):
with lock:
mcp = self.getmcp(bus)
mcp.config(pin, mcp.OUTPUT)
def write(self, bus, pin, value):
with lock:
mcp = self.getmcp(bus)
mcp.output(pin, value)
def read(self, bus, pin):
with lock:
mcp = self.getmcp(bus)
mcp.pullup(pin, 1)
value = mcp.input(pin) >> pin
mcp.pullup(pin, 0)
return value
def getmcp(self, bus):
if not bus in self.mcp:
self.mcp[bus] = MCP230XX(bus)
return self.mcp[bus]