author | greg |
Tue, 25 Sep 2007 08:30:01 +0200 | |
changeset 282 | 6afe145df950 |
parent 261 | dbcd80bcab82 |
child 284 | 24bf3d692993 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
73 | 3 |
|
4 |
#This file is part of CanFestival, a library implementing CanOpen Stack. |
|
5 |
# |
|
6 |
#Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
7 |
# |
|
8 |
#See COPYING file for copyrights details. |
|
9 |
# |
|
10 |
#This library is free software; you can redistribute it and/or |
|
11 |
#modify it under the terms of the GNU Lesser General Public |
|
12 |
#License as published by the Free Software Foundation; either |
|
13 |
#version 2.1 of the License, or (at your option) any later version. |
|
14 |
# |
|
15 |
#This library is distributed in the hope that it will be useful, |
|
16 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
18 |
#Lesser General Public License for more details. |
|
19 |
# |
|
20 |
#You should have received a copy of the GNU Lesser General Public |
|
21 |
#License along with this library; if not, write to the Free Software |
|
22 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
0 | 23 |
|
24 |
from node import * |
|
25 |
from types import * |
|
26 |
||
27 |
import re, os |
|
28 |
||
29 |
word_model = re.compile('([a-zA-Z_0-9]*)') |
|
30 |
type_model = re.compile('([\_A-Z]*)([0-9]*)') |
|
31 |
range_model = re.compile('([\_A-Z]*)([0-9]*)\[([\-0-9]*)-([\-0-9]*)\]') |
|
32 |
||
33 |
categories = [("SDO_SVR", 0x1200, 0x127F), ("SDO_CLT", 0x1280, 0x12FF), |
|
34 |
("PDO_RCV", 0x1400, 0x15FF), ("PDO_RCV_MAP", 0x1600, 0x17FF), |
|
35 |
("PDO_TRS", 0x1800, 0x19FF), ("PDO_TRS_MAP", 0x1A00, 0x1BFF)] |
|
36 |
index_categories = ["firstIndex", "lastIndex"] |
|
37 |
||
38 |
generated_tag = """\n/* File generated by gen_cfile.py. Should not be modified. */\n""" |
|
39 |
||
188 | 40 |
internal_types = {} |
41 |
||
0 | 42 |
# Format a string for making a C++ variable |
43 |
def FormatName(name): |
|
44 |
wordlist = [word for word in word_model.findall(name) if word != ''] |
|
227 | 45 |
return "_".join(wordlist) |
0 | 46 |
|
47 |
# Extract the informations from a given type name |
|
48 |
def GetValidTypeInfos(typename): |
|
188 | 49 |
if typename in internal_types: |
50 |
return internal_types[typename] |
|
51 |
else: |
|
52 |
result = type_model.match(typename) |
|
53 |
if result: |
|
54 |
values = result.groups() |
|
55 |
if values[0] == "UNSIGNED" and int(values[1]) in [i * 8 for i in xrange(1, 9)]: |
|
56 |
typeinfos = ("UNS%s"%values[1], "", "uint%s"%values[1]) |
|
57 |
elif values[0] == "INTEGER" and int(values[1]) in [i * 8 for i in xrange(1, 9)]: |
|
58 |
typeinfos = ("INTEGER%s"%values[1], "", "int%s"%values[1]) |
|
59 |
elif values[0] == "REAL" and int(values[1]) in (32, 64): |
|
60 |
typeinfos = ("%s%s"%(values[0], values[1]), "", "real%s"%values[1]) |
|
61 |
elif values[0] == "VISIBLE_STRING": |
|
62 |
if values[1] == "": |
|
63 |
typeinfos = ("UNS8", "[10]", "visible_string") |
|
64 |
else: |
|
65 |
typeinfos = ("UNS8", "[%s]"%values[1], "visible_string") |
|
66 |
elif values[0] == "DOMAIN": |
|
67 |
typeinfos = ("UNS8*", "", "domain") |
|
68 |
elif values[0] == "BOOLEAN": |
|
69 |
typeinfos = ("UNS8", "", "boolean") |
|
70 |
else: |
|
71 |
raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename |
|
72 |
internal_types[typename] = typeinfos |
|
73 |
else: |
|
74 |
raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename |
|
75 |
return typeinfos |
|
0 | 76 |
|
77 |
def WriteFile(filepath, content): |
|
78 |
cfile = open(filepath,"w") |
|
79 |
cfile.write(content) |
|
80 |
cfile.close() |
|
81 |
||
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
82 |
def GenerateFileContent(Node, headerfilepath): |
0 | 83 |
global type |
188 | 84 |
global internal_types |
0 | 85 |
texts = {} |
86 |
texts["maxPDOtransmit"] = 0 |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
87 |
texts["NodeName"] = Node.GetNodeName() |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
88 |
texts["NodeID"] = Node.GetNodeID() |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
89 |
texts["NodeType"] = Node.GetNodeType() |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
90 |
texts["Description"] = Node.GetNodeDescription() |
0 | 91 |
texts["iam_a_slave"] = 0 |
92 |
if (texts["NodeType"] == "slave"): |
|
93 |
texts["iam_a_slave"] = 1 |
|
94 |
||
95 |
# Compiling lists of indexes |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
96 |
rangelist = [idx for idx in Node.GetIndexes() if 0 <= idx <= 0x260] |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
97 |
listIndex = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0xFFFF] |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
98 |
communicationlist = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0x11FF] |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
99 |
sdolist = [idx for idx in Node.GetIndexes() if 0x1200 <= idx <= 0x12FF] |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
100 |
pdolist = [idx for idx in Node.GetIndexes() if 0x1400 <= idx <= 0x1BFF] |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
101 |
variablelist = [idx for idx in Node.GetIndexes() if 0x2000 <= idx <= 0xBFFF] |
0 | 102 |
|
103 |
#------------------------------------------------------------------------------- |
|
104 |
# Declaration of the value range types |
|
105 |
#------------------------------------------------------------------------------- |
|
106 |
||
107 |
valueRangeContent = "" |
|
108 |
strDefine = "" |
|
109 |
strSwitch = "" |
|
110 |
num = 0 |
|
111 |
for index in rangelist: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
112 |
rangename = Node.GetEntryName(index) |
0 | 113 |
result = range_model.match(rangename) |
114 |
if result: |
|
115 |
num += 1 |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
116 |
typeindex = Node.GetEntry(index, 1) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
117 |
typename = Node.GetTypeName(typeindex) |
0 | 118 |
typeinfos = GetValidTypeInfos(typename) |
188 | 119 |
internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num) |
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
120 |
minvalue = str(Node.GetEntry(index, 2)) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
121 |
maxvalue = str(Node.GetEntry(index, 3)) |
188 | 122 |
strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],minvalue,maxvalue) |
0 | 123 |
strSwitch += """ case valueRange_%d: |
124 |
if (*(%s*)Value < (%s)%s) return OD_VALUE_TOO_LOW; |
|
125 |
if (*(%s*)Value > (%s)%s) return OD_VALUE_TOO_HIGH; |
|
188 | 126 |
break;\n"""%(num,typeinfos[0],typeinfos[0],minvalue,typeinfos[0],typeinfos[0],maxvalue) |
0 | 127 |
|
128 |
valueRangeContent += strDefine |
|
129 |
valueRangeContent += "\nUNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value)\n{"%texts |
|
130 |
valueRangeContent += "\n switch (typeValue) {\n" |
|
131 |
valueRangeContent += strSwitch |
|
132 |
valueRangeContent += " }\n return 0;\n}\n" |
|
133 |
||
134 |
#------------------------------------------------------------------------------- |
|
135 |
# Creation of the mapped variables and object dictionary |
|
136 |
#------------------------------------------------------------------------------- |
|
137 |
||
138 |
mappedVariableContent = "" |
|
139 |
strDeclareHeader = "" |
|
140 |
strDeclareCallback = "" |
|
141 |
indexContents = {} |
|
142 |
indexCallbacks = {} |
|
143 |
for index in listIndex: |
|
144 |
texts["index"] = index |
|
145 |
strIndex = "" |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
146 |
entry_infos = Node.GetEntryInfos(index) |
229 | 147 |
texts["EntryName"] = entry_infos["name"].encode('ascii','replace') |
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
148 |
values = Node.GetEntry(index) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
149 |
callbacks = Node.HasEntryCallbacks(index) |
0 | 150 |
if index in variablelist: |
151 |
strIndex += "\n/* index 0x%(index)04X : Mapped variable %(EntryName)s */\n"%texts |
|
152 |
else: |
|
153 |
strIndex += "\n/* index 0x%(index)04X : %(EntryName)s. */\n"%texts |
|
154 |
||
155 |
# Entry type is VAR |
|
156 |
if type(values) != ListType: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
157 |
subentry_infos = Node.GetSubentryInfos(index, 0) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
158 |
typename = Node.GetTypeName(subentry_infos["type"]) |
0 | 159 |
typeinfos = GetValidTypeInfos(typename) |
160 |
texts["subIndexType"] = typeinfos[0] |
|
161 |
texts["suffixe"] = typeinfos[1] |
|
162 |
if typeinfos[2] == "visible_string": |
|
163 |
texts["value"] = "\"%s\""%values |
|
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
164 |
texts["comment"] = "" |
223 | 165 |
elif typeinfos[2] == "domain": |
166 |
texts["value"] = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value]) |
|
167 |
texts["comment"] = "" |
|
0 | 168 |
else: |
169 |
texts["value"] = "0x%X"%values |
|
76 | 170 |
texts["comment"] = "\t/* %s */"%str(values) |
0 | 171 |
if index in variablelist: |
172 |
texts["name"] = FormatName(subentry_infos["name"]) |
|
76 | 173 |
strDeclareHeader += "extern %(subIndexType)s %(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00*/\n"%texts |
0 | 174 |
if callbacks: |
76 | 175 |
strDeclareHeader += "extern ODCallback_t %(name)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts |
176 |
mappedVariableContent += "%(subIndexType)s %(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00 */\n"%texts |
|
0 | 177 |
else: |
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
178 |
strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts |
0 | 179 |
values = [values] |
180 |
else: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
181 |
subentry_infos = Node.GetSubentryInfos(index, 0) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
182 |
typename = Node.GetTypeName(subentry_infos["type"]) |
188 | 183 |
typeinfos = GetValidTypeInfos(typename) |
184 |
texts["value"] = values[0] |
|
185 |
texts["subIndexType"] = typeinfos[0] |
|
186 |
strIndex += " %(subIndexType)s %(NodeName)s_highestSubIndex_obj%(index)04X = %(value)d; /* number of subindex - 1*/\n"%texts |
|
0 | 187 |
|
188 |
# Entry type is RECORD |
|
189 |
if entry_infos["struct"] & OD_IdenticalSubindexes: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
190 |
subentry_infos = Node.GetSubentryInfos(index, 1) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
191 |
typename = Node.GetTypeName(subentry_infos["type"]) |
0 | 192 |
typeinfos = GetValidTypeInfos(typename) |
193 |
texts["subIndexType"] = typeinfos[0] |
|
194 |
texts["suffixe"] = typeinfos[1] |
|
195 |
texts["length"] = values[0] |
|
196 |
if index in variablelist: |
|
197 |
texts["name"] = FormatName(entry_infos["name"]) |
|
76 | 198 |
strDeclareHeader += "extern %(subIndexType)s %(name)s[%(length)d]%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n"%texts |
0 | 199 |
if callbacks: |
76 | 200 |
strDeclareHeader += "extern ODCallback_t %(name)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts |
201 |
mappedVariableContent += "%(subIndexType)s %(name)s[] =\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n {\n"%texts |
|
0 | 202 |
for subIndex, value in enumerate(values): |
203 |
sep = "," |
|
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
204 |
comment = "" |
0 | 205 |
if subIndex > 0: |
206 |
if subIndex == len(values)-1: |
|
207 |
sep = "" |
|
208 |
if typeinfos[2] == "visible_string": |
|
209 |
value = "\"%s\""%value |
|
223 | 210 |
elif typeinfos[2] == "domain": |
211 |
value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value]) |
|
227 | 212 |
else: |
76 | 213 |
comment = "\t/* %s */"%str(value) |
0 | 214 |
value = "0x%X"%value |
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
215 |
mappedVariableContent += " %s%s%s\n"%(value, sep, comment) |
28 | 216 |
mappedVariableContent += " };\n" |
0 | 217 |
else: |
218 |
strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X[] = \n {\n"%texts |
|
219 |
for subIndex, value in enumerate(values): |
|
220 |
sep = "," |
|
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
221 |
comment = "" |
0 | 222 |
if subIndex > 0: |
223 |
if subIndex == len(values)-1: |
|
224 |
sep = "" |
|
225 |
if typeinfos[2] == "visible_string": |
|
226 |
value = "\"%s\""%value |
|
182
988f2b302aa6
Adding support for importing and exporting EDS files
lbessard
parents:
176
diff
changeset
|
227 |
elif typeinfos[2] == "domain": |
176 | 228 |
value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value]) |
0 | 229 |
else: |
76 | 230 |
comment = "\t/* %s */"%str(value) |
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
231 |
value = "0x%X"%value |
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
232 |
strIndex += " %s%s%s\n"%(value, sep, comment) |
0 | 233 |
strIndex += " };\n" |
234 |
else: |
|
235 |
||
66
94212a58c097
gen_cfile.py modified for avoiding possible conflict in mapped variable names
lbessard
parents:
64
diff
changeset
|
236 |
texts["parent"] = FormatName(entry_infos["name"]) |
0 | 237 |
# Entry type is ARRAY |
238 |
for subIndex, value in enumerate(values): |
|
239 |
texts["subIndex"] = subIndex |
|
240 |
if subIndex > 0: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
241 |
subentry_infos = Node.GetSubentryInfos(index, subIndex) |
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
242 |
typename = Node.GetTypeName(subentry_infos["type"]) |
0 | 243 |
typeinfos = GetValidTypeInfos(typename) |
244 |
texts["subIndexType"] = typeinfos[0] |
|
245 |
texts["suffixe"] = typeinfos[1] |
|
246 |
if typeinfos[2] == "visible_string": |
|
247 |
texts["value"] = "\"%s\""%value |
|
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
248 |
texts["comment"] = "" |
223 | 249 |
elif typeinfos[2] == "domain": |
250 |
texts["value"] = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value]) |
|
251 |
texts["comment"] = "" |
|
227 | 252 |
else: |
0 | 253 |
texts["value"] = "0x%X"%value |
76 | 254 |
texts["comment"] = "\t/* %s */"%str(value) |
0 | 255 |
texts["name"] = FormatName(subentry_infos["name"]) |
256 |
if index in variablelist: |
|
76 | 257 |
strDeclareHeader += "extern %(subIndexType)s %(parent)s_%(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x%(subIndex)02X */\n"%texts |
258 |
mappedVariableContent += "%(subIndexType)s %(parent)s_%(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x%(subIndex)02X */\n"%texts |
|
0 | 259 |
else: |
5
e4365e7d47f0
Bug on number in hexa computed by gen_cfile corrected
lbessard
parents:
0
diff
changeset
|
260 |
strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X_%(name)s%(suffixe)s = %(value)s;%(comment)s\n"%texts |
0 | 261 |
if callbacks: |
76 | 262 |
strDeclareHeader += "extern ODCallback_t %(parent)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts |
0 | 263 |
|
264 |
# Generating Dictionary C++ entry |
|
265 |
if callbacks: |
|
266 |
if index in variablelist: |
|
267 |
name = FormatName(entry_infos["name"]) |
|
268 |
else: |
|
269 |
name = "%(NodeName)s_Index%(index)04X"%texts |
|
270 |
strIndex += " ODCallback_t %s_callbacks[] = \n {\n"%name |
|
271 |
for subIndex in xrange(len(values)): |
|
272 |
strIndex += " NULL,\n" |
|
273 |
strIndex += " };\n" |
|
274 |
indexCallbacks[index] = "*callbacks = %s_callbacks; "%name |
|
275 |
else: |
|
276 |
indexCallbacks[index] = "" |
|
277 |
strIndex += " subindex %(NodeName)s_Index%(index)04X[] = \n {\n"%texts |
|
278 |
for subIndex in xrange(len(values)): |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
279 |
subentry_infos = Node.GetSubentryInfos(index, subIndex) |
0 | 280 |
if subIndex < len(values) - 1: |
281 |
sep = "," |
|
282 |
else: |
|
283 |
sep = "" |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
284 |
typename = Node.GetTypeName(subentry_infos["type"]) |
0 | 285 |
typeinfos = GetValidTypeInfos(typename) |
286 |
if subIndex == 0: |
|
287 |
if entry_infos["struct"] & OD_MultipleSubindexes: |
|
288 |
name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts |
|
289 |
elif index in variablelist: |
|
290 |
name = FormatName(subentry_infos["name"]) |
|
291 |
else: |
|
292 |
name = FormatName("%s_obj%04X"%(texts["NodeName"], texts["index"])) |
|
293 |
elif entry_infos["struct"] & OD_IdenticalSubindexes: |
|
294 |
if index in variablelist: |
|
295 |
name = "%s[%d]"%(FormatName(entry_infos["name"]), subIndex - 1) |
|
296 |
else: |
|
297 |
name = "%s_obj%04X[%d]"%(texts["NodeName"], texts["index"], subIndex - 1) |
|
298 |
else: |
|
299 |
if index in variablelist: |
|
70 | 300 |
name = FormatName("%s_%s"%(entry_infos["name"],subentry_infos["name"])) |
0 | 301 |
else: |
302 |
name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"])) |
|
188 | 303 |
if typeinfos[2] in ["visible_string", "domain"]: |
176 | 304 |
sizeof = str(len(values[subIndex])) |
305 |
else: |
|
306 |
sizeof = "sizeof (%s)"%typeinfos[0] |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
307 |
params = Node.GetParamsEntry(index, subIndex) |
0 | 308 |
if params["save"]: |
309 |
save = "|TO_BE_SAVE" |
|
310 |
else: |
|
311 |
save = "" |
|
188 | 312 |
strIndex += " { %s%s, %s, %s, (void*)&%s }%s\n"%(subentry_infos["access"].upper(),save,typeinfos[2],sizeof,name,sep) |
0 | 313 |
strIndex += " };\n" |
314 |
indexContents[index] = strIndex |
|
315 |
||
316 |
#------------------------------------------------------------------------------- |
|
317 |
# Declaration of Particular Parameters |
|
318 |
#------------------------------------------------------------------------------- |
|
319 |
||
261
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
320 |
if 0x1005 not in communicationlist: |
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
321 |
entry_infos = Node.GetEntryInfos(0x1005) |
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
322 |
texts["EntryName"] = entry_infos["name"] |
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
323 |
indexContents[0x1005] = """\n/* index 0x1005 : %(EntryName)s */ |
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
324 |
UNS32 %(NodeName)s_obj1005 = 0x0; /* 0 */ |
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
325 |
"""%texts |
dbcd80bcab82
Prevent potential problem with missing 0x1005 OD entry
etisserant
parents:
245
diff
changeset
|
326 |
|
0 | 327 |
if 0x1006 not in communicationlist: |
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
328 |
entry_infos = Node.GetEntryInfos(0x1006) |
0 | 329 |
texts["EntryName"] = entry_infos["name"] |
330 |
indexContents[0x1006] = """\n/* index 0x1006 : %(EntryName)s */ |
|
76 | 331 |
UNS32 %(NodeName)s_obj1006 = 0x0; /* 0 */ |
0 | 332 |
"""%texts |
333 |
||
334 |
if 0x1016 in communicationlist: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
335 |
texts["nombre"] = Node.GetEntry(0x1016, 0) |
0 | 336 |
else: |
337 |
texts["nombre"] = 0 |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
338 |
entry_infos = Node.GetEntryInfos(0x1016) |
0 | 339 |
texts["EntryName"] = entry_infos["name"] |
340 |
indexContents[0x1016] = """\n/* index 0x1016 : %(EntryName)s */ |
|
341 |
UNS8 %(NodeName)s_highestSubIndex_obj1016 = 0; |
|
91
ed2612282988
- Better array initialization in data.h CANOPEN_NODE_DATA_INITIALIZER macro. Use a little hack with configure and config.h to create the "pure Ansi C" initializer.
etisserant
parents:
76
diff
changeset
|
342 |
UNS32 %(NodeName)s_obj1016[]={0}; |
0 | 343 |
"""%texts |
344 |
if texts["nombre"] > 0: |
|
345 |
strTimers = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[%(nombre)d] = {TIMER_NONE,};\n"%texts |
|
346 |
else: |
|
76 | 347 |
strTimers = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[1];\n"%texts |
0 | 348 |
|
349 |
if 0x1017 not in communicationlist: |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
350 |
entry_infos = Node.GetEntryInfos(0x1017) |
0 | 351 |
texts["EntryName"] = entry_infos["name"] |
352 |
indexContents[0x1017] = """\n/* index 0x1017 : %(EntryName)s */ |
|
76 | 353 |
UNS16 %(NodeName)s_obj1017 = 0x0; /* 0 */ |
0 | 354 |
"""%texts |
355 |
||
356 |
#------------------------------------------------------------------------------- |
|
357 |
# Declaration of navigation in the Object Dictionary |
|
358 |
#------------------------------------------------------------------------------- |
|
359 |
||
360 |
strDeclareIndex = "" |
|
361 |
strDeclareSwitch = "" |
|
362 |
strQuickIndex = "" |
|
363 |
quick_index = {} |
|
364 |
for index_cat in index_categories: |
|
365 |
quick_index[index_cat] = {} |
|
366 |
for cat, idx_min, idx_max in categories: |
|
367 |
quick_index[index_cat][cat] = 0 |
|
368 |
maxPDOtransmit = 0 |
|
369 |
for i, index in enumerate(listIndex): |
|
370 |
texts["index"] = index |
|
371 |
strDeclareIndex += " { (subindex*)%(NodeName)s_Index%(index)04X,sizeof(%(NodeName)s_Index%(index)04X)/sizeof(%(NodeName)s_Index%(index)04X[0]), 0x%(index)04X},\n"%texts |
|
372 |
strDeclareSwitch += " case 0x%04X: i = %d;%sbreak;\n"%(index, i, indexCallbacks[index]) |
|
373 |
for cat, idx_min, idx_max in categories: |
|
374 |
if idx_min <= index <= idx_max: |
|
375 |
quick_index["lastIndex"][cat] = i |
|
376 |
if quick_index["firstIndex"][cat] == 0: |
|
377 |
quick_index["firstIndex"][cat] = i |
|
378 |
if cat == "PDO_TRS": |
|
379 |
maxPDOtransmit += 1 |
|
380 |
texts["maxPDOtransmit"] = max(1, maxPDOtransmit) |
|
381 |
for index_cat in index_categories: |
|
382 |
strQuickIndex += "\nquick_index %s_%s = {\n"%(texts["NodeName"], index_cat) |
|
383 |
sep = "," |
|
384 |
for i, (cat, idx_min, idx_max) in enumerate(categories): |
|
385 |
if i == len(categories) - 1: |
|
386 |
sep = "" |
|
76 | 387 |
strQuickIndex += " %d%s /* %s */\n"%(quick_index[index_cat][cat],sep,cat) |
0 | 388 |
strQuickIndex += "};\n" |
389 |
||
390 |
#------------------------------------------------------------------------------- |
|
391 |
# Write File Content |
|
392 |
#------------------------------------------------------------------------------- |
|
393 |
||
73 | 394 |
fileContent = generated_tag + """ |
0 | 395 |
#include "%s" |
396 |
"""%(headerfilepath) |
|
397 |
||
398 |
fileContent += """ |
|
399 |
/**************************************************************************/ |
|
400 |
/* Declaration of the mapped variables */ |
|
401 |
/**************************************************************************/ |
|
402 |
""" + mappedVariableContent |
|
403 |
||
404 |
fileContent += """ |
|
405 |
/**************************************************************************/ |
|
406 |
/* Declaration of the value range types */ |
|
407 |
/**************************************************************************/ |
|
408 |
""" + valueRangeContent |
|
409 |
||
410 |
fileContent += """ |
|
411 |
/**************************************************************************/ |
|
412 |
/* The node id */ |
|
413 |
/**************************************************************************/ |
|
414 |
/* node_id default value.*/ |
|
415 |
UNS8 %(NodeName)s_bDeviceNodeId = 0x%(NodeID)02X; |
|
416 |
||
76 | 417 |
/**************************************************************************/ |
0 | 418 |
/* Array of message processing information */ |
419 |
||
420 |
const UNS8 %(NodeName)s_iam_a_slave = %(iam_a_slave)d; |
|
421 |
||
422 |
"""%texts |
|
423 |
fileContent += strTimers |
|
424 |
||
425 |
fileContent += """ |
|
76 | 426 |
/* |
427 |
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
|
428 |
||
429 |
OBJECT DICTIONARY |
|
430 |
||
431 |
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
|
432 |
*/ |
|
0 | 433 |
"""%texts |
434 |
contentlist = indexContents.keys() |
|
435 |
contentlist.sort() |
|
436 |
for index in contentlist: |
|
437 |
fileContent += indexContents[index] |
|
438 |
||
439 |
fileContent += """ |
|
440 |
const indextable %(NodeName)s_objdict[] = |
|
441 |
{ |
|
442 |
"""%texts |
|
443 |
fileContent += strDeclareIndex |
|
444 |
fileContent += """}; |
|
445 |
||
446 |
const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks) |
|
447 |
{ |
|
448 |
int i; |
|
449 |
*callbacks = NULL; |
|
450 |
switch(wIndex){ |
|
451 |
"""%texts |
|
452 |
fileContent += strDeclareSwitch |
|
453 |
fileContent += """ default: |
|
454 |
*errorCode = OD_NO_SUCH_OBJECT; |
|
455 |
return NULL; |
|
456 |
} |
|
457 |
*errorCode = OD_SUCCESSFUL; |
|
458 |
return &%(NodeName)s_objdict[i]; |
|
459 |
} |
|
460 |
||
235
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
461 |
/* |
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
462 |
* To count at which received SYNC a PDO must be sent. |
76 | 463 |
* Even if no pdoTransmit are defined, at least one entry is computed |
464 |
* for compilations issues. |
|
465 |
*/ |
|
235
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
466 |
s_PDO_status %(NodeName)s_PDO_status[%(maxPDOtransmit)d] = {"""%texts |
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
467 |
|
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
468 |
fileContent += ",".join(["s_PDO_staus_Initializer"]*texts["maxPDOtransmit"]) + """}; |
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
469 |
""" |
f812bf6b7237
Preliminary implementation of Event Timer and Inhibit Timer driven TPDO
etisserant
parents:
229
diff
changeset
|
470 |
|
0 | 471 |
fileContent += strQuickIndex |
472 |
fileContent += """ |
|
473 |
UNS16 %(NodeName)s_ObjdictSize = sizeof(%(NodeName)s_objdict)/sizeof(%(NodeName)s_objdict[0]); |
|
474 |
||
475 |
CO_Data %(NodeName)s_Data = CANOPEN_NODE_DATA_INITIALIZER(%(NodeName)s); |
|
476 |
||
477 |
"""%texts |
|
478 |
||
479 |
#------------------------------------------------------------------------------- |
|
480 |
# Write Header File Content |
|
481 |
#------------------------------------------------------------------------------- |
|
482 |
||
223 | 483 |
texts["file_include_name"] = headerfilepath.replace(".", "_").upper() |
73 | 484 |
HeaderFileContent = generated_tag + """ |
224 | 485 |
#ifndef %(file_include_name)s |
223 | 486 |
#define %(file_include_name)s |
487 |
||
0 | 488 |
#include "data.h" |
489 |
||
76 | 490 |
/* Prototypes of function provided by object dictionnary */ |
63
2be18e405e40
Bug on map variable type changing and on comments with special characters corrected
lbessard
parents:
60
diff
changeset
|
491 |
UNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value); |
2be18e405e40
Bug on map variable type changing and on comments with special characters corrected
lbessard
parents:
60
diff
changeset
|
492 |
const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks); |
2be18e405e40
Bug on map variable type changing and on comments with special characters corrected
lbessard
parents:
60
diff
changeset
|
493 |
|
76 | 494 |
/* Master node data struct */ |
0 | 495 |
extern CO_Data %(NodeName)s_Data; |
496 |
"""%texts |
|
497 |
HeaderFileContent += strDeclareHeader |
|
498 |
||
223 | 499 |
HeaderFileContent += "\n#endif // %(file_include_name)s\n"%texts |
500 |
||
0 | 501 |
return fileContent,HeaderFileContent |
502 |
||
503 |
#------------------------------------------------------------------------------- |
|
504 |
# Main Function |
|
505 |
#------------------------------------------------------------------------------- |
|
506 |
||
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
507 |
def GenerateFile(filepath, node): |
188 | 508 |
try: |
509 |
headerfilepath = os.path.splitext(filepath)[0]+".h" |
|
245
d43ebbed895f
Modifying gen_cfile.py for generating C file from a node as data rather than a manager
lbessard
parents:
235
diff
changeset
|
510 |
content, header = GenerateFileContent(node, os.path.split(headerfilepath)[1]) |
188 | 511 |
WriteFile(filepath, content) |
512 |
WriteFile(headerfilepath, header) |
|
513 |
return None |
|
514 |
except ValueError, message: |
|
515 |
return "Unable to Generate C File\n%s"%message |
|
516 |