77 def WriteFile(filepath, content): |
77 def WriteFile(filepath, content): |
78 cfile = open(filepath,"w") |
78 cfile = open(filepath,"w") |
79 cfile.write(content) |
79 cfile.write(content) |
80 cfile.close() |
80 cfile.close() |
81 |
81 |
82 def GenerateFileContent(Manager, headerfilepath): |
82 def GenerateFileContent(Node, headerfilepath): |
83 global type |
83 global type |
84 global internal_types |
84 global internal_types |
85 texts = {} |
85 texts = {} |
86 texts["maxPDOtransmit"] = 0 |
86 texts["maxPDOtransmit"] = 0 |
87 texts["NodeName"], texts["NodeID"], texts["NodeType"], texts["Description"] = Manager.GetCurrentNodeInfos() |
87 texts["NodeName"] = Node.GetNodeName() |
|
88 texts["NodeID"] = Node.GetNodeID() |
|
89 texts["NodeType"] = Node.GetNodeType() |
|
90 texts["Description"] = Node.GetNodeDescription() |
88 texts["iam_a_slave"] = 0 |
91 texts["iam_a_slave"] = 0 |
89 if (texts["NodeType"] == "slave"): |
92 if (texts["NodeType"] == "slave"): |
90 texts["iam_a_slave"] = 1 |
93 texts["iam_a_slave"] = 1 |
91 |
94 |
92 # Compiling lists of indexes |
95 # Compiling lists of indexes |
93 rangelist = [idx for name,idx in Manager.GetCurrentValidIndexes(0, 0x260)] |
96 rangelist = [idx for idx in Node.GetIndexes() if 0 <= idx <= 0x260] |
94 listIndex = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1000, 0xFFFF)] |
97 listIndex = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0xFFFF] |
95 communicationlist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1000, 0x11FF)] |
98 communicationlist = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0x11FF] |
96 sdolist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1200, 0x12FF)] |
99 sdolist = [idx for idx in Node.GetIndexes() if 0x1200 <= idx <= 0x12FF] |
97 pdolist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1400, 0x1BFF)] |
100 pdolist = [idx for idx in Node.GetIndexes() if 0x1400 <= idx <= 0x1BFF] |
98 variablelist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x2000, 0xBFFF)] |
101 variablelist = [idx for idx in Node.GetIndexes() if 0x2000 <= idx <= 0xBFFF] |
99 |
102 |
100 #------------------------------------------------------------------------------- |
103 #------------------------------------------------------------------------------- |
101 # Declaration of the value range types |
104 # Declaration of the value range types |
102 #------------------------------------------------------------------------------- |
105 #------------------------------------------------------------------------------- |
103 |
106 |
104 valueRangeContent = "" |
107 valueRangeContent = "" |
105 strDefine = "" |
108 strDefine = "" |
106 strSwitch = "" |
109 strSwitch = "" |
107 num = 0 |
110 num = 0 |
108 for index in rangelist: |
111 for index in rangelist: |
109 rangename = Manager.GetEntryName(index) |
112 rangename = Node.GetEntryName(index) |
110 result = range_model.match(rangename) |
113 result = range_model.match(rangename) |
111 if result: |
114 if result: |
112 num += 1 |
115 num += 1 |
113 typeindex = Manager.GetCurrentEntry(index, 1) |
116 typeindex = Node.GetEntry(index, 1) |
114 typename = Manager.GetTypeName(typeindex) |
117 typename = Node.GetTypeName(typeindex) |
115 typeinfos = GetValidTypeInfos(typename) |
118 typeinfos = GetValidTypeInfos(typename) |
116 internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num) |
119 internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num) |
117 minvalue = str(Manager.GetCurrentEntry(index, 2)) |
120 minvalue = str(Node.GetEntry(index, 2)) |
118 maxvalue = str(Manager.GetCurrentEntry(index, 3)) |
121 maxvalue = str(Node.GetEntry(index, 3)) |
119 strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],minvalue,maxvalue) |
122 strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],minvalue,maxvalue) |
120 strSwitch += """ case valueRange_%d: |
123 strSwitch += """ case valueRange_%d: |
121 if (*(%s*)Value < (%s)%s) return OD_VALUE_TOO_LOW; |
124 if (*(%s*)Value < (%s)%s) return OD_VALUE_TOO_LOW; |
122 if (*(%s*)Value > (%s)%s) return OD_VALUE_TOO_HIGH; |
125 if (*(%s*)Value > (%s)%s) return OD_VALUE_TOO_HIGH; |
123 break;\n"""%(num,typeinfos[0],typeinfos[0],minvalue,typeinfos[0],typeinfos[0],maxvalue) |
126 break;\n"""%(num,typeinfos[0],typeinfos[0],minvalue,typeinfos[0],typeinfos[0],maxvalue) |
138 indexContents = {} |
141 indexContents = {} |
139 indexCallbacks = {} |
142 indexCallbacks = {} |
140 for index in listIndex: |
143 for index in listIndex: |
141 texts["index"] = index |
144 texts["index"] = index |
142 strIndex = "" |
145 strIndex = "" |
143 entry_infos = Manager.GetEntryInfos(index) |
146 entry_infos = Node.GetEntryInfos(index) |
144 texts["EntryName"] = entry_infos["name"].encode('ascii','replace') |
147 texts["EntryName"] = entry_infos["name"].encode('ascii','replace') |
145 values = Manager.GetCurrentEntry(index) |
148 values = Node.GetEntry(index) |
146 callbacks = Manager.HasCurrentEntryCallbacks(index) |
149 callbacks = Node.HasEntryCallbacks(index) |
147 if index in variablelist: |
150 if index in variablelist: |
148 strIndex += "\n/* index 0x%(index)04X : Mapped variable %(EntryName)s */\n"%texts |
151 strIndex += "\n/* index 0x%(index)04X : Mapped variable %(EntryName)s */\n"%texts |
149 else: |
152 else: |
150 strIndex += "\n/* index 0x%(index)04X : %(EntryName)s. */\n"%texts |
153 strIndex += "\n/* index 0x%(index)04X : %(EntryName)s. */\n"%texts |
151 |
154 |
152 # Entry type is VAR |
155 # Entry type is VAR |
153 if type(values) != ListType: |
156 if type(values) != ListType: |
154 subentry_infos = Manager.GetSubentryInfos(index, 0) |
157 subentry_infos = Node.GetSubentryInfos(index, 0) |
155 typename = Manager.GetTypeName(subentry_infos["type"]) |
158 typename = Node.GetTypeName(subentry_infos["type"]) |
156 typeinfos = GetValidTypeInfos(typename) |
159 typeinfos = GetValidTypeInfos(typename) |
157 texts["subIndexType"] = typeinfos[0] |
160 texts["subIndexType"] = typeinfos[0] |
158 texts["suffixe"] = typeinfos[1] |
161 texts["suffixe"] = typeinfos[1] |
159 if typeinfos[2] == "visible_string": |
162 if typeinfos[2] == "visible_string": |
160 texts["value"] = "\"%s\""%values |
163 texts["value"] = "\"%s\""%values |
173 mappedVariableContent += "%(subIndexType)s %(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00 */\n"%texts |
176 mappedVariableContent += "%(subIndexType)s %(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00 */\n"%texts |
174 else: |
177 else: |
175 strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts |
178 strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts |
176 values = [values] |
179 values = [values] |
177 else: |
180 else: |
178 subentry_infos = Manager.GetSubentryInfos(index, 0) |
181 subentry_infos = Node.GetSubentryInfos(index, 0) |
179 typename = Manager.GetTypeName(subentry_infos["type"]) |
182 typename = Node.GetTypeName(subentry_infos["type"]) |
180 typeinfos = GetValidTypeInfos(typename) |
183 typeinfos = GetValidTypeInfos(typename) |
181 texts["value"] = values[0] |
184 texts["value"] = values[0] |
182 texts["subIndexType"] = typeinfos[0] |
185 texts["subIndexType"] = typeinfos[0] |
183 strIndex += " %(subIndexType)s %(NodeName)s_highestSubIndex_obj%(index)04X = %(value)d; /* number of subindex - 1*/\n"%texts |
186 strIndex += " %(subIndexType)s %(NodeName)s_highestSubIndex_obj%(index)04X = %(value)d; /* number of subindex - 1*/\n"%texts |
184 |
187 |
185 # Entry type is RECORD |
188 # Entry type is RECORD |
186 if entry_infos["struct"] & OD_IdenticalSubindexes: |
189 if entry_infos["struct"] & OD_IdenticalSubindexes: |
187 subentry_infos = Manager.GetSubentryInfos(index, 1) |
190 subentry_infos = Node.GetSubentryInfos(index, 1) |
188 typename = Manager.GetTypeName(subentry_infos["type"]) |
191 typename = Node.GetTypeName(subentry_infos["type"]) |
189 typeinfos = GetValidTypeInfos(typename) |
192 typeinfos = GetValidTypeInfos(typename) |
190 texts["subIndexType"] = typeinfos[0] |
193 texts["subIndexType"] = typeinfos[0] |
191 texts["suffixe"] = typeinfos[1] |
194 texts["suffixe"] = typeinfos[1] |
192 texts["length"] = values[0] |
195 texts["length"] = values[0] |
193 if index in variablelist: |
196 if index in variablelist: |
233 texts["parent"] = FormatName(entry_infos["name"]) |
236 texts["parent"] = FormatName(entry_infos["name"]) |
234 # Entry type is ARRAY |
237 # Entry type is ARRAY |
235 for subIndex, value in enumerate(values): |
238 for subIndex, value in enumerate(values): |
236 texts["subIndex"] = subIndex |
239 texts["subIndex"] = subIndex |
237 if subIndex > 0: |
240 if subIndex > 0: |
238 subentry_infos = Manager.GetSubentryInfos(index, subIndex) |
241 subentry_infos = Node.GetSubentryInfos(index, subIndex) |
239 typename = Manager.GetTypeName(subentry_infos["type"]) |
242 typename = Node.GetTypeName(subentry_infos["type"]) |
240 typeinfos = GetValidTypeInfos(typename) |
243 typeinfos = GetValidTypeInfos(typename) |
241 texts["subIndexType"] = typeinfos[0] |
244 texts["subIndexType"] = typeinfos[0] |
242 texts["suffixe"] = typeinfos[1] |
245 texts["suffixe"] = typeinfos[1] |
243 if typeinfos[2] == "visible_string": |
246 if typeinfos[2] == "visible_string": |
244 texts["value"] = "\"%s\""%value |
247 texts["value"] = "\"%s\""%value |
271 indexCallbacks[index] = "*callbacks = %s_callbacks; "%name |
274 indexCallbacks[index] = "*callbacks = %s_callbacks; "%name |
272 else: |
275 else: |
273 indexCallbacks[index] = "" |
276 indexCallbacks[index] = "" |
274 strIndex += " subindex %(NodeName)s_Index%(index)04X[] = \n {\n"%texts |
277 strIndex += " subindex %(NodeName)s_Index%(index)04X[] = \n {\n"%texts |
275 for subIndex in xrange(len(values)): |
278 for subIndex in xrange(len(values)): |
276 subentry_infos = Manager.GetSubentryInfos(index, subIndex) |
279 subentry_infos = Node.GetSubentryInfos(index, subIndex) |
277 if subIndex < len(values) - 1: |
280 if subIndex < len(values) - 1: |
278 sep = "," |
281 sep = "," |
279 else: |
282 else: |
280 sep = "" |
283 sep = "" |
281 typename = Manager.GetTypeName(subentry_infos["type"]) |
284 typename = Node.GetTypeName(subentry_infos["type"]) |
282 typeinfos = GetValidTypeInfos(typename) |
285 typeinfos = GetValidTypeInfos(typename) |
283 if subIndex == 0: |
286 if subIndex == 0: |
284 if entry_infos["struct"] & OD_MultipleSubindexes: |
287 if entry_infos["struct"] & OD_MultipleSubindexes: |
285 name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts |
288 name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts |
286 elif index in variablelist: |
289 elif index in variablelist: |
299 name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"])) |
302 name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"])) |
300 if typeinfos[2] in ["visible_string", "domain"]: |
303 if typeinfos[2] in ["visible_string", "domain"]: |
301 sizeof = str(len(values[subIndex])) |
304 sizeof = str(len(values[subIndex])) |
302 else: |
305 else: |
303 sizeof = "sizeof (%s)"%typeinfos[0] |
306 sizeof = "sizeof (%s)"%typeinfos[0] |
304 params = Manager.GetCurrentParamsEntry(index, subIndex) |
307 params = Node.GetParamsEntry(index, subIndex) |
305 if params["save"]: |
308 if params["save"]: |
306 save = "|TO_BE_SAVE" |
309 save = "|TO_BE_SAVE" |
307 else: |
310 else: |
308 save = "" |
311 save = "" |
309 strIndex += " { %s%s, %s, %s, (void*)&%s }%s\n"%(subentry_infos["access"].upper(),save,typeinfos[2],sizeof,name,sep) |
312 strIndex += " { %s%s, %s, %s, (void*)&%s }%s\n"%(subentry_infos["access"].upper(),save,typeinfos[2],sizeof,name,sep) |
313 #------------------------------------------------------------------------------- |
316 #------------------------------------------------------------------------------- |
314 # Declaration of Particular Parameters |
317 # Declaration of Particular Parameters |
315 #------------------------------------------------------------------------------- |
318 #------------------------------------------------------------------------------- |
316 |
319 |
317 if 0x1006 not in communicationlist: |
320 if 0x1006 not in communicationlist: |
318 entry_infos = Manager.GetEntryInfos(0x1006) |
321 entry_infos = Node.GetEntryInfos(0x1006) |
319 texts["EntryName"] = entry_infos["name"] |
322 texts["EntryName"] = entry_infos["name"] |
320 indexContents[0x1006] = """\n/* index 0x1006 : %(EntryName)s */ |
323 indexContents[0x1006] = """\n/* index 0x1006 : %(EntryName)s */ |
321 UNS32 %(NodeName)s_obj1006 = 0x0; /* 0 */ |
324 UNS32 %(NodeName)s_obj1006 = 0x0; /* 0 */ |
322 """%texts |
325 """%texts |
323 |
326 |
324 if 0x1016 in communicationlist: |
327 if 0x1016 in communicationlist: |
325 texts["nombre"] = Manager.GetCurrentEntry(0x1016, 0) |
328 texts["nombre"] = Node.GetEntry(0x1016, 0) |
326 else: |
329 else: |
327 texts["nombre"] = 0 |
330 texts["nombre"] = 0 |
328 entry_infos = Manager.GetEntryInfos(0x1016) |
331 entry_infos = Node.GetEntryInfos(0x1016) |
329 texts["EntryName"] = entry_infos["name"] |
332 texts["EntryName"] = entry_infos["name"] |
330 indexContents[0x1016] = """\n/* index 0x1016 : %(EntryName)s */ |
333 indexContents[0x1016] = """\n/* index 0x1016 : %(EntryName)s */ |
331 UNS8 %(NodeName)s_highestSubIndex_obj1016 = 0; |
334 UNS8 %(NodeName)s_highestSubIndex_obj1016 = 0; |
332 UNS32 %(NodeName)s_obj1016[]={0}; |
335 UNS32 %(NodeName)s_obj1016[]={0}; |
333 """%texts |
336 """%texts |
492 |
495 |
493 #------------------------------------------------------------------------------- |
496 #------------------------------------------------------------------------------- |
494 # Main Function |
497 # Main Function |
495 #------------------------------------------------------------------------------- |
498 #------------------------------------------------------------------------------- |
496 |
499 |
497 def GenerateFile(filepath, manager): |
500 def GenerateFile(filepath, node): |
498 try: |
501 try: |
499 headerfilepath = os.path.splitext(filepath)[0]+".h" |
502 headerfilepath = os.path.splitext(filepath)[0]+".h" |
500 content, header = GenerateFileContent(manager, os.path.split(headerfilepath)[1]) |
503 content, header = GenerateFileContent(node, os.path.split(headerfilepath)[1]) |
501 WriteFile(filepath, content) |
504 WriteFile(filepath, content) |
502 WriteFile(headerfilepath, header) |
505 WriteFile(headerfilepath, header) |
503 return None |
506 return None |
504 except ValueError, message: |
507 except ValueError, message: |
505 return "Unable to Generate C File\n%s"%message |
508 return "Unable to Generate C File\n%s"%message |