etisserant@31: import wx lbessard@145: import os laurent@401: from xml.dom import minidom laurent@401: import cPickle laurent@401: laurent@401: from xmlclass import * laurent@401: laurent@630: from plugger import PlugTemplate, opjimg lbessard@145: from CFileEditor import CFileEditor laurent@630: from PLCControler import PLCControler, UndoBuffer, LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY laurent@630: laurent@630: CFileClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "cext_xsd.xsd")) lbessard@145: lbessard@145: TYPECONVERSION = {"BOOL" : "X", "SINT" : "B", "INT" : "W", "DINT" : "D", "LINT" : "L", lbessard@145: "USINT" : "B", "UINT" : "W", "UDINT" : "D", "ULINT" : "L", "REAL" : "D", "LREAL" : "L", lbessard@145: "STRING" : "B", "BYTE" : "B", "WORD" : "W", "DWORD" : "D", "LWORD" : "L", "WSTRING" : "W"} etisserant@31: etisserant@31: class _Cfile: etisserant@31: XSD = """ etisserant@31: lbessard@145: etisserant@31: etisserant@45: etisserant@45: etisserant@31: etisserant@31: etisserant@31: etisserant@31: """ laurent@656: EditorType = CFileEditor laurent@656: etisserant@31: def __init__(self): lbessard@145: filepath = self.CFileName() lbessard@145: laurent@656: self._View = None laurent@656: lbessard@145: self.Buffering = False lbessard@145: self.CFile = CFileClasses["CFile"]() lbessard@145: if os.path.isfile(filepath): lbessard@145: xmlfile = open(filepath, 'r') lbessard@145: tree = minidom.parse(xmlfile) lbessard@145: xmlfile.close() lbessard@145: lbessard@145: for child in tree.childNodes: lbessard@145: if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "CFile": lbessard@145: self.CFile.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) lbessard@145: self.CFileBuffer = UndoBuffer(self.Copy(self.CFile), True) lbessard@145: else: laurent@651: self.CFileBuffer = UndoBuffer(self.Copy(self.CFile), False) lbessard@145: self.OnPlugSave() lbessard@145: lbessard@145: def CFileName(self): lbessard@145: return os.path.join(self.PlugPath(), "cfile.xml") lbessard@145: lbessard@145: def GetFilename(self): laurent@630: return self.MandatoryParams[1].getName() lbessard@145: lbessard@145: def GetBaseTypes(self): lbessard@145: return self.GetPlugRoot().GetBaseTypes() lbessard@145: laurent@610: def GetDataTypes(self, basetypes = False, only_locatables = False): laurent@610: return self.GetPlugRoot().GetDataTypes(basetypes=basetypes, only_locatables=only_locatables) lbessard@145: lbessard@145: def GetSizeOfType(self, type): laurent@610: return TYPECONVERSION.get(self.GetPlugRoot().GetBaseType(type), None) lbessard@145: lbessard@145: def SetVariables(self, variables): lbessard@145: self.CFile.variables.setvariable([]) lbessard@145: for var in variables: lbessard@145: variable = CFileClasses["variables_variable"]() lbessard@145: variable.setname(var["Name"]) lbessard@145: variable.settype(var["Type"]) lbessard@145: variable.setclass(var["Class"]) lbessard@145: self.CFile.variables.appendvariable(variable) lbessard@145: lbessard@145: def GetVariables(self): lbessard@145: datas = [] lbessard@145: for var in self.CFile.variables.getvariable(): lbessard@145: datas.append({"Name" : var.getname(), "Type" : var.gettype(), "Class" : var.getclass()}) lbessard@145: return datas lbessard@145: laurent@401: def GetVariableLocationTree(self): laurent@401: '''See PlugTemplate.GetVariableLocationTree() for a description.''' laurent@401: laurent@401: current_location = ".".join(map(str, self.GetCurrentLocation())) laurent@401: laurent@401: vars = [] Edouard@603: input = memory = output = 0 laurent@401: for var in self.CFile.variables.getvariable(): laurent@401: var_size = self.GetSizeOfType(var.gettype()) laurent@610: var_location = "" laurent@401: if var.getclass() == "input": laurent@401: var_class = LOCATION_VAR_INPUT laurent@610: if var_size is not None: laurent@610: var_location = "%%I%s%s.%d"%(var_size, current_location, input) laurent@401: input += 1 Edouard@603: elif var.getclass() == "memory": Edouard@603: var_class = LOCATION_VAR_INPUT laurent@610: if var_size is not None: laurent@610: var_location = "%%M%s%s.%d"%(var_size, current_location, memory) Edouard@603: memory += 1 laurent@401: else: laurent@401: var_class = LOCATION_VAR_OUTPUT laurent@610: if var_size is not None: laurent@610: var_location = "%%Q%s%s.%d"%(var_size, current_location, output) laurent@401: output += 1 laurent@401: vars.append({"name": var.getname(), laurent@401: "type": var_class, laurent@401: "size": var_size, laurent@401: "IEC_type": var.gettype(), laurent@401: "location": var_location, laurent@401: "description": "", laurent@401: "children": []}) laurent@401: laurent@402: return {"name": self.BaseParams.getName(), laurent@402: "type": LOCATION_PLUGIN, laurent@402: "location": self.GetFullIEC_Channel(), laurent@402: "children": vars} laurent@401: lbessard@145: def SetPartText(self, name, text): lbessard@145: if name == "Includes": lbessard@145: self.CFile.includes.settext(text) lbessard@145: elif name == "Globals": lbessard@145: self.CFile.globals.settext(text) lbessard@145: elif name == "Init": lbessard@145: self.CFile.initFunction.settext(text) lbessard@145: elif name == "CleanUp": lbessard@145: self.CFile.cleanUpFunction.settext(text) lbessard@145: elif name == "Retrieve": lbessard@145: self.CFile.retrieveFunction.settext(text) lbessard@145: elif name == "Publish": lbessard@145: self.CFile.publishFunction.settext(text) lbessard@145: lbessard@145: def GetPartText(self, name): lbessard@145: if name == "Includes": lbessard@145: return self.CFile.includes.gettext() lbessard@145: elif name == "Globals": lbessard@145: return self.CFile.globals.gettext() lbessard@145: elif name == "Init": lbessard@145: return self.CFile.initFunction.gettext() lbessard@145: elif name == "CleanUp": lbessard@145: return self.CFile.cleanUpFunction.gettext() lbessard@145: elif name == "Retrieve": lbessard@145: return self.CFile.retrieveFunction.gettext() lbessard@145: elif name == "Publish": lbessard@145: return self.CFile.publishFunction.gettext() lbessard@145: return "" laurent@630: lbessard@65: PluginMethods = [ etisserant@199: {"bitmap" : os.path.join("images", "EditCfile"), laurent@361: "name" : _("Edit C File"), laurent@361: "tooltip" : _("Edit C File"), etisserant@105: "method" : "_OpenView"}, lbessard@65: ] etisserant@45: laurent@630: def PlugTestModified(self): laurent@630: return self.ChangesToSave or not self.CFileIsSaved() laurent@630: etisserant@31: def OnPlugSave(self): lbessard@145: filepath = self.CFileName() lbessard@145: lbessard@145: text = "\n" lbessard@145: extras = {"xmlns":"http://www.w3.org/2001/XMLSchema", lbessard@145: "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", lbessard@145: "xsi:schemaLocation" : "cext_xsd.xsd"} lbessard@145: text += self.CFile.generateXMLText("CFile", 0, extras) lbessard@145: lbessard@145: xmlfile = open(filepath,"w") laurent@430: xmlfile.write(text.encode("utf-8")) lbessard@145: xmlfile.close() lbessard@145: laurent@651: self.MarkCFileAsSaved() etisserant@31: return True etisserant@31: etisserant@203: def PlugGenerate_C(self, buildpath, locations): etisserant@31: """ etisserant@31: Generate C code etisserant@31: @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5) etisserant@31: @param locations: List of complete variables locations \ etisserant@31: [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) etisserant@31: "NAME" : name of the variable (generally "__IW0_1_2" style) etisserant@31: "DIR" : direction "Q","I" or "M" etisserant@31: "SIZE" : size "X", "B", "W", "D", "L" etisserant@31: "LOC" : tuple of interger for IEC location (0,1,2,...) etisserant@31: }, ...] etisserant@31: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND etisserant@31: """ etisserant@31: current_location = self.GetCurrentLocation() etisserant@31: # define a unique name for the generated C file laurent@401: location_str = "_".join(map(str, current_location)) lbessard@145: lbessard@145: text = "/* Code generated by Beremiz c_ext plugin */\n\n" lbessard@145: lbessard@145: # Adding includes lbessard@145: text += "/* User includes */\n" lbessard@145: text += self.CFile.includes.gettext() lbessard@145: text += "\n" lbessard@145: lbessard@145: text += """/* Beremiz c_ext plugin includes */ lbessard@145: #ifdef _WINDOWS_H lbessard@145: #include "iec_types.h" lbessard@145: #else lbessard@145: #include "iec_std_lib.h" lbessard@145: #endif lbessard@145: lbessard@145: """ lbessard@145: lbessard@145: # Adding variables lbessard@145: vars = [] Edouard@603: inputs = memories = outputs = 0 lbessard@145: for variable in self.CFile.variables.variable: lbessard@145: var = {"Name" : variable.getname(), "Type" : variable.gettype()} lbessard@145: if variable.getclass() == "input": lbessard@145: var["location"] = "__I%s%s_%d"%(self.GetSizeOfType(var["Type"]), location_str, inputs) lbessard@145: inputs += 1 Edouard@603: elif variable.getclass() == "memory": Edouard@603: var["location"] = "__M%s%s_%d"%(self.GetSizeOfType(var["Type"]), location_str, memories) Edouard@603: memories += 1 lbessard@145: else: lbessard@145: var["location"] = "__Q%s%s_%d"%(self.GetSizeOfType(var["Type"]), location_str, outputs) lbessard@145: outputs += 1 lbessard@145: vars.append(var) lbessard@145: text += "/* Beremiz c_ext plugin user variables definition */\n" lbessard@145: base_types = self.GetPlugRoot().GetBaseTypes() lbessard@145: for var in vars: lbessard@145: if var["Type"] in base_types: lbessard@145: prefix = "IEC_" lbessard@145: else: lbessard@145: prefix = "" etisserant@180: text += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"]) etisserant@180: text += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"]) lbessard@145: text += "/* User variables reference */\n" lbessard@145: for var in vars: etisserant@180: text += "#define %s beremiz%s\n"%(var["Name"], var["location"]) lbessard@145: text += "\n" lbessard@145: lbessard@145: # Adding user global variables and routines lbessard@145: text += "/* User internal user variables and routines */\n" lbessard@145: text += self.CFile.globals.gettext() lbessard@145: lbessard@145: # Adding Beremiz plugin functions lbessard@145: text += "/* Beremiz plugin functions */\n" lbessard@145: text += "int __init_%s(int argc,char **argv)\n{\n"%location_str lbessard@145: text += self.CFile.initFunction.gettext() etisserant@180: text += " return 0;\n" lbessard@145: text += "\n}\n\n" lbessard@145: laurent@419: text += "void __cleanup_%s(void)\n{\n"%location_str lbessard@145: text += self.CFile.cleanUpFunction.gettext() lbessard@145: text += "\n}\n\n" lbessard@145: laurent@419: text += "void __retrieve_%s(void)\n{\n"%location_str lbessard@145: text += self.CFile.retrieveFunction.gettext() lbessard@145: text += "\n}\n\n" lbessard@145: laurent@419: text += "void __publish_%s(void)\n{\n"%location_str lbessard@145: text += self.CFile.publishFunction.gettext() lbessard@145: text += "\n}\n\n" lbessard@145: lbessard@145: Gen_Cfile_path = os.path.join(buildpath, "CFile_%s.c"%location_str) lbessard@145: cfile = open(Gen_Cfile_path,'w') lbessard@145: cfile.write(text) lbessard@145: cfile.close() lbessard@145: greg@418: matiec_flags = '"-I%s"'%os.path.abspath(self.GetPlugRoot().GetIECLibPath()) lbessard@145: lbessard@145: return [(Gen_Cfile_path, str(self.CExtension.getCFLAGS() + matiec_flags))],str(self.CExtension.getLDFLAGS()),True laurent@656: laurent@656: lbessard@145: #------------------------------------------------------------------------------- lbessard@145: # Current Buffering Management Functions lbessard@145: #------------------------------------------------------------------------------- lbessard@145: lbessard@145: """ laurent@651: Return a copy of the cfile model lbessard@145: """ lbessard@145: def Copy(self, model): lbessard@145: return cPickle.loads(cPickle.dumps(model)) lbessard@145: laurent@651: def CreateConfigBuffer(self, saved): laurent@651: self.CFileBuffer = UndoBuffer(cPickle.dumps(self.CFile), saved) laurent@651: lbessard@145: def BufferCFile(self): laurent@651: self.CFileBuffer.Buffering(cPickle.dumps(self.CFile)) lbessard@145: lbessard@145: def StartBuffering(self): lbessard@145: self.Buffering = True lbessard@145: lbessard@145: def EndBuffering(self): lbessard@145: if self.Buffering: laurent@651: self.CFileBuffer.Buffering(cPickle.dumps(self.CFile)) lbessard@145: self.Buffering = False lbessard@145: laurent@651: def MarkCFileAsSaved(self): laurent@651: self.EndBuffering() laurent@651: self.CFileBuffer.CurrentSaved() laurent@651: lbessard@145: def CFileIsSaved(self): lbessard@145: if self.CFileBuffer: laurent@651: return self.CFileBuffer.IsCurrentSaved() and not self.Buffering lbessard@145: else: lbessard@145: return True lbessard@145: lbessard@145: def LoadPrevious(self): laurent@651: self.EndBuffering() laurent@651: self.CFile = cPickle.loads(self.CFileBuffer.Previous()) lbessard@145: lbessard@145: def LoadNext(self): laurent@651: self.CFile = cPickle.loads(self.CFileBuffer.Next()) lbessard@145: lbessard@145: def GetBufferState(self): lbessard@145: first = self.CFileBuffer.IsFirst() lbessard@145: last = self.CFileBuffer.IsLast() lbessard@145: return not first, not last lbessard@145: etisserant@31: class RootClass: etisserant@31: etisserant@106: PlugChildsTypes = [("C_File",_Cfile, "C file")] etisserant@31: etisserant@203: def PlugGenerate_C(self, buildpath, locations): etisserant@55: return [],"",False etisserant@31: etisserant@31: