Bugs on C File generating corrected
authorlbessard
Fri, 11 May 2007 15:24:43 +0200
changeset 188 00245bc2e6fe
parent 187 d3930d3f1323
child 189 1c1d1893f1c9
Bugs on C File generating corrected
objdictgen/gen_cfile.py
objdictgen/nodemanager.py
--- a/objdictgen/gen_cfile.py	Fri May 11 15:16:45 2007 +0200
+++ b/objdictgen/gen_cfile.py	Fri May 11 15:24:43 2007 +0200
@@ -37,6 +37,8 @@
 
 generated_tag = """\n/* File generated by gen_cfile.py. Should not be modified. */\n"""
 
+internal_types = {}
+
 # Format a string for making a C++ variable
 def FormatName(name):
     wordlist = [word for word in word_model.findall(name) if word != '']
@@ -49,23 +51,33 @@
 
 # Extract the informations from a given type name
 def GetValidTypeInfos(typename):
-    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)]:
-            return "UNS%s"%values[1], "", "uint%s"%values[1]
-        if values[0] == "INTEGER" and int(values[1]) in [i * 8 for i in xrange(1, 9)]:
-            return "INTEGER%s"%values[1], "", "int%s"%values[1]
-        elif values[0] == "REAL" and int(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"
-            else:
-                return "UNS8", "[%s]"%values[1], "visible_string"
-        elif values[0] == "DOMAIN":
-        	return "UNS8*", "", "domain"
-    return None
+    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], "", "uint%s"%values[1])
+            elif values[0] == "INTEGER" and int(values[1]) in [i * 8 for i in xrange(1, 9)]:
+                typeinfos = ("INTEGER%s"%values[1], "", "int%s"%values[1])
+            elif values[0] == "REAL" and int(values[1]) in (32, 64):
+                typeinfos = ("%s%s"%(values[0], values[1]), "", "real%s"%values[1])
+            elif values[0] == "VISIBLE_STRING":
+                if values[1] == "":
+                    typeinfos = ("UNS8", "[10]", "visible_string")
+                else:
+                    typeinfos = ("UNS8", "[%s]"%values[1], "visible_string")
+            elif values[0] == "DOMAIN":
+                typeinfos = ("UNS8*", "", "domain")
+            elif values[0] == "BOOLEAN":
+                typeinfos = ("UNS8", "", "boolean")
+            else:
+                raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
+            internal_types[typename] = typeinfos
+        else:
+            raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
+    return typeinfos
 
 def WriteFile(filepath, content):
     cfile = open(filepath,"w")
@@ -74,10 +86,10 @@
 
 def GenerateFileContent(Manager, headerfilepath):
     global type
+    global internal_types
     texts = {}
     texts["maxPDOtransmit"] = 0
-    texts["NodeName"], texts["NodeID"], texts["NodeType"] = Manager.GetCurrentNodeInfos()
-    internal_types = {}
+    texts["NodeName"], texts["NodeID"], texts["NodeType"], texts["Description"] = Manager.GetCurrentNodeInfos()
     texts["iam_a_slave"] = 0
     if (texts["NodeType"] == "slave"):
         texts["iam_a_slave"] = 1
@@ -103,20 +115,17 @@
         result = range_model.match(rangename)
         if result:
             num += 1
-            internal_types[rangename] = "valueRange_%d"%num
             typeindex = Manager.GetCurrentEntry(index, 1)
             typename = Manager.GetTypeName(typeindex)
             typeinfos = GetValidTypeInfos(typename)
-            if typeinfos == None:
-                raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
-            typename = typeinfos[0]
+            internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num)
             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)
+            strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],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)
+      break;\n"""%(num,typeinfos[0],typeinfos[0],minvalue,typeinfos[0],typeinfos[0],maxvalue)
 
     valueRangeContent += strDefine
     valueRangeContent += "\nUNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value)\n{"%texts
@@ -144,19 +153,12 @@
             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]
             texts["subIndexType"] = typeinfos[0]
             texts["suffixe"] = typeinfos[1]
             if typeinfos[2] == "visible_string":
@@ -175,16 +177,18 @@
                 strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts
             values = [values]
         else:
+            subentry_infos = Manager.GetSubentryInfos(index, 0)
+            typename = Manager.GetTypeName(subentry_infos["type"])
+            typeinfos = GetValidTypeInfos(typename)
+            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]
                 texts["subIndexType"] = typeinfos[0]
                 texts["suffixe"] = typeinfos[1]
                 texts["length"] = values[0]
@@ -234,10 +238,6 @@
                         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]
                         texts["subIndexType"] = typeinfos[0]
                         texts["suffixe"] = typeinfos[1]
                         if typeinfos[2] == "visible_string":
@@ -277,12 +277,6 @@
                 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]
-            else:
-                subIndexType = typename
             if subIndex == 0:
                 if entry_infos["struct"] & OD_MultipleSubindexes:
                     name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts
@@ -300,7 +294,7 @@
                     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 in ["visible_string", "domain"]:
+            if typeinfos[2] in ["visible_string", "domain"]:
                 sizeof = str(len(values[subIndex]))
             else:
                 sizeof = "sizeof (%s)"%typeinfos[0]
@@ -309,7 +303,7 @@
                 save = "|TO_BE_SAVE"
             else:
                 save = ""
-            strIndex += "                       { %s%s, %s, %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)
         strIndex += "                     };\n"
         indexContents[index] = strIndex
 
@@ -489,8 +483,12 @@
 #-------------------------------------------------------------------------------
 
 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
+    try:
+        headerfilepath = os.path.splitext(filepath)[0]+".h"
+        content, header = GenerateFileContent(manager, os.path.split(headerfilepath)[1])
+        WriteFile(filepath, content)
+        WriteFile(headerfilepath, header)
+        return None
+    except ValueError, message:
+        return "Unable to Generate C File\n%s"%message
+
--- a/objdictgen/nodemanager.py	Fri May 11 15:16:45 2007 +0200
+++ b/objdictgen/nodemanager.py	Fri May 11 15:24:43 2007 +0200
@@ -47,7 +47,6 @@
 """
 Format the text given with the index and subindex defined
 """
-
 def StringFormat(text, idx, sub):
     result = name_model.match(text)
     if result:
@@ -794,7 +793,10 @@
                 elif editor == "time":
                     self.CurrentNode.SetEntry(index, subIndex, value)
                 elif editor == "number":
-                    self.CurrentNode.SetEntry(index, subIndex, value)
+                    try:
+                        self.CurrentNode.SetEntry(index, subIndex, int(value))
+                    except:
+                        pass
                 elif editor == "domain":
                     try:
                         if len(value) % 2 != 0: