etisserant@0: #!/usr/bin/env python etisserant@0: # -*- coding: utf-8 -*- lbessard@73: lbessard@73: #This file is part of CanFestival, a library implementing CanOpen Stack. lbessard@73: # lbessard@73: #Copyright (C): Edouard TISSERANT and Francis DUPIN lbessard@73: # lbessard@73: #See COPYING file for copyrights details. lbessard@73: # lbessard@73: #This library is free software; you can redistribute it and/or lbessard@73: #modify it under the terms of the GNU Lesser General Public lbessard@73: #License as published by the Free Software Foundation; either lbessard@73: #version 2.1 of the License, or (at your option) any later version. lbessard@73: # lbessard@73: #This library is distributed in the hope that it will be useful, lbessard@73: #but WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@73: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@73: #Lesser General Public License for more details. lbessard@73: # lbessard@73: #You should have received a copy of the GNU Lesser General Public lbessard@73: #License along with this library; if not, write to the Free Software lbessard@73: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: etisserant@0: from node import * etisserant@0: from types import * etisserant@0: etisserant@0: import re, os etisserant@0: etisserant@0: word_model = re.compile('([a-zA-Z_0-9]*)') etisserant@0: type_model = re.compile('([\_A-Z]*)([0-9]*)') etisserant@0: range_model = re.compile('([\_A-Z]*)([0-9]*)\[([\-0-9]*)-([\-0-9]*)\]') etisserant@0: etisserant@0: categories = [("SDO_SVR", 0x1200, 0x127F), ("SDO_CLT", 0x1280, 0x12FF), etisserant@0: ("PDO_RCV", 0x1400, 0x15FF), ("PDO_RCV_MAP", 0x1600, 0x17FF), etisserant@0: ("PDO_TRS", 0x1800, 0x19FF), ("PDO_TRS_MAP", 0x1A00, 0x1BFF)] etisserant@0: index_categories = ["firstIndex", "lastIndex"] etisserant@0: etisserant@0: generated_tag = """\n/* File generated by gen_cfile.py. Should not be modified. */\n""" etisserant@0: lbessard@188: internal_types = {} lbessard@418: default_string_size = 10 lbessard@188: etisserant@0: # Format a string for making a C++ variable etisserant@0: def FormatName(name): etisserant@0: wordlist = [word for word in word_model.findall(name) if word != ''] lbessard@227: return "_".join(wordlist) etisserant@0: etisserant@0: # Extract the informations from a given type name lbessard@418: def GetValidTypeInfos(typename, items=[]): lbessard@188: if typename in internal_types: lbessard@188: return internal_types[typename] lbessard@188: else: lbessard@188: result = type_model.match(typename) lbessard@188: if result: lbessard@188: values = result.groups() lbessard@188: if values[0] == "UNSIGNED" and int(values[1]) in [i * 8 for i in xrange(1, 9)]: lbessard@418: typeinfos = ("UNS%s"%values[1], None, "uint%s"%values[1], True) lbessard@188: elif values[0] == "INTEGER" and int(values[1]) in [i * 8 for i in xrange(1, 9)]: lbessard@418: typeinfos = ("INTEGER%s"%values[1], None, "int%s"%values[1], False) lbessard@188: elif values[0] == "REAL" and int(values[1]) in (32, 64): lbessard@418: typeinfos = ("%s%s"%(values[0], values[1]), None, "real%s"%values[1], False) lbessard@188: elif values[0] == "VISIBLE_STRING": lbessard@418: size = default_string_size lbessard@418: for item in items: lbessard@418: size = max(size, len(item)) lbessard@418: if values[1] != "": lbessard@418: size = max(size, int(values[1])) lbessard@418: typeinfos = ("UNS8", size, "visible_string", False) lbessard@188: elif values[0] == "DOMAIN": greg@527: size = 0 lbessard@420: for item in items: lbessard@420: size = max(size, len(item)) greg@526: typeinfos = ("UNS8", size, "domain", False) lbessard@188: elif values[0] == "BOOLEAN": lbessard@418: typeinfos = ("UNS8", None, "boolean", False) lbessard@188: else: lbessard@188: raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename greg@526: if typeinfos[2] not in ["visible_string", "domain"]: greg@526: internal_types[typename] = typeinfos lbessard@188: else: lbessard@188: raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename lbessard@188: return typeinfos etisserant@0: lbessard@453: def ComputeValue(type, value): lbessard@453: if type == "visible_string": lbessard@453: return "\"%s\""%value, "" lbessard@453: elif type == "domain": lbessard@453: return "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value]), "" lbessard@453: elif type.startswith("real"): lbessard@453: return "%f"%value, "" lbessard@453: else: lbessard@453: return "0x%X"%value, "\t/* %s */"%str(value) lbessard@453: etisserant@0: def WriteFile(filepath, content): etisserant@0: cfile = open(filepath,"w") etisserant@0: cfile.write(content) etisserant@0: cfile.close() etisserant@0: etisserant@484: def GenerateFileContent(Node, headerfilepath, pointers_dict = {}): etisserant@484: """ etisserant@484: pointers_dict = {(Idx,Sidx):"VariableName",...} etisserant@484: """ etisserant@0: global type lbessard@188: global internal_types lbessard@418: global default_string_size lbessard@418: etisserant@0: texts = {} etisserant@0: texts["maxPDOtransmit"] = 0 lbessard@245: texts["NodeName"] = Node.GetNodeName() lbessard@245: texts["NodeID"] = Node.GetNodeID() lbessard@245: texts["NodeType"] = Node.GetNodeType() lbessard@245: texts["Description"] = Node.GetNodeDescription() etisserant@0: texts["iam_a_slave"] = 0 etisserant@0: if (texts["NodeType"] == "slave"): etisserant@0: texts["iam_a_slave"] = 1 etisserant@0: lbessard@418: default_string_size = Node.GetDefaultStringSize() lbessard@418: etisserant@0: # Compiling lists of indexes lbessard@245: rangelist = [idx for idx in Node.GetIndexes() if 0 <= idx <= 0x260] lbessard@245: listIndex = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0xFFFF] lbessard@245: communicationlist = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0x11FF] lbessard@245: sdolist = [idx for idx in Node.GetIndexes() if 0x1200 <= idx <= 0x12FF] lbessard@245: pdolist = [idx for idx in Node.GetIndexes() if 0x1400 <= idx <= 0x1BFF] lbessard@245: variablelist = [idx for idx in Node.GetIndexes() if 0x2000 <= idx <= 0xBFFF] etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Declaration of the value range types etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: valueRangeContent = "" lbessard@286: strDefine = "\n#define valueRange_EMC 0x9F /* Type for index 0x1003 subindex 0x00 (only set of value 0 is possible) */" lbessard@286: strSwitch = """ case valueRange_EMC: luis@288: if (*(UNS8*)value != (UNS8)0) return OD_VALUE_RANGE_EXCEEDED; lbessard@286: break;\n""" lbessard@313: internal_types["valueRange_EMC"] = ("UNS8", "", "valueRange_EMC", True) etisserant@0: num = 0 etisserant@0: for index in rangelist: lbessard@245: rangename = Node.GetEntryName(index) etisserant@0: result = range_model.match(rangename) etisserant@0: if result: etisserant@0: num += 1 lbessard@245: typeindex = Node.GetEntry(index, 1) lbessard@245: typename = Node.GetTypeName(typeindex) etisserant@0: typeinfos = GetValidTypeInfos(typename) lbessard@188: internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num) etisserant@289: minvalue = Node.GetEntry(index, 2) etisserant@289: maxvalue = Node.GetEntry(index, 3) etisserant@289: strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],str(minvalue),str(maxvalue)) etisserant@289: strSwitch += " case valueRange_%d:\n"%(num) lbessard@313: if typeinfos[3] and minvalue <= 0: etisserant@289: strSwitch += " /* Negative or null low limit ignored because of unsigned type */;\n" etisserant@289: else: etisserant@289: strSwitch += " if (*(%s*)value < (%s)%s) return OD_VALUE_TOO_LOW;\n"%(typeinfos[0],typeinfos[0],str(minvalue)) etisserant@289: strSwitch += " if (*(%s*)value > (%s)%s) return OD_VALUE_TOO_HIGH;\n"%(typeinfos[0],typeinfos[0],str(maxvalue)) etisserant@289: strSwitch += " break;\n" etisserant@0: etisserant@0: valueRangeContent += strDefine etisserant@0: valueRangeContent += "\nUNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value)\n{"%texts etisserant@0: valueRangeContent += "\n switch (typeValue) {\n" etisserant@0: valueRangeContent += strSwitch etisserant@0: valueRangeContent += " }\n return 0;\n}\n" etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Creation of the mapped variables and object dictionary etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: mappedVariableContent = "" etisserant@484: pointedVariableContent = "" etisserant@0: strDeclareHeader = "" etisserant@0: strDeclareCallback = "" etisserant@0: indexContents = {} etisserant@0: indexCallbacks = {} etisserant@0: for index in listIndex: etisserant@0: texts["index"] = index etisserant@0: strIndex = "" lbessard@245: entry_infos = Node.GetEntryInfos(index) etisserant@229: texts["EntryName"] = entry_infos["name"].encode('ascii','replace') lbessard@245: values = Node.GetEntry(index) lbessard@245: callbacks = Node.HasEntryCallbacks(index) etisserant@0: if index in variablelist: etisserant@0: strIndex += "\n/* index 0x%(index)04X : Mapped variable %(EntryName)s */\n"%texts etisserant@0: else: etisserant@0: strIndex += "\n/* index 0x%(index)04X : %(EntryName)s. */\n"%texts etisserant@0: etisserant@0: # Entry type is VAR lbessard@515: if not isinstance(values, ListType): lbessard@245: subentry_infos = Node.GetSubentryInfos(index, 0) lbessard@245: typename = Node.GetTypeName(subentry_infos["type"]) lbessard@418: typeinfos = GetValidTypeInfos(typename, [values]) greg@527: if typename is "DOMAIN" and index in variablelist: greg@527: if not typeinfos[1]: greg@527: raise ValueError("\nDomain variable not initialized\nindex : 0x%04X\nsubindex 0x00"%index) etisserant@0: texts["subIndexType"] = typeinfos[0] lbessard@418: if typeinfos[1] is not None: lbessard@418: texts["suffixe"] = "[%d]"%typeinfos[1] lbessard@418: else: lbessard@418: texts["suffixe"] = "" lbessard@453: texts["value"], texts["comment"] = ComputeValue(typeinfos[2], values) etisserant@0: if index in variablelist: etisserant@0: texts["name"] = FormatName(subentry_infos["name"]) frdupin@76: strDeclareHeader += "extern %(subIndexType)s %(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00*/\n"%texts frdupin@76: mappedVariableContent += "%(subIndexType)s %(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00 */\n"%texts etisserant@0: else: lbessard@5: strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts etisserant@0: values = [values] etisserant@0: else: lbessard@245: subentry_infos = Node.GetSubentryInfos(index, 0) lbessard@245: typename = Node.GetTypeName(subentry_infos["type"]) lbessard@188: typeinfos = GetValidTypeInfos(typename) lbessard@286: if index == 0x1003: lbessard@286: texts["value"] = 0 lbessard@286: else: lbessard@286: texts["value"] = values[0] lbessard@188: texts["subIndexType"] = typeinfos[0] lbessard@188: strIndex += " %(subIndexType)s %(NodeName)s_highestSubIndex_obj%(index)04X = %(value)d; /* number of subindex - 1*/\n"%texts etisserant@0: etisserant@0: # Entry type is RECORD etisserant@0: if entry_infos["struct"] & OD_IdenticalSubindexes: lbessard@245: subentry_infos = Node.GetSubentryInfos(index, 1) lbessard@245: typename = Node.GetTypeName(subentry_infos["type"]) lbessard@418: typeinfos = GetValidTypeInfos(typename, values[1:]) etisserant@0: texts["subIndexType"] = typeinfos[0] lbessard@418: if typeinfos[1] is not None: lbessard@418: texts["suffixe"] = "[%d]"%typeinfos[1] greg@526: texts["type_suffixe"] = "*" lbessard@418: else: lbessard@418: texts["suffixe"] = "" greg@526: texts["type_suffixe"] = "" etisserant@0: texts["length"] = values[0] etisserant@0: if index in variablelist: etisserant@0: texts["name"] = FormatName(entry_infos["name"]) greg@526: strDeclareHeader += "extern %(subIndexType)s%(type_suffixe)s %(name)s[];\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n"%texts greg@526: mappedVariableContent += "%(subIndexType)s%(type_suffixe)s %(name)s[] =\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n {\n"%texts etisserant@0: for subIndex, value in enumerate(values): etisserant@0: sep = "," etisserant@0: if subIndex > 0: etisserant@0: if subIndex == len(values)-1: etisserant@0: sep = "" lbessard@453: value, comment = ComputeValue(typeinfos[2], value) greg@527: if len(value) is 2 and typename is "DOMAIN": greg@527: raise ValueError("\nDomain variable not initialized\nindex : 0x%04X\nsubindex 0x%02X"%(index, subIndex)) lbessard@5: mappedVariableContent += " %s%s%s\n"%(value, sep, comment) lbessard@28: mappedVariableContent += " };\n" etisserant@0: else: greg@526: strIndex += " %(subIndexType)s%(type_suffixe)s %(NodeName)s_obj%(index)04X[] = \n {\n"%texts etisserant@0: for subIndex, value in enumerate(values): etisserant@0: sep = "," etisserant@0: if subIndex > 0: etisserant@0: if subIndex == len(values)-1: etisserant@0: sep = "" lbessard@453: value, comment = ComputeValue(typeinfos[2], value) lbessard@5: strIndex += " %s%s%s\n"%(value, sep, comment) etisserant@0: strIndex += " };\n" etisserant@0: else: etisserant@0: lbessard@66: texts["parent"] = FormatName(entry_infos["name"]) etisserant@0: # Entry type is ARRAY etisserant@0: for subIndex, value in enumerate(values): etisserant@0: texts["subIndex"] = subIndex etisserant@0: if subIndex > 0: lbessard@245: subentry_infos = Node.GetSubentryInfos(index, subIndex) lbessard@245: typename = Node.GetTypeName(subentry_infos["type"]) lbessard@418: typeinfos = GetValidTypeInfos(typename, [values[subIndex]]) etisserant@0: texts["subIndexType"] = typeinfos[0] lbessard@418: if typeinfos[1] is not None: lbessard@418: texts["suffixe"] = "[%d]"%typeinfos[1] lbessard@418: else: lbessard@418: texts["suffixe"] = "" lbessard@453: texts["value"], texts["comment"] = ComputeValue(typeinfos[2], value) etisserant@0: texts["name"] = FormatName(subentry_infos["name"]) etisserant@0: if index in variablelist: frdupin@76: strDeclareHeader += "extern %(subIndexType)s %(parent)s_%(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x%(subIndex)02X */\n"%texts frdupin@76: 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 etisserant@0: else: lbessard@5: strIndex += " %(subIndexType)s %(NodeName)s_obj%(index)04X_%(name)s%(suffixe)s = %(value)s;%(comment)s\n"%texts etisserant@0: etisserant@0: # Generating Dictionary C++ entry etisserant@0: if callbacks: etisserant@0: if index in variablelist: etisserant@0: name = FormatName(entry_infos["name"]) etisserant@0: else: etisserant@0: name = "%(NodeName)s_Index%(index)04X"%texts etisserant@0: strIndex += " ODCallback_t %s_callbacks[] = \n {\n"%name etisserant@0: for subIndex in xrange(len(values)): etisserant@0: strIndex += " NULL,\n" etisserant@0: strIndex += " };\n" etisserant@0: indexCallbacks[index] = "*callbacks = %s_callbacks; "%name etisserant@0: else: etisserant@0: indexCallbacks[index] = "" etisserant@0: strIndex += " subindex %(NodeName)s_Index%(index)04X[] = \n {\n"%texts etisserant@0: for subIndex in xrange(len(values)): lbessard@245: subentry_infos = Node.GetSubentryInfos(index, subIndex) etisserant@0: if subIndex < len(values) - 1: etisserant@0: sep = "," etisserant@0: else: etisserant@0: sep = "" lbessard@245: typename = Node.GetTypeName(subentry_infos["type"]) lbessard@418: if entry_infos["struct"] & OD_IdenticalSubindexes: greg@526: typeinfos = GetValidTypeInfos(typename, values[1:]) lbessard@418: else: lbessard@418: typeinfos = GetValidTypeInfos(typename, [values[subIndex]]) etisserant@0: if subIndex == 0: lbessard@286: if index == 0x1003: lbessard@286: typeinfos = GetValidTypeInfos("valueRange_EMC") etisserant@0: if entry_infos["struct"] & OD_MultipleSubindexes: etisserant@0: name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts etisserant@0: elif index in variablelist: etisserant@0: name = FormatName(subentry_infos["name"]) etisserant@0: else: etisserant@0: name = FormatName("%s_obj%04X"%(texts["NodeName"], texts["index"])) etisserant@0: elif entry_infos["struct"] & OD_IdenticalSubindexes: etisserant@0: if index in variablelist: etisserant@0: name = "%s[%d]"%(FormatName(entry_infos["name"]), subIndex - 1) etisserant@0: else: etisserant@0: name = "%s_obj%04X[%d]"%(texts["NodeName"], texts["index"], subIndex - 1) etisserant@0: else: etisserant@0: if index in variablelist: lbessard@70: name = FormatName("%s_%s"%(entry_infos["name"],subentry_infos["name"])) etisserant@0: else: etisserant@0: name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"])) etisserant@421: if typeinfos[2] == "visible_string": etisserant@421: sizeof = str(max(len(values[subIndex]), default_string_size)) etisserant@421: elif typeinfos[2] == "domain": etisserant@421: sizeof = str(len(values[subIndex])) greg@176: else: greg@176: sizeof = "sizeof (%s)"%typeinfos[0] lbessard@245: params = Node.GetParamsEntry(index, subIndex) etisserant@0: if params["save"]: etisserant@0: save = "|TO_BE_SAVE" etisserant@0: else: etisserant@0: save = "" lbessard@188: strIndex += " { %s%s, %s, %s, (void*)&%s }%s\n"%(subentry_infos["access"].upper(),save,typeinfos[2],sizeof,name,sep) etisserant@484: pointer_name = pointers_dict.get((index, subIndex), None) etisserant@484: if pointer_name is not None: etisserant@484: pointedVariableContent += "%s* %s = &%s;\n"%(typeinfos[0], pointer_name, name) etisserant@0: strIndex += " };\n" etisserant@0: indexContents[index] = strIndex etisserant@484: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Declaration of Particular Parameters etisserant@0: #------------------------------------------------------------------------------- etisserant@0: luis@284: if 0x1003 not in communicationlist: luis@284: entry_infos = Node.GetEntryInfos(0x1003) luis@284: texts["EntryName"] = entry_infos["name"] luis@284: indexContents[0x1003] = """\n/* index 0x1003 : %(EntryName)s */ luis@284: UNS8 %(NodeName)s_highestSubIndex_obj1003 = 0; /* number of subindex - 1*/ luis@284: UNS32 %(NodeName)s_obj1003[] = luis@284: { luis@284: 0x0 /* 0 */ luis@284: }; luis@284: ODCallback_t %(NodeName)s_Index1003_callbacks[] = luis@284: { luis@284: NULL, luis@284: NULL, luis@284: }; luis@284: subindex %(NodeName)s_Index1003[] = luis@284: { lbessard@286: { RW, valueRange_EMC, sizeof (UNS8), (void*)&%(NodeName)s_highestSubIndex_obj1003 }, luis@284: { RO, uint32, sizeof (UNS32), (void*)&%(NodeName)s_obj1003[0] } luis@284: }; luis@284: """%texts luis@284: etisserant@261: if 0x1005 not in communicationlist: etisserant@261: entry_infos = Node.GetEntryInfos(0x1005) etisserant@261: texts["EntryName"] = entry_infos["name"] etisserant@261: indexContents[0x1005] = """\n/* index 0x1005 : %(EntryName)s */ etisserant@261: UNS32 %(NodeName)s_obj1005 = 0x0; /* 0 */ etisserant@261: """%texts etisserant@261: etisserant@0: if 0x1006 not in communicationlist: lbessard@245: entry_infos = Node.GetEntryInfos(0x1006) etisserant@0: texts["EntryName"] = entry_infos["name"] etisserant@0: indexContents[0x1006] = """\n/* index 0x1006 : %(EntryName)s */ frdupin@76: UNS32 %(NodeName)s_obj1006 = 0x0; /* 0 */ etisserant@0: """%texts etisserant@0: etisserant@314: if 0x1014 not in communicationlist: etisserant@314: entry_infos = Node.GetEntryInfos(0x1014) etisserant@314: texts["EntryName"] = entry_infos["name"] etisserant@314: indexContents[0x1014] = """\n/* index 0x1014 : %(EntryName)s */ luis@493: UNS32 %(NodeName)s_obj1014 = 0x80 + 0x%(NodeID)02X; /* 128 + NodeID */ etisserant@314: """%texts etisserant@314: etisserant@0: if 0x1016 in communicationlist: lbessard@325: texts["heartBeatTimers_number"] = Node.GetEntry(0x1016, 0) etisserant@0: else: lbessard@325: texts["heartBeatTimers_number"] = 0 lbessard@245: entry_infos = Node.GetEntryInfos(0x1016) etisserant@0: texts["EntryName"] = entry_infos["name"] etisserant@0: indexContents[0x1016] = """\n/* index 0x1016 : %(EntryName)s */ etisserant@0: UNS8 %(NodeName)s_highestSubIndex_obj1016 = 0; etisserant@91: UNS32 %(NodeName)s_obj1016[]={0}; etisserant@0: """%texts lbessard@325: etisserant@0: if 0x1017 not in communicationlist: lbessard@245: entry_infos = Node.GetEntryInfos(0x1017) etisserant@0: texts["EntryName"] = entry_infos["name"] etisserant@0: indexContents[0x1017] = """\n/* index 0x1017 : %(EntryName)s */ frdupin@76: UNS16 %(NodeName)s_obj1017 = 0x0; /* 0 */ etisserant@0: """%texts etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Declaration of navigation in the Object Dictionary etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: strDeclareIndex = "" etisserant@0: strDeclareSwitch = "" etisserant@0: strQuickIndex = "" etisserant@0: quick_index = {} etisserant@0: for index_cat in index_categories: etisserant@0: quick_index[index_cat] = {} etisserant@0: for cat, idx_min, idx_max in categories: etisserant@0: quick_index[index_cat][cat] = 0 etisserant@0: maxPDOtransmit = 0 etisserant@0: for i, index in enumerate(listIndex): etisserant@0: texts["index"] = index etisserant@0: 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 etisserant@0: strDeclareSwitch += " case 0x%04X: i = %d;%sbreak;\n"%(index, i, indexCallbacks[index]) etisserant@0: for cat, idx_min, idx_max in categories: etisserant@0: if idx_min <= index <= idx_max: etisserant@0: quick_index["lastIndex"][cat] = i etisserant@0: if quick_index["firstIndex"][cat] == 0: etisserant@0: quick_index["firstIndex"][cat] = i etisserant@0: if cat == "PDO_TRS": etisserant@0: maxPDOtransmit += 1 etisserant@0: texts["maxPDOtransmit"] = max(1, maxPDOtransmit) etisserant@0: for index_cat in index_categories: lbessard@479: strQuickIndex += "\nconst quick_index %s_%s = {\n"%(texts["NodeName"], index_cat) etisserant@0: sep = "," etisserant@0: for i, (cat, idx_min, idx_max) in enumerate(categories): etisserant@0: if i == len(categories) - 1: etisserant@0: sep = "" frdupin@76: strQuickIndex += " %d%s /* %s */\n"%(quick_index[index_cat][cat],sep,cat) etisserant@0: strQuickIndex += "};\n" etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Write File Content etisserant@0: #------------------------------------------------------------------------------- etisserant@0: lbessard@73: fileContent = generated_tag + """ etisserant@0: #include "%s" etisserant@0: """%(headerfilepath) etisserant@0: etisserant@0: fileContent += """ etisserant@0: /**************************************************************************/ etisserant@484: /* Declaration of mapped variables */ etisserant@0: /**************************************************************************/ etisserant@0: """ + mappedVariableContent etisserant@0: etisserant@0: fileContent += """ etisserant@0: /**************************************************************************/ etisserant@484: /* Declaration of value range types */ etisserant@0: /**************************************************************************/ etisserant@0: """ + valueRangeContent etisserant@0: etisserant@0: fileContent += """ etisserant@0: /**************************************************************************/ etisserant@0: /* The node id */ etisserant@0: /**************************************************************************/ etisserant@0: /* node_id default value.*/ etisserant@0: UNS8 %(NodeName)s_bDeviceNodeId = 0x%(NodeID)02X; etisserant@0: frdupin@76: /**************************************************************************/ etisserant@0: /* Array of message processing information */ etisserant@0: etisserant@0: const UNS8 %(NodeName)s_iam_a_slave = %(iam_a_slave)d; etisserant@0: etisserant@0: """%texts lbessard@325: if texts["heartBeatTimers_number"] > 0: etisserant@422: declaration = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[%(heartBeatTimers_number)d]"%texts etisserant@422: initializer = "{TIMER_NONE" + ",TIMER_NONE" * (texts["heartBeatTimers_number"] - 1) + "}" etisserant@422: fileContent += declaration + " = " + initializer + ";\n" lbessard@325: else: lbessard@325: fileContent += "TIMER_HANDLE %(NodeName)s_heartBeatTimers[1];\n"%texts etisserant@0: etisserant@0: fileContent += """ frdupin@76: /* frdupin@76: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ frdupin@76: frdupin@76: OBJECT DICTIONARY frdupin@76: frdupin@76: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ frdupin@76: */ etisserant@0: """%texts etisserant@0: contentlist = indexContents.keys() etisserant@0: contentlist.sort() etisserant@0: for index in contentlist: etisserant@0: fileContent += indexContents[index] etisserant@0: etisserant@0: fileContent += """ etisserant@484: /**************************************************************************/ etisserant@484: /* Declaration of pointed variables */ etisserant@484: /**************************************************************************/ etisserant@484: """ + pointedVariableContent etisserant@484: etisserant@484: fileContent += """ etisserant@0: const indextable %(NodeName)s_objdict[] = etisserant@0: { etisserant@0: """%texts etisserant@0: fileContent += strDeclareIndex etisserant@0: fileContent += """}; etisserant@0: etisserant@0: const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks) etisserant@0: { etisserant@0: int i; etisserant@0: *callbacks = NULL; etisserant@0: switch(wIndex){ etisserant@0: """%texts etisserant@0: fileContent += strDeclareSwitch etisserant@0: fileContent += """ default: etisserant@0: *errorCode = OD_NO_SUCH_OBJECT; etisserant@0: return NULL; etisserant@0: } etisserant@0: *errorCode = OD_SUCCESSFUL; etisserant@0: return &%(NodeName)s_objdict[i]; etisserant@0: } etisserant@0: etisserant@235: /* etisserant@235: * To count at which received SYNC a PDO must be sent. frdupin@76: * Even if no pdoTransmit are defined, at least one entry is computed frdupin@76: * for compilations issues. frdupin@76: */ etisserant@235: s_PDO_status %(NodeName)s_PDO_status[%(maxPDOtransmit)d] = {"""%texts etisserant@235: etisserant@366: fileContent += ",".join(["s_PDO_status_Initializer"]*texts["maxPDOtransmit"]) + """}; etisserant@235: """ etisserant@235: etisserant@0: fileContent += strQuickIndex etisserant@0: fileContent += """ lbessard@479: const UNS16 %(NodeName)s_ObjdictSize = sizeof(%(NodeName)s_objdict)/sizeof(%(NodeName)s_objdict[0]); etisserant@0: etisserant@0: CO_Data %(NodeName)s_Data = CANOPEN_NODE_DATA_INITIALIZER(%(NodeName)s); etisserant@0: etisserant@0: """%texts etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Write Header File Content etisserant@0: #------------------------------------------------------------------------------- etisserant@0: lbessard@223: texts["file_include_name"] = headerfilepath.replace(".", "_").upper() lbessard@73: HeaderFileContent = generated_tag + """ lbessard@224: #ifndef %(file_include_name)s lbessard@223: #define %(file_include_name)s lbessard@223: etisserant@0: #include "data.h" etisserant@0: frdupin@76: /* Prototypes of function provided by object dictionnary */ lbessard@63: UNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value); lbessard@63: const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks); lbessard@63: frdupin@76: /* Master node data struct */ etisserant@0: extern CO_Data %(NodeName)s_Data; etisserant@0: """%texts etisserant@0: HeaderFileContent += strDeclareHeader etisserant@0: lbessard@223: HeaderFileContent += "\n#endif // %(file_include_name)s\n"%texts lbessard@223: etisserant@0: return fileContent,HeaderFileContent etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Main Function etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@484: def GenerateFile(filepath, node, pointers_dict = {}): lbessard@188: try: lbessard@188: headerfilepath = os.path.splitext(filepath)[0]+".h" etisserant@484: content, header = GenerateFileContent(node, os.path.split(headerfilepath)[1], pointers_dict) lbessard@188: WriteFile(filepath, content) lbessard@188: WriteFile(headerfilepath, header) lbessard@188: return None lbessard@188: except ValueError, message: lbessard@188: return "Unable to Generate C File\n%s"%message lbessard@188: