nico@207: nico@207: nico@207: CanFestival: /home/epimerde/documents/tc11/CanFestival-3/objdictgen/gen_cfile.py Source File nico@207: nico@207: nico@207: nico@207: nico@207:
nico@207:
nico@207:
nico@207:
nico@207:

/home/epimerde/documents/tc11/CanFestival-3/objdictgen/gen_cfile.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
nico@207: 00002 # -*- coding: utf-8 -*-
nico@207: 00003 
nico@207: 00004 #This file is part of CanFestival, a library implementing CanOpen Stack. 
nico@207: 00005 #
nico@207: 00006 #Copyright (C): Edouard TISSERANT and Francis DUPIN
nico@207: 00007 #
nico@207: 00008 #See COPYING file for copyrights details.
nico@207: 00009 #
nico@207: 00010 #This library is free software; you can redistribute it and/or
nico@207: 00011 #modify it under the terms of the GNU Lesser General Public
nico@207: 00012 #License as published by the Free Software Foundation; either
nico@207: 00013 #version 2.1 of the License, or (at your option) any later version.
nico@207: 00014 #
nico@207: 00015 #This library is distributed in the hope that it will be useful,
nico@207: 00016 #but WITHOUT ANY WARRANTY; without even the implied warranty of
nico@207: 00017 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
nico@207: 00018 #Lesser General Public License for more details.
nico@207: 00019 #
nico@207: 00020 #You should have received a copy of the GNU Lesser General Public
nico@207: 00021 #License along with this library; if not, write to the Free Software
nico@207: 00022 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
nico@207: 00023 
nico@207: 00024 from node import *
nico@207: 00025 from types import *
nico@207: 00026 
nico@207: 00027 import re, os
nico@207: 00028 
nico@207: 00029 word_model = re.compile('([a-zA-Z_0-9]*)')
nico@207: 00030 type_model = re.compile('([\_A-Z]*)([0-9]*)')
nico@207: 00031 range_model = re.compile('([\_A-Z]*)([0-9]*)\[([\-0-9]*)-([\-0-9]*)\]')
nico@207: 00032 
nico@207: 00033 categories = [("SDO_SVR", 0x1200, 0x127F), ("SDO_CLT", 0x1280, 0x12FF),
nico@207: 00034               ("PDO_RCV", 0x1400, 0x15FF), ("PDO_RCV_MAP", 0x1600, 0x17FF),
nico@207: 00035               ("PDO_TRS", 0x1800, 0x19FF), ("PDO_TRS_MAP", 0x1A00, 0x1BFF)]
nico@207: 00036 index_categories = ["firstIndex", "lastIndex"]
nico@207: 00037 
nico@207: 00038 generated_tag = """\n/* File generated by gen_cfile.py. Should not be modified. */\n"""
nico@207: 00039 
nico@207: 00040 internal_types = {}
nico@207: 00041 
nico@207: 00042 # Format a string for making a C++ variable
nico@207: 00043 def FormatName(name):
nico@207: 00044     wordlist = [word for word in word_model.findall(name) if word != '']
nico@207: 00045     result = ''
nico@207: 00046     sep = ''
nico@207: 00047     for word in wordlist:
nico@207: 00048         result += "%s%s"%(sep,word)
nico@207: 00049         sep = '_'
nico@207: 00050     return result
nico@207: 00051 
nico@207: 00052 # Extract the informations from a given type name
nico@207: 00053 def GetValidTypeInfos(typename):
nico@207: 00054     if typename in internal_types:
nico@207: 00055         return internal_types[typename]
nico@207: 00056     else:
nico@207: 00057         result = type_model.match(typename)
nico@207: 00058         if result:
nico@207: 00059             values = result.groups()
nico@207: 00060             if values[0] == "UNSIGNED" and int(values[1]) in [i * 8 for i in xrange(1, 9)]:
nico@207: 00061                 typeinfos = ("UNS%s"%values[1], "", "uint%s"%values[1])
nico@207: 00062             elif values[0] == "INTEGER" and int(values[1]) in [i * 8 for i in xrange(1, 9)]:
nico@207: 00063                 typeinfos = ("INTEGER%s"%values[1], "", "int%s"%values[1])
nico@207: 00064             elif values[0] == "REAL" and int(values[1]) in (32, 64):
nico@207: 00065                 typeinfos = ("%s%s"%(values[0], values[1]), "", "real%s"%values[1])
nico@207: 00066             elif values[0] == "VISIBLE_STRING":
nico@207: 00067                 if values[1] == "":
nico@207: 00068                     typeinfos = ("UNS8", "[10]", "visible_string")
nico@207: 00069                 else:
nico@207: 00070                     typeinfos = ("UNS8", "[%s]"%values[1], "visible_string")
nico@207: 00071             elif values[0] == "DOMAIN":
nico@207: 00072                 typeinfos = ("UNS8*", "", "domain")
nico@207: 00073             elif values[0] == "BOOLEAN":
nico@207: 00074                 typeinfos = ("UNS8", "", "boolean")
nico@207: 00075             else:
nico@207: 00076                 raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
nico@207: 00077             internal_types[typename] = typeinfos
nico@207: 00078         else:
nico@207: 00079             raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
nico@207: 00080     return typeinfos
nico@207: 00081 
nico@207: 00082 def WriteFile(filepath, content):
nico@207: 00083     cfile = open(filepath,"w")
nico@207: 00084     cfile.write(content)
nico@207: 00085     cfile.close()
nico@207: 00086 
nico@207: 00087 def GenerateFileContent(Manager, headerfilepath):
nico@207: 00088     global type
nico@207: 00089     global internal_types
nico@207: 00090     texts = {}
nico@207: 00091     texts["maxPDOtransmit"] = 0
nico@207: 00092     texts["NodeName"], texts["NodeID"], texts["NodeType"], texts["Description"] = Manager.GetCurrentNodeInfos()
nico@207: 00093     texts["iam_a_slave"] = 0
nico@207: 00094     if (texts["NodeType"] == "slave"):
nico@207: 00095         texts["iam_a_slave"] = 1
nico@207: 00096     
nico@207: 00097     # Compiling lists of indexes
nico@207: 00098     rangelist = [idx for name,idx in Manager.GetCurrentValidIndexes(0, 0x260)]
nico@207: 00099     listIndex = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1000, 0xFFFF)]
nico@207: 00100     communicationlist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1000, 0x11FF)]
nico@207: 00101     sdolist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1200, 0x12FF)]
nico@207: 00102     pdolist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x1400, 0x1BFF)]
nico@207: 00103     variablelist = [idx for name,idx in Manager.GetCurrentValidIndexes(0x2000, 0xBFFF)]
nico@207: 00104 
nico@207: 00105 #-------------------------------------------------------------------------------
nico@207: 00106 #                       Declaration of the value range types
nico@207: 00107 #-------------------------------------------------------------------------------    
nico@207: 00108     
nico@207: 00109     valueRangeContent = ""
nico@207: 00110     strDefine = ""
nico@207: 00111     strSwitch = ""
nico@207: 00112     num = 0
nico@207: 00113     for index in rangelist:
nico@207: 00114         rangename = Manager.GetEntryName(index)
nico@207: 00115         result = range_model.match(rangename)
nico@207: 00116         if result:
nico@207: 00117             num += 1
nico@207: 00118             typeindex = Manager.GetCurrentEntry(index, 1)
nico@207: 00119             typename = Manager.GetTypeName(typeindex)
nico@207: 00120             typeinfos = GetValidTypeInfos(typename)
nico@207: 00121             internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num)
nico@207: 00122             minvalue = str(Manager.GetCurrentEntry(index, 2))
nico@207: 00123             maxvalue = str(Manager.GetCurrentEntry(index, 3))
nico@207: 00124             strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],minvalue,maxvalue)
nico@207: 00125             strSwitch += """    case valueRange_%d:
nico@207: 00126       if (*(%s*)Value < (%s)%s) return OD_VALUE_TOO_LOW;
nico@207: 00127       if (*(%s*)Value > (%s)%s) return OD_VALUE_TOO_HIGH;
nico@207: 00128       break;\n"""%(num,typeinfos[0],typeinfos[0],minvalue,typeinfos[0],typeinfos[0],maxvalue)
nico@207: 00129 
nico@207: 00130     valueRangeContent += strDefine
nico@207: 00131     valueRangeContent += "\nUNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value)\n{"%texts
nico@207: 00132     valueRangeContent += "\n  switch (typeValue) {\n"
nico@207: 00133     valueRangeContent += strSwitch
nico@207: 00134     valueRangeContent += "  }\n  return 0;\n}\n"
nico@207: 00135 
nico@207: 00136 #-------------------------------------------------------------------------------
nico@207: 00137 #            Creation of the mapped variables and object dictionary
nico@207: 00138 #-------------------------------------------------------------------------------
nico@207: 00139 
nico@207: 00140     mappedVariableContent = ""
nico@207: 00141     strDeclareHeader = ""
nico@207: 00142     strDeclareCallback = ""
nico@207: 00143     indexContents = {}
nico@207: 00144     indexCallbacks = {}
nico@207: 00145     for index in listIndex:
nico@207: 00146         texts["index"] = index
nico@207: 00147         strIndex = ""
nico@207: 00148         entry_infos = Manager.GetEntryInfos(index)
nico@207: 00149         texts["EntryName"] = entry_infos["name"]
nico@207: 00150         values = Manager.GetCurrentEntry(index)
nico@207: 00151         callbacks = Manager.HasCurrentEntryCallbacks(index)
nico@207: 00152         if index in variablelist:
nico@207: 00153             strIndex += "\n/* index 0x%(index)04X :   Mapped variable %(EntryName)s */\n"%texts
nico@207: 00154         else:
nico@207: 00155             strIndex += "\n/* index 0x%(index)04X :   %(EntryName)s. */\n"%texts
nico@207: 00156         
nico@207: 00157         # Entry type is VAR
nico@207: 00158         if type(values) != ListType:
nico@207: 00159             subentry_infos = Manager.GetSubentryInfos(index, 0)
nico@207: 00160             typename = Manager.GetTypeName(subentry_infos["type"])
nico@207: 00161             typeinfos = GetValidTypeInfos(typename)
nico@207: 00162             texts["subIndexType"] = typeinfos[0]
nico@207: 00163             texts["suffixe"] = typeinfos[1]
nico@207: 00164             if typeinfos[2] == "visible_string":
nico@207: 00165                 texts["value"] = "\"%s\""%values
nico@207: 00166                 texts["comment"] = ""
nico@207: 00167             else:
nico@207: 00168                 texts["value"] = "0x%X"%values
nico@207: 00169                 texts["comment"] = "\t/* %s */"%str(values)
nico@207: 00170             if index in variablelist:
nico@207: 00171                 texts["name"] = FormatName(subentry_infos["name"])
nico@207: 00172                 strDeclareHeader += "extern %(subIndexType)s %(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00*/\n"%texts
nico@207: 00173                 if callbacks:
nico@207: 00174                     strDeclareHeader += "extern ODCallback_t %(name)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts
nico@207: 00175                 mappedVariableContent += "%(subIndexType)s %(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00 */\n"%texts
nico@207: 00176             else:
nico@207: 00177                 strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts
nico@207: 00178             values = [values]
nico@207: 00179         else:
nico@207: 00180             subentry_infos = Manager.GetSubentryInfos(index, 0)
nico@207: 00181             typename = Manager.GetTypeName(subentry_infos["type"])
nico@207: 00182             typeinfos = GetValidTypeInfos(typename)
nico@207: 00183             texts["value"] = values[0]
nico@207: 00184             texts["subIndexType"] = typeinfos[0]
nico@207: 00185             strIndex += "                    %(subIndexType)s %(NodeName)s_highestSubIndex_obj%(index)04X = %(value)d; /* number of subindex - 1*/\n"%texts
nico@207: 00186             
nico@207: 00187             # Entry type is RECORD
nico@207: 00188             if entry_infos["struct"] & OD_IdenticalSubindexes:
nico@207: 00189                 subentry_infos = Manager.GetSubentryInfos(index, 1)
nico@207: 00190                 typename = Manager.GetTypeName(subentry_infos["type"])
nico@207: 00191                 typeinfos = GetValidTypeInfos(typename)
nico@207: 00192                 texts["subIndexType"] = typeinfos[0]
nico@207: 00193                 texts["suffixe"] = typeinfos[1]
nico@207: 00194                 texts["length"] = values[0]
nico@207: 00195                 if index in variablelist:
nico@207: 00196                     texts["name"] = FormatName(entry_infos["name"])
nico@207: 00197                     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
nico@207: 00198                     if callbacks:
nico@207: 00199                         strDeclareHeader += "extern ODCallback_t %(name)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts
nico@207: 00200                     mappedVariableContent += "%(subIndexType)s %(name)s[] =\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n  {\n"%texts
nico@207: 00201                     for subIndex, value in enumerate(values):
nico@207: 00202                         sep = ","
nico@207: 00203                         comment = ""
nico@207: 00204                         if subIndex > 0:
nico@207: 00205                             if subIndex == len(values)-1:
nico@207: 00206                                 sep = ""
nico@207: 00207                             if typeinfos[2] == "visible_string":
nico@207: 00208                                 value = "\"%s\""%value
nico@207: 00209                             else:
nico@207: 00210                                 comment = "\t/* %s */"%str(value)
nico@207: 00211                                 value = "0x%X"%value
nico@207: 00212                             mappedVariableContent += "    %s%s%s\n"%(value, sep, comment)
nico@207: 00213                     mappedVariableContent += "  };\n"
nico@207: 00214                 else:
nico@207: 00215                     strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X[] = \n                    {\n"%texts
nico@207: 00216                     for subIndex, value in enumerate(values):
nico@207: 00217                         sep = ","
nico@207: 00218                         comment = ""
nico@207: 00219                         if subIndex > 0:
nico@207: 00220                             if subIndex == len(values)-1:
nico@207: 00221                                 sep = ""
nico@207: 00222                             if typeinfos[2] == "visible_string":
nico@207: 00223                                 value = "\"%s\""%value
nico@207: 00224                             elif typeinfos[2] == "domain":
nico@207: 00225                                 value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
nico@207: 00226                             else:
nico@207: 00227                                 comment = "\t/* %s */"%str(value)
nico@207: 00228                                 value = "0x%X"%value
nico@207: 00229                             strIndex += "                      %s%s%s\n"%(value, sep, comment)
nico@207: 00230                     strIndex += "                    };\n"
nico@207: 00231             else:
nico@207: 00232                 
nico@207: 00233                 texts["parent"] = FormatName(entry_infos["name"])
nico@207: 00234                 # Entry type is ARRAY
nico@207: 00235                 for subIndex, value in enumerate(values):
nico@207: 00236                     texts["subIndex"] = subIndex
nico@207: 00237                     if subIndex > 0:
nico@207: 00238                         subentry_infos = Manager.GetSubentryInfos(index, subIndex)
nico@207: 00239                         typename = Manager.GetTypeName(subentry_infos["type"])
nico@207: 00240                         typeinfos = GetValidTypeInfos(typename)
nico@207: 00241                         texts["subIndexType"] = typeinfos[0]
nico@207: 00242                         texts["suffixe"] = typeinfos[1]
nico@207: 00243                         if typeinfos[2] == "visible_string":
nico@207: 00244                             texts["value"] = "\"%s\""%value
nico@207: 00245                             texts["comment"] = ""
nico@207: 00246                         else:
nico@207: 00247                             texts["value"] = "0x%X"%value
nico@207: 00248                             texts["comment"] = "\t/* %s */"%str(value)
nico@207: 00249                         texts["name"] = FormatName(subentry_infos["name"])
nico@207: 00250                         if index in variablelist:
nico@207: 00251                             strDeclareHeader += "extern %(subIndexType)s %(parent)s_%(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x%(subIndex)02X */\n"%texts
nico@207: 00252                             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
nico@207: 00253                         else:
nico@207: 00254                             strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X_%(name)s%(suffixe)s = %(value)s;%(comment)s\n"%texts
nico@207: 00255                 if callbacks:
nico@207: 00256                     strDeclareHeader += "extern ODCallback_t %(parent)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts
nico@207: 00257         
nico@207: 00258         # Generating Dictionary C++ entry
nico@207: 00259         if callbacks:
nico@207: 00260             if index in variablelist:
nico@207: 00261                 name = FormatName(entry_infos["name"])
nico@207: 00262             else:
nico@207: 00263                 name = "%(NodeName)s_Index%(index)04X"%texts
nico@207: 00264             strIndex += "                    ODCallback_t %s_callbacks[] = \n                     {\n"%name
nico@207: 00265             for subIndex in xrange(len(values)):
nico@207: 00266                 strIndex += "                       NULL,\n"
nico@207: 00267             strIndex += "                     };\n"
nico@207: 00268             indexCallbacks[index] = "*callbacks = %s_callbacks; "%name
nico@207: 00269         else:
nico@207: 00270             indexCallbacks[index] = ""
nico@207: 00271         strIndex += "                    subindex %(NodeName)s_Index%(index)04X[] = \n                     {\n"%texts
nico@207: 00272         for subIndex in xrange(len(values)):
nico@207: 00273             subentry_infos = Manager.GetSubentryInfos(index, subIndex)
nico@207: 00274             if subIndex < len(values) - 1:
nico@207: 00275                 sep = ","
nico@207: 00276             else:
nico@207: 00277                 sep = ""
nico@207: 00278             typename = Manager.GetTypeName(subentry_infos["type"])
nico@207: 00279             typeinfos = GetValidTypeInfos(typename)
nico@207: 00280             if subIndex == 0:
nico@207: 00281                 if entry_infos["struct"] & OD_MultipleSubindexes:
nico@207: 00282                     name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts
nico@207: 00283                 elif index in variablelist:
nico@207: 00284                     name = FormatName(subentry_infos["name"])
nico@207: 00285                 else:
nico@207: 00286                     name = FormatName("%s_obj%04X"%(texts["NodeName"], texts["index"]))
nico@207: 00287             elif entry_infos["struct"] & OD_IdenticalSubindexes:
nico@207: 00288                 if index in variablelist:
nico@207: 00289                     name = "%s[%d]"%(FormatName(entry_infos["name"]), subIndex - 1)
nico@207: 00290                 else:
nico@207: 00291                     name = "%s_obj%04X[%d]"%(texts["NodeName"], texts["index"], subIndex - 1)
nico@207: 00292             else:
nico@207: 00293                 if index in variablelist:
nico@207: 00294                     name = FormatName("%s_%s"%(entry_infos["name"],subentry_infos["name"]))
nico@207: 00295                 else:
nico@207: 00296                     name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"]))
nico@207: 00297             if typeinfos[2] in ["visible_string", "domain"]:
nico@207: 00298                 sizeof = str(len(values[subIndex]))
nico@207: 00299             else:
nico@207: 00300                 sizeof = "sizeof (%s)"%typeinfos[0]
nico@207: 00301             params = Manager.GetCurrentParamsEntry(index, subIndex)
nico@207: 00302             if params["save"]:
nico@207: 00303                 save = "|TO_BE_SAVE"
nico@207: 00304             else:
nico@207: 00305                 save = ""
nico@207: 00306             strIndex += "                       { %s%s, %s, %s, (void*)&%s }%s\n"%(subentry_infos["access"].upper(),save,typeinfos[2],sizeof,name,sep)
nico@207: 00307         strIndex += "                     };\n"
nico@207: 00308         indexContents[index] = strIndex
nico@207: 00309 
nico@207: 00310 #-------------------------------------------------------------------------------
nico@207: 00311 #                     Declaration of Particular Parameters
nico@207: 00312 #-------------------------------------------------------------------------------
nico@207: 00313 
nico@207: 00314     if 0x1006 not in communicationlist:
nico@207: 00315         entry_infos = Manager.GetEntryInfos(0x1006)
nico@207: 00316         texts["EntryName"] = entry_infos["name"]
nico@207: 00317         indexContents[0x1006] = """\n/* index 0x1006 :   %(EntryName)s */
nico@207: 00318                     UNS32 %(NodeName)s_obj1006 = 0x0;   /* 0 */
nico@207: 00319 """%texts
nico@207: 00320 
nico@207: 00321     if 0x1016 in communicationlist:
nico@207: 00322         texts["nombre"] = Manager.GetCurrentEntry(0x1016, 0)
nico@207: 00323     else:
nico@207: 00324         texts["nombre"] = 0
nico@207: 00325         entry_infos = Manager.GetEntryInfos(0x1016)
nico@207: 00326         texts["EntryName"] = entry_infos["name"]
nico@207: 00327         indexContents[0x1016] = """\n/* index 0x1016 :   %(EntryName)s */
nico@207: 00328                     UNS8 %(NodeName)s_highestSubIndex_obj1016 = 0;
nico@207: 00329                     UNS32 %(NodeName)s_obj1016[]={0};
nico@207: 00330 """%texts
nico@207: 00331     if texts["nombre"] > 0:
nico@207: 00332         strTimers = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[%(nombre)d] = {TIMER_NONE,};\n"%texts
nico@207: 00333     else:
nico@207: 00334         strTimers = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[1];\n"%texts
nico@207: 00335 
nico@207: 00336     if 0x1017 not in communicationlist:
nico@207: 00337         entry_infos = Manager.GetEntryInfos(0x1017)
nico@207: 00338         texts["EntryName"] = entry_infos["name"]
nico@207: 00339         indexContents[0x1017] = """\n/* index 0x1017 :   %(EntryName)s */ 
nico@207: 00340                     UNS16 %(NodeName)s_obj1017 = 0x0;   /* 0 */
nico@207: 00341 """%texts
nico@207: 00342 
nico@207: 00343 #-------------------------------------------------------------------------------
nico@207: 00344 #               Declaration of navigation in the Object Dictionary
nico@207: 00345 #-------------------------------------------------------------------------------
nico@207: 00346 
nico@207: 00347     strDeclareIndex = ""
nico@207: 00348     strDeclareSwitch = ""
nico@207: 00349     strQuickIndex = ""
nico@207: 00350     quick_index = {}
nico@207: 00351     for index_cat in index_categories:
nico@207: 00352         quick_index[index_cat] = {}
nico@207: 00353         for cat, idx_min, idx_max in categories:
nico@207: 00354             quick_index[index_cat][cat] = 0
nico@207: 00355     maxPDOtransmit = 0
nico@207: 00356     for i, index in enumerate(listIndex):
nico@207: 00357         texts["index"] = index
nico@207: 00358         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
nico@207: 00359         strDeclareSwitch += "           case 0x%04X: i = %d;%sbreak;\n"%(index, i, indexCallbacks[index])
nico@207: 00360         for cat, idx_min, idx_max in categories:
nico@207: 00361             if idx_min <= index <= idx_max:
nico@207: 00362                 quick_index["lastIndex"][cat] = i
nico@207: 00363                 if quick_index["firstIndex"][cat] == 0:
nico@207: 00364                     quick_index["firstIndex"][cat] = i
nico@207: 00365                 if cat == "PDO_TRS":
nico@207: 00366                     maxPDOtransmit += 1
nico@207: 00367     texts["maxPDOtransmit"] = max(1, maxPDOtransmit)
nico@207: 00368     for index_cat in index_categories:
nico@207: 00369         strQuickIndex += "\nquick_index %s_%s = {\n"%(texts["NodeName"], index_cat)
nico@207: 00370         sep = ","
nico@207: 00371         for i, (cat, idx_min, idx_max) in enumerate(categories):
nico@207: 00372             if i == len(categories) - 1:
nico@207: 00373                 sep = ""
nico@207: 00374             strQuickIndex += "  %d%s /* %s */\n"%(quick_index[index_cat][cat],sep,cat)
nico@207: 00375         strQuickIndex += "};\n"
nico@207: 00376 
nico@207: 00377 #-------------------------------------------------------------------------------
nico@207: 00378 #                            Write File Content
nico@207: 00379 #-------------------------------------------------------------------------------
nico@207: 00380 
nico@207: 00381     fileContent = generated_tag + """
nico@207: 00382 #include "%s"
nico@207: 00383 """%(headerfilepath)
nico@207: 00384 
nico@207: 00385     fileContent += """
nico@207: 00386 /**************************************************************************/
nico@207: 00387 /* Declaration of the mapped variables                                    */
nico@207: 00388 /**************************************************************************/
nico@207: 00389 """ + mappedVariableContent
nico@207: 00390 
nico@207: 00391     fileContent += """
nico@207: 00392 /**************************************************************************/
nico@207: 00393 /* Declaration of the value range types                                   */
nico@207: 00394 /**************************************************************************/
nico@207: 00395 """ + valueRangeContent
nico@207: 00396 
nico@207: 00397     fileContent += """
nico@207: 00398 /**************************************************************************/
nico@207: 00399 /* The node id                                                            */
nico@207: 00400 /**************************************************************************/
nico@207: 00401 /* node_id default value.*/
nico@207: 00402 UNS8 %(NodeName)s_bDeviceNodeId = 0x%(NodeID)02X;
nico@207: 00403 
nico@207: 00404 /**************************************************************************/
nico@207: 00405 /* Array of message processing information */
nico@207: 00406 
nico@207: 00407 const UNS8 %(NodeName)s_iam_a_slave = %(iam_a_slave)d;
nico@207: 00408 
nico@207: 00409 """%texts
nico@207: 00410     fileContent += strTimers
nico@207: 00411     
nico@207: 00412     fileContent += """
nico@207: 00413 /*
nico@207: 00414 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
nico@207: 00415 
nico@207: 00416                                OBJECT DICTIONARY
nico@207: 00417 
nico@207: 00418 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
nico@207: 00419 */
nico@207: 00420 """%texts
nico@207: 00421     contentlist = indexContents.keys()
nico@207: 00422     contentlist.sort()
nico@207: 00423     for index in contentlist:
nico@207: 00424         fileContent += indexContents[index]
nico@207: 00425 
nico@207: 00426     fileContent += """
nico@207: 00427 const indextable %(NodeName)s_objdict[] = 
nico@207: 00428 {
nico@207: 00429 """%texts
nico@207: 00430     fileContent += strDeclareIndex
nico@207: 00431     fileContent += """};
nico@207: 00432 
nico@207: 00433 const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks)
nico@207: 00434 {
nico@207: 00435         int i;
nico@207: 00436         *callbacks = NULL;
nico@207: 00437         switch(wIndex){
nico@207: 00438 """%texts
nico@207: 00439     fileContent += strDeclareSwitch
nico@207: 00440     fileContent += """          default:
nico@207: 00441                         *errorCode = OD_NO_SUCH_OBJECT;
nico@207: 00442                         return NULL;
nico@207: 00443         }
nico@207: 00444         *errorCode = OD_SUCCESSFUL;
nico@207: 00445         return &%(NodeName)s_objdict[i];
nico@207: 00446 }
nico@207: 00447 
nico@207: 00448 /* To count at which received SYNC a PDO must be sent.
nico@207: 00449  * Even if no pdoTransmit are defined, at least one entry is computed
nico@207: 00450  * for compilations issues.
nico@207: 00451  */
nico@207: 00452 UNS8 %(NodeName)s_count_sync[%(maxPDOtransmit)d] = {0,};
nico@207: 00453 """%texts
nico@207: 00454     fileContent += strQuickIndex
nico@207: 00455     fileContent += """
nico@207: 00456 UNS16 %(NodeName)s_ObjdictSize = sizeof(%(NodeName)s_objdict)/sizeof(%(NodeName)s_objdict[0]); 
nico@207: 00457 
nico@207: 00458 CO_Data %(NodeName)s_Data = CANOPEN_NODE_DATA_INITIALIZER(%(NodeName)s);
nico@207: 00459 
nico@207: 00460 """%texts
nico@207: 00461 
nico@207: 00462 #-------------------------------------------------------------------------------
nico@207: 00463 #                          Write Header File Content
nico@207: 00464 #-------------------------------------------------------------------------------
nico@207: 00465 
nico@207: 00466     HeaderFileContent = generated_tag + """
nico@207: 00467 #include "data.h"
nico@207: 00468 
nico@207: 00469 /* Prototypes of function provided by object dictionnary */
nico@207: 00470 UNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value);
nico@207: 00471 const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks);
nico@207: 00472 
nico@207: 00473 /* Master node data struct */
nico@207: 00474 extern CO_Data %(NodeName)s_Data;
nico@207: 00475 
nico@207: 00476 """%texts
nico@207: 00477     HeaderFileContent += strDeclareHeader
nico@207: 00478     
nico@207: 00479     return fileContent,HeaderFileContent
nico@207: 00480 
nico@207: 00481 #-------------------------------------------------------------------------------
nico@207: 00482 #                             Main Function
nico@207: 00483 #-------------------------------------------------------------------------------
nico@207: 00484 
nico@207: 00485 def GenerateFile(filepath, manager):
nico@207: 00486     try:
nico@207: 00487         headerfilepath = os.path.splitext(filepath)[0]+".h"
nico@207: 00488         content, header = GenerateFileContent(manager, os.path.split(headerfilepath)[1])
nico@207: 00489         WriteFile(filepath, content)
nico@207: 00490         WriteFile(headerfilepath, header)
nico@207: 00491         return None
nico@207: 00492     except ValueError, message:
nico@207: 00493         return "Unable to Generate C File\n%s"%message
nico@207: 00494 
nico@207: 

Generated on Mon Jun 4 16:29:06 2007 for CanFestival by  nico@207: nico@207: doxygen 1.5.1
nico@207: nico@207: