-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyMCP23017.py
133 lines (90 loc) · 3.31 KB
/
pyMCP23017.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
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
import smbus, sys, subprocess
from time import sleep
soc = 0
gpioSetup = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
gpioOut = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
gpioPullUp = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
RgpioArowSet = 0x00
RgpioBrowSet = 0x01
RgpioArowOut = 0x12
RgpioBrowOut = 0x13
gppuA = 0x6
gppuB = 0x16
gpioAbinSet = ''
gpioBbinSet = ''
gpioAbinSetPu = ''
gpioBbinSetPu = ''
gpioAbinOut = ''
gpioBbinOut = ''
PUD_UP = 1
out = 0
class MCP23017:
def __init__(self, adress):
try:
check = subprocess.check_output(['sudo i2cdetect -y 0'])
except:
soc = 1
self.soc = soc
self.adress = adress
self.gpioSetup = gpioSetup
self.gpioOut = gpioOut
self.gpioPullUp = gpioPullUp
self.gpioAbinSet = gpioAbinSet
self.gpioBbinSet = gpioBbinSet
self.gpioAbinSetPu = gpioAbinSetPu
self.gpioBbinSetPu = gpioBbinSetPu
self.gpioAbinOut = gpioAbinOut
self.gpioBbinOut = gpioBbinOut
self.IN = 1
self.OUT = 0
self.HIGH = 1
self.LOW = 0
self.PUD_UP = PUD_UP
self.readArowStr = ''
self.readBrowStr = ''
self.readRowBin = []
self.bus = smbus.SMBus(self.soc)
def setup(self, pin, mode, pull_up_down = None):
self.gpioAbinSet = ''
self.gpioBbinSet = ''
self.gpioSetup[pin] = mode
for i in reversed(range(8)):
self.gpioAbinSet += str(self.gpioSetup[i])
self.gpioBbinSet += str(self.gpioSetup[i+8])
self.bus.write_byte_data(self.adress, RgpioArowSet, int(self.gpioAbinSet, 2))
self.bus.write_byte_data(self.adress, RgpioBrowSet, int(self.gpioBbinSet, 2))
if pull_up_down != None:
self._pullUp(pin, 1)
else:
self._pullUp(pin, 0)
self.pull_up_down = None
def output(self, pin, set):
self.gpioAbinOut = ''
self.gpioBbinOut = ''
self.gpioOut[pin] = set
for i in reversed(range(8)):
self.gpioAbinOut += str(self.gpioOut[i])
self.gpioBbinOut += str(self.gpioOut[i+8])
self.bus.write_byte_data(self.adress, RgpioArowOut, int(self.gpioAbinOut, 2))
self.bus.write_byte_data(self.adress, RgpioBrowOut, int(self.gpioBbinOut, 2))
def input(self, pin):
self.readArowStr = str(bin(self.bus.read_byte_data(self.adress, RgpioArowOut))[2:])
self.readBrowStr = str(bin(self.bus.read_byte_data(self.adress, RgpioBrowOut))[2:])
for y in reversed(self.readArowStr):
self.readRowBin.append(int(y))
for i in range(8-len(self.readRowBin)):
self.readRowBin.append(0)
for y in reversed(self.readBrowStr):
self.readRowBin.append(int(y))
for i in range(16-len(self.readRowBin)):
self.readRowBin.append(0)
return self.readRowBin[pin]
def _pullUp(self, pin, up_down):
self.gpioAbinSetPu = ''
self.gpioBbinSetPu = ''
self.gpioPullUp[pin] = up_down
for i in reversed(range(8)):
self.gpioAbinSetPu += str(self.gpioPullUp[i])
self.gpioBbinSetPu += str(self.gpioPullUp[i+8])
self.bus.write_byte_data(self.adress, gppuA, int(self.gpioAbinSetPu, 2))
self.bus.write_byte_data(self.adress, gppuB, int(self.gpioBbinSetPu, 2))