-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeGenerator.py
executable file
·256 lines (202 loc) · 6.78 KB
/
codeGenerator.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
import sys
import re
# Attribute object
class Attribute(object):
def __init__(self, type, name, size=0):
self.type = type
self.name = name
self.size = size
class Function(object):
def __init__(self, name="", type="", attributes=[]):
super(Function, self).__init__()
self.name = name
self.type = type
self.attributes = attributes
def getAttributesLen(self):
res=0
for attribute in self.attributes :
res+=int(attribute.size)
return res
class Flag(object):
def __init__(self, name, value):
self.name = name
self.value = value
def parseFile(filename):
# Read a file
file = open(filename, "r")
input = file.readlines()
print("Parsing "+filename)
functions = []
flags = []
new_function = None
for line in input:
# Parse tags
if re.match(r"^#\ @(\w+)", line):
words=line.split();
tag=words[1]
if tag == "@method":
if new_function!=None :
functions.append(new_function)
new_function = None
new_function=Function()
new_function.attributes=[] #fix a bug :/
new_function.name = words[2]
elif tag == "@type":
new_function.type = words[2]
elif tag == "@param":
type = words[2]
name = words[-1]
size = int(words[-2])
new_function.attributes.append(Attribute(type, name, size))
elif tag == "@flag":
name = words[2]
value = words[3]
flags.append(Flag(name, value));
if new_function!=None :
functions.append(new_function)
new_function = None
return functions, flags
def generatePython(filename,functions):
print("Generating python code "+filename)
# Write a file
output = ""
output+="from utils import *\n\n"
output+="class GeneratedProxy_i2c():\n\n"
# I2C Register
output += "i2c_registers = {\n"
i = 1
for function in functions:
output += " \"REG_" + function.name.upper() + "\":" + str(i) + ",\n"
i += 1
output += "}\n\n"
# Functions definition
for function in functions:
output += "def " + function.name + "(self,"
if function.type == "setter":
for attribute in function.attributes:
output += attribute.name + ","
output = output[:-1] # Remove the last character
output += "):\n vals=[]\n"
for attribute in function.attributes:
output += " vals.extend(split_" + attribute.type + "_"+ str(attribute.size) + "(" + attribute.name + "))\n"
output += " self.bus.write_block_data(self.address,self.i2c_registers['REG_" + function.name.upper() +"'],vals)\n\n"
elif function.type == "getter":
output += "):\n"
output += " vals=self.readBlock(self.i2c_registers['REG_" + function.name.upper() + "']," + str( 1+int(function.getAttributesLen()/8) ) + ")\n"
# output += " vals=self.bus.read_i2c_block_data(self.address,self.i2c_registers['REG_" + function.name.upper() + "']," + str( 1+int(function.getAttributesLen()/8) ) + ")\n"
output += " res=0\n"
offset = 0
for attribute in function.attributes:
output += " " +attribute.name + "=make_" + attribute.type + "_"+ str(attribute.size) + "(vals," + str(int(offset/8)) + ")\n"
offset += attribute.size
output += " return res,"
for attribute in function.attributes:
output += attribute.name + ","
output = output[:-1] # Remove the last character
output +="\n\n"
#little hack to reindent the file
output=output.replace("\n", "\n\t")
output=output.replace("\tclass", "class")
# Write a file
file = open(filename, "w")
file.write(output)
file.close
def typsize2c(type,size):
the_type=type
the_type=the_type.replace("integer","int"+str(size)+"_t")
the_type=the_type.replace("bool","int"+str(size)+"_t")
return the_type
def generateIno(filename,functions):
print("Generating arduino/c code "+filename)
# Write a file
output = ""
output += "#include \"proxi2c_generated.h\"\n\n"
# I2C Register
output += "enum i2c_registers\n"
output += "{\n"
i = 1
for function in functions:
output += " REG_" + function.name.upper() + "=" + str(i) + ",\n"
i += 1
output += "};\n\n"
output +="void i2c_runCmd(int i2c_reg,uint8_t * data_in,uint8_t * data_out,uint8_t * data_out_len)\n"
output +="{\n"
output += "\tswitch(i2c_reg) {\n"
for function in functions:
output += "\t\tcase REG_" + function.name.upper() + " :\n"
# output +="Serial.println(\"cmd_"+function.name+"\");\n"
if function.type == "setter":
output += "\t\t\tcmd_"+function.name+"( \n"
offset=0
for attribute in function.attributes:
the_type=typsize2c(attribute.type,attribute.size)
output += "\t\t\tMAKE" + the_type.upper()+"("
for i in range(0,int((attribute.size)/8)):
output += "data_in["+str(offset)+"],"
offset+=1
output = output[:-1]
output += "),\n"
output = output[:-2] # Remove the last character
output += " \n"
# //offset += attribute.size
output += "\t\t\t);\n"
elif function.type == "getter":
names=[]
for attribute in function.attributes:
the_type=typsize2c(attribute.type,attribute.size)
names.append("ret_"+function.name+"_"+attribute.name)
output += "\t\t\t"+the_type+" "+names[-1]+";\n"
output += "\t\t\tcmd_"+function.name+"( \n"
for name in names:
output += "\t\t\t\t&"+name+",\n"
output = output[:-2]
output += "\n\t\t\t);\n"
offset=0
# output+="uint8_t "+ function.name+"_out[256];\n"
i=0;
for attribute in function.attributes:
the_type=typsize2c(attribute.type,attribute.size)
output += "\t\t\tSPLIT" + the_type.upper()+"("+names[i]+",data_out,"+str(offset)+");\n"
# output += "\t\t\tSPLIT" + the_type.upper()+"("+names[0]+","+function.name+"_out,"+str(offset)+");\n"
offset+=int(attribute.size/8)
i+=1
output+="\t\t\t*data_out_len = "+str(offset)+";\n"
# output+="Wire.write("+ function.name+"_out,"+str(offset)+");\n";
output += "\t\tbreak;\n"
output += "\t};\n"
output += "};\n"
# Write a file
file = open(filename, "w")
file.write(output)
file.close
def generateHeader(filename,functions,flags):
print("Generating arduino/header code "+filename)
# Write a file
output = ""
output +="void i2c_runCmd(int i2c_reg,uint8_t * data_in,uint8_t * data_out,uint8_t * data_out_len);\n"
for function in functions:
output += "void cmd_"+function.name+"("
for attribute in function.attributes:
the_type=typsize2c(attribute.type,attribute.size)
if function.type == "setter":
output += the_type+" "+attribute.name+","
elif function.type == "getter":
output += the_type+"* "+attribute.name+","
output = output[:-1]
output += ");\n"
output += "\n"
for flag in flags:
output += "#define "+flag.name+" "+flag.value+"\n"
# Write a file
file = open(filename, "w")
file.write(output)
file.close
functions,flags = parseFile(sys.argv[1])
out_file = sys.argv[2]
if out_file.find(".py")>0:
generatePython(out_file,functions)
elif out_file.find(".ino")>0:
generateIno(out_file,functions)
generateHeader(out_file.replace(".ino",".h"),functions,flags)
else:
print("unsupported output file format")