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