-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateIO.py
299 lines (242 loc) · 8.16 KB
/
updateIO.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
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
#!/usr/bin/python
def binary(num, pre='0b', length=16, spacer=0):
return '{0}{{:{1}>{2}}}'.format(pre, spacer, length).format(bin(num)[2:])
i = 0
tab = {}
tab2 = {}
indices = []
names = []
iodirs = []
regularIO = 0
mcpIO = 0
mcpIO0 = 0
mcpIO1 = 0
mcpIO2 = 0
mcpIO3 = 0
mcpIO4 = 0
mcpIO5 = 0
mcpIO6 = 0
mcpIO7 = 0
with open("io.tab", "r") as f:
lines = f.readlines()
for line in lines:
# tab = line.split('\t','\t\t','\t\t\t','\t\t\t\t','\s*')
tab = line.split('\t')
if tab[0] != '\n': # if not newline char
if tab[0][:-2] == "MCP": # if MCP
mcpIO += 1
elif mcpIO == 1:
mcpIO0 += 1
elif mcpIO == 2:
mcpIO1 += 1
elif mcpIO == 3:
mcpIO2 += 1
elif mcpIO == 4:
mcpIO3 += 1
elif mcpIO == 5:
mcpIO4 += 1
elif mcpIO == 6:
mcpIO5 += 1
elif mcpIO == 7:
mcpIO6 += 1
elif mcpIO == 8:
mcpIO7 += 1
else: # regular IO
regularIO += 1
if tab[0][:-2] != "MCP":
indices.append( tab[0] )
names.append( tab[1] )
iodirs.append( tab[2][:-1] )
f.close()
highestIO = 0
nMCPp = 0
nPin = 0
#structNames = ["mcp1", "mcp2", "mcp3", "mcp4", "mcp5", "mcp6", "mcp7", "mcp8"]
#print("{} {} {}".format(indices, names, iodirs))
### HEADER FILE
ioCounter = 0
with open("src/basics/io.h", "w") as f:
for i in range(0, regularIO):
if names[i] != "-":
f.write("\n#define " + names[i] + " " + indices[i])
# f.write('\n')
if mcpIO > 0:
ioCounter += regularIO
for i in range(0, mcpIO0):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], indices[i + ioCounter]))
# f.write('\n')
if mcpIO > 1:
ioCounter += mcpIO0
for i in range(0, mcpIO1):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+16))
# f.write('\n')
if mcpIO > 2:
ioCounter += mcpIO1
for i in range(0, mcpIO2):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+32))
# f.write('\n')
if mcpIO > 3:
ioCounter += mcpIO2
for i in range(0, mcpIO3):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+48))
# f.write('\n')
if mcpIO > 4:
ioCounter += mcpIO3
for i in range(0, mcpIO4):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+64))
# f.write('\n')
if mcpIO > 5:
ioCounter += mcpIO4
for i in range(0, mcpIO5):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+80))
# f.write('\n')
if mcpIO > 6:
ioCounter += mcpIO5
for i in range(0, mcpIO6):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+96))
# f.write('\n')
if mcpIO > 7:
ioCounter += mcpIO6
for i in range(0, mcpIO7):
f.write("{} {} {}".format("\n#define", names[i + ioCounter], int(indices[i + ioCounter])+112))
f.write("\n\nextern void initIO(void);")
if mcpIO > 0:
f.write("""
extern void mcpWrite(unsigned char pin, unsigned char state);
extern unsigned char mcpRead(unsigned char pin);""")
f.close()
iodir = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
pullup = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
ioCounter = 0
address = 0x20
## cpp FILE
with open("src/basics/io.cpp", "w") as f:
f.write('#include <Arduino.h>\n')
f.write('#include "io.h"\n')
if mcpIO > 0:
f.write('#include <Wire.h>\n')
# I2C extenders
f.write("""
#define portA 0x12
#define portB 0x13
#define iodirRegA 0x00
#define pullUpRegA 0x0C
static void initMcp(unsigned char address, unsigned int iodir, unsigned int pullUp) {
Wire.beginTransmission(address);
Wire.write(iodirRegA);
Wire.write(iodir >> 8);
Wire.write(iodir);
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write(pullUpRegA);
Wire.write(pullUp >> 8);
Wire.write(pullUp);
Wire.endTransmission();
}
extern void mcpWrite(unsigned char pin, unsigned char state) {
unsigned char address = 0x20, port, IO;
address += (pin / 16); // select address
if(pin % 16 < 8) port = portA; // select port
else port = portB;
pin %= 8 ;
Wire.beginTransmission(address);
Wire.write(port);
Wire.endTransmission();
Wire.requestFrom(address, 1);
IO = Wire.read(); // fetch current IO status
Wire.beginTransmission(address);
Wire.write(port);
if(state) IO |= (1 << pin); // OR 'IO' with pin
else IO &= ~(1 << pin); // or AND the inverse of pin
Wire.write(IO); // transmit the updated IO
Wire.endTransmission();
}
extern unsigned char mcpRead(unsigned char pin) {
unsigned char address = 0x20, port, IO;
address += (pin / 16); // select address
if((pin % 16) < 8) port = portA; // select port
else port = portB;
pin %= 8 ;
Wire.beginTransmission(address);
Wire.write(port);
Wire.endTransmission();
Wire.requestFrom(address, 1);
IO = Wire.read();
if(IO & (1 << pin)) return 1;
else return 0;
}
""")
f.write("extern void initIO(void) {\n")
if mcpIO > 0:
f.write("\tWire.begin();\n")
for i in range(0, regularIO):
f.write("\tpinMode(" + names[i] + ', ' + iodirs[i] + ");\n")
try:
if mcpIO > 0:
ioCounter += regularIO
for i in range(0, mcpIO0):
if iodirs[i+ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[0] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[0] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x20, {0:b}".format(iodir[0])+ ', ' + "{0:b}".format(pullup[0]) +");\n")
if mcpIO > 1:
ioCounter += mcpIO0
for i in range(0, mcpIO1):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[1] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[1] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x21, {0:b}".format(iodir[1])+ ', ' + "{0:b}".format(pullup[1]) +");\n")
if mcpIO > 2:
ioCounter += mcpIO1
for i in range(0, mcpIO2):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[2] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[2] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x22, {0:b}".format(iodir[2])+ ', ' + "{0:b}".format(pullup[2]) +");\n")
if mcpIO > 3:
ioCounter += mcpIO2
for i in range(0, mcpIO3):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[3] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[3] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x23, {0:b}".format(iodir[3])+ ', ' + "{0:b}".format(pullup[3]) +");\n")
if mcpIO > 4:
ioCounter += mcpIO3
for i in range(0, mcpIO4):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[4] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[4] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x24, {0:b}".format(iodir[4])+ ', ' + "{0:b}".format(pullup[4]) +");\n")
if mcpIO > 5:
ioCounter += mcpIO4
for i in range(0, mcpIO5):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[5] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[5] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x25, {0:b}".format(iodir[5])+ ', ' + "{0:b}".format(pullup[5]) +");\n")
if mcpIO > 6:
ioCounter += mcpIO5
for i in range(0, mcpIO6):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[6] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[6] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x26, {0:b}".format(iodir[6])+ ', ' + "{0:b}".format(pullup[6]) +");\n")
if mcpIO > 7:
ioCounter += mcpIO6
for i in range(0, mcpIO7):
if iodirs[i + ioCounter] == "INPUT_PULLUP" or iodirs[i + ioCounter] == "INPUT":
iodir[7] |= (1 << int(indices[i + ioCounter]))
if iodirs[i + ioCounter] == "INPUT_PULLUP":
pullup[7] |= (1 << int(indices[i + ioCounter]))
f.write("\tinitMcp(0x27, {0:b}".format(iodir[7])+ ', ' + "{0:b}".format(pullup[7]) +");\n")
f.write("}")
except:
print("{} {}".format(i, ioCounter))
f.close()