LPCBeremiz.py
changeset 550 b03f586547c9
parent 549 5dd92bd6e6e5
child 551 d18e86b603bc
equal deleted inserted replaced
549:5dd92bd6e6e5 550:b03f586547c9
    95             children.extend(child["children"])
    95             children.extend(child["children"])
    96         else:
    96         else:
    97             children.append(child)
    97             children.append(child)
    98     return children
    98     return children
    99 
    99 
       
   100 def _GetVariables(module):
       
   101     variables = []
       
   102     for child in module["children"]:
       
   103         if child["type"] in [LOCATION_GROUP, LOCATION_MODULE]:
       
   104             variables.extend(_GetVariables(child))
       
   105         else:
       
   106             variables.append(child)
       
   107     return variables
       
   108 
   100 def _GetLastModuleGroup(module):
   109 def _GetLastModuleGroup(module):
   101     group = module
   110     group = module
   102     for child in module["children"]:
   111     for child in module["children"]:
   103         if child["type"] == LOCATION_GROUP:
   112         if child["type"] == LOCATION_GROUP:
   104             group = child
   113             group = child
   134   #include "iec_types.h"
   143   #include "iec_types.h"
   135 #else
   144 #else
   136   #include "iec_std_lib.h"
   145   #include "iec_std_lib.h"
   137 #endif
   146 #endif
   138 
   147 
   139 %(declare_code)s
       
   140 
       
   141 /* LPCBus plugin user variables definition */
   148 /* LPCBus plugin user variables definition */
   142 %(var_decl)s
   149 %(var_decl)s
   143 
   150 
   144 /* LPCBus plugin functions */
   151 /* LPCBus plugin functions */
   145 int __init_%(location_str)s(int argc,char **argv)
   152 int __init_%(location_str)s(int argc,char **argv)
   146 {
   153 {
       
   154 %(declare_code)s
   147   return 0;
   155   return 0;
   148 }
   156 }
   149 
   157 
   150 void __cleanup_%(location_str)s(void)
   158 void __cleanup_%(location_str)s(void)
   151 {
   159 {
   152 }
   160 }
   153 
   161 
   154 void __retrieve_%(location_str)s(void)
   162 void __retrieve_%(location_str)s(void)
   155 {
   163 {
   156   %(retrieve_code)s
   164 %(retrieve_code)s
   157 }
   165 }
   158         
   166         
   159 void __publish_%(location_str)s(void)
   167 void __publish_%(location_str)s(void)
   160 {
   168 {
   161   %(publish_code)s
   169 %(publish_code)s
   162 }
   170 }
   163 """
   171 """
   164 
   172 
   165 class LPCBus(object):
   173 class LPCBus(object):
   166     
   174     
   282                     "var_decl": "",
   290                     "var_decl": "",
   283                     "declare_code": "",
   291                     "declare_code": "",
   284                     "retrieve_code": "",
   292                     "retrieve_code": "",
   285                     "publish_code": "",
   293                     "publish_code": "",
   286                    }
   294                    }
       
   295         
       
   296         for variable in _GetVariables(self):
       
   297             if variable["declare"] != "":
       
   298                 code_str["declare_code"] += "  %s\n" % variable["declare"]
   287         
   299         
   288         # Adding variables
   300         # Adding variables
   289         vars = []
   301         vars = []
   290         self.ResetUsedLocations()
   302         self.ResetUsedLocations()
   291         for location in locations:
   303         for location in locations:
   308                         if self.CheckLocationConflicts(location["LOC"]):
   320                         if self.CheckLocationConflicts(location["LOC"]):
   309                             raise Exception, "BYTE and BIT from the same BYTE can't be used together"
   321                             raise Exception, "BYTE and BIT from the same BYTE can't be used together"
   310                         self.AddUsedLocation(location["LOC"])
   322                         self.AddUsedLocation(location["LOC"])
   311                     vars.append({"location": location["NAME"],
   323                     vars.append({"location": location["NAME"],
   312                                  "Type": variable["IEC_type"],
   324                                  "Type": variable["IEC_type"],
   313                                  "Declare": variable["declare"],
       
   314                                  "Retrieve": variable["retrieve"],
   325                                  "Retrieve": variable["retrieve"],
   315                                  "Publish": variable["publish"],
   326                                  "Publish": variable["publish"],
   316                                 })
   327                                 })
   317                     break
   328                     break
   318         base_types = self.GetPlugRoot().GetBaseTypes()
   329         base_types = self.GetPlugRoot().GetBaseTypes()
   320             prefix = ""
   331             prefix = ""
   321             if var["Type"] in base_types:
   332             if var["Type"] in base_types:
   322                 prefix = "IEC_"
   333                 prefix = "IEC_"
   323             code_str["var_decl"] += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"])
   334             code_str["var_decl"] += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"])
   324             code_str["var_decl"] += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"])
   335             code_str["var_decl"] += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"])
   325             if var["Declare"] != "":
       
   326                 code_str["declare_code"] += var["Declare"] + "\n"
       
   327             if var["Retrieve"] != "":
   336             if var["Retrieve"] != "":
   328                 code_str["retrieve_code"] += var["Retrieve"] % ("*" + var["location"]) + "\n"
   337                 code_str["retrieve_code"] += "  " + var["Retrieve"] % ("*" + var["location"]) + "\n"
   329             if var["Publish"] != "":
   338             if var["Publish"] != "":
   330                 code_str["publish_code"] += var["Publish"] % ("*" + var["location"]) + "\n"
   339                 code_str["publish_code"] += "  " + var["Publish"] % ("*" + var["location"]) + "\n"
   331         
   340         
   332         Gen_Module_path = os.path.join(buildpath, "Module_%s.c"%location_str)
   341         Gen_Module_path = os.path.join(buildpath, "Module_%s.c"%location_str)
   333         module = open(Gen_Module_path,'w')
   342         module = open(Gen_Module_path,'w')
   334         module.write(BUS_TEXT % code_str)
   343         module.write(BUS_TEXT % code_str)
   335         module.close()
   344         module.close()