canfestival/canfestival.py
branch1.1 Korean release
changeset 968 eee7625de1f7
parent 960 d72a866cf9b0
child 974 0d32a8748814
equal deleted inserted replaced
808:6e205c1f05a0 968:eee7625de1f7
     1 import os, sys
     1 import os, sys
       
     2 
     2 base_folder = os.path.split(sys.path[0])[0]
     3 base_folder = os.path.split(sys.path[0])[0]
     3 CanFestivalPath = os.path.join(base_folder, "CanFestival-3")
     4 CanFestivalPath = os.path.join(base_folder, "CanFestival-3")
     4 sys.path.append(os.path.join(CanFestivalPath, "objdictgen"))
     5 sys.path.append(os.path.join(CanFestivalPath, "objdictgen"))
       
     6 
       
     7 import wx
     5 
     8 
     6 from nodelist import NodeList
     9 from nodelist import NodeList
     7 from nodemanager import NodeManager
    10 from nodemanager import NodeManager
     8 import config_utils, gen_cfile, eds_utils
    11 import config_utils, gen_cfile, eds_utils
     9 from networkedit import networkedit
    12 from networkedit import networkedit
    10 from objdictedit import objdictedit
    13 from objdictedit import objdictedit
    11 import canfestival_config as local_canfestival_config
    14 import canfestival_config as local_canfestival_config
    12 from ConfigTreeNode import ConfigTreeNode
    15 from ConfigTreeNode import ConfigTreeNode
    13 from commondialogs import CreateNodeDialog
    16 from commondialogs import CreateNodeDialog
    14 import wx
       
    15 
    17 
    16 from SlaveEditor import SlaveEditor, MasterViewer
    18 from SlaveEditor import SlaveEditor, MasterViewer
    17 from NetworkEditor import NetworkEditor
    19 from NetworkEditor import NetworkEditor
    18 
    20 
    19 from gnosis.xml.pickle import *
    21 from gnosis.xml.pickle import *
    20 from gnosis.xml.pickle.util import setParanoia
    22 from gnosis.xml.pickle.util import setParanoia
    21 setParanoia(0)
    23 setParanoia(0)
    22 
    24 
    23 if wx.Platform == '__WXMSW__':
    25 from util.TranslationCatalogs import AddCatalog
    24     DEFAULT_SETTINGS = {
    26 AddCatalog(os.path.join(CanFestivalPath, "objdictgen", "locale"))
    25         "CAN_Driver": "can_tcp_win32",
       
    26         "CAN_Device": "127.0.0.1",
       
    27         "CAN_Baudrate": "125K",
       
    28         "Slave_NodeId": 2,
       
    29         "Master_NodeId": 1,
       
    30     }
       
    31 else:
       
    32     DEFAULT_SETTINGS = {
       
    33         "CAN_Driver": "../CanFestival-3/drivers/can_socket/libcanfestival_can_socket.so",
       
    34         "CAN_Device": "vcan0",
       
    35         "CAN_Baudrate": "125K",
       
    36         "Slave_NodeId": 2,
       
    37         "Master_NodeId": 1,
       
    38     }
       
    39 
    27 
    40 #--------------------------------------------------
    28 #--------------------------------------------------
    41 #                    SLAVE
    29 #                    SLAVE
    42 #--------------------------------------------------
    30 #--------------------------------------------------
    43 
    31 
    44 class _SlaveCTN(NodeManager):
    32 class _SlaveCTN(NodeManager):
    45     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
    33     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
    46     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    34     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    47       <xsd:element name="CanFestivalSlaveNode">
    35       <xsd:element name="CanFestivalSlaveNode">
    48         <xsd:complexType>
    36         <xsd:complexType>
    49           <xsd:attribute name="CAN_Device" type="xsd:string" use="optional" default="%(CAN_Device)s"/>
    37           <xsd:attribute name="CAN_Device" type="xsd:string" use="optional"/>
    50           <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="optional" default="%(CAN_Baudrate)s"/>
    38           <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="optional"/>
    51           <xsd:attribute name="NodeId" type="xsd:string" use="optional" default="%(Slave_NodeId)d"/>
    39           <xsd:attribute name="NodeId" type="xsd:integer" use="optional" default="2"/>
    52           <xsd:attribute name="Sync_Align" type="xsd:integer" use="optional" default="0"/>
    40           <xsd:attribute name="Sync_Align" type="xsd:integer" use="optional" default="0"/>
    53           <xsd:attribute name="Sync_Align_Ratio" use="optional" default="50">
    41           <xsd:attribute name="Sync_Align_Ratio" use="optional" default="50">
    54             <xsd:simpleType>
    42             <xsd:simpleType>
    55                 <xsd:restriction base="xsd:integer">
    43                 <xsd:restriction base="xsd:integer">
    56                     <xsd:minInclusive value="1"/>
    44                     <xsd:minInclusive value="1"/>
    59             </xsd:simpleType>
    47             </xsd:simpleType>
    60           </xsd:attribute>
    48           </xsd:attribute>
    61         </xsd:complexType>
    49         </xsd:complexType>
    62       </xsd:element>
    50       </xsd:element>
    63     </xsd:schema>
    51     </xsd:schema>
    64     """ % DEFAULT_SETTINGS
    52     """
    65     
    53     
    66     EditorType = SlaveEditor
    54     EditorType = SlaveEditor
    67     IconPath = os.path.join(CanFestivalPath, "objdictgen", "networkedit.png")
    55     IconPath = os.path.join(CanFestivalPath, "objdictgen", "networkedit.png")
    68 
    56 
    69     def __init__(self):
    57     def __init__(self):
   100                                    "heartbeat",  # NMT
    88                                    "heartbeat",  # NMT
   101                                    [])           # options
    89                                    [])           # options
   102             dialog.Destroy()
    90             dialog.Destroy()
   103             self.OnCTNSave()
    91             self.OnCTNSave()
   104 
    92 
       
    93     def GetCurrentNodeName(self):
       
    94         return self.CTNName()
       
    95 
   105     def GetSlaveODPath(self):
    96     def GetSlaveODPath(self):
   106         return os.path.join(self.CTNPath(), 'slave.od')
    97         return os.path.join(self.CTNPath(), 'slave.od')
   107 
    98 
   108     def GetCanDevice(self):
    99     def GetCanDevice(self):
   109         return self.CanFestivalSlaveNode.getCan_Device()
   100         return self.CanFestivalSlaveNode.getCAN_Device()
   110 
   101 
   111     def _OpenView(self, name=None, onlyopened=False):
   102     def _OpenView(self, name=None, onlyopened=False):
   112         ConfigTreeNode._OpenView(self, name, onlyopened)
   103         ConfigTreeNode._OpenView(self, name, onlyopened)
   113         if self._View is not None:
   104         if self._View is not None:
   114             self._View.SetBusId(self.GetCurrentLocation())
   105             self._View.SetBusId(self.GetCurrentLocation())
   115         return self._View
   106         return self._View
   116     
   107     
   117     def _ExportSlave(self):
   108     def _ExportSlave(self):
   118         dialog = wx.FileDialog(self.GetCTRoot().AppFrame, _("Choose a file"), os.getcwd(), "",  _("EDS files (*.eds)|*.eds|All files|*.*"), wx.SAVE|wx.OVERWRITE_PROMPT)
   109         dialog = wx.FileDialog(self.GetCTRoot().AppFrame, 
       
   110                                _("Choose a file"), 
       
   111                                os.path.expanduser("~"), 
       
   112                                "%s.eds" % self.CTNName(),  
       
   113                                _("EDS files (*.eds)|*.eds|All files|*.*"),
       
   114                                wx.SAVE|wx.OVERWRITE_PROMPT)
   119         if dialog.ShowModal() == wx.ID_OK:
   115         if dialog.ShowModal() == wx.ID_OK:
   120             result = eds_utils.GenerateEDSFile(dialog.GetPath(), self.GetCurrentNodeCopy())
   116             result = eds_utils.GenerateEDSFile(dialog.GetPath(), self.GetCurrentNodeCopy())
   121             if result:
   117             if result:
   122                 self.GetCTRoot().logger.write_error(_("Error: Export slave failed\n"))
   118                 self.GetCTRoot().logger.write_error(_("Error: Export slave failed\n"))
   123         dialog.Destroy()  
   119         dialog.Destroy()  
   212     def GetBufferState(self):
   208     def GetBufferState(self):
   213         return self.GetCurrentBufferState()
   209         return self.GetCurrentBufferState()
   214     
   210     
   215     ConfNodeMethods = []
   211     ConfNodeMethods = []
   216 
   212 
       
   213 class _NodeManager(NodeManager):
       
   214 
       
   215     def __init__(self, parent, *args, **kwargs):
       
   216         NodeManager.__init__(self, *args, **kwargs)
       
   217         self.Parent = parent
       
   218         
       
   219     def __del__(self):
       
   220         self.Parent = None
       
   221         
       
   222     def GetCurrentNodeName(self):
       
   223         return self.Parent.CTNName()
       
   224     
       
   225     def GetCurrentNodeID(self):
       
   226         return self.Parent.CanFestivalNode.getNodeId()
       
   227     
   217 class _NodeListCTN(NodeList):
   228 class _NodeListCTN(NodeList):
   218     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
   229     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
   219     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   230     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   220       <xsd:element name="CanFestivalNode">
   231       <xsd:element name="CanFestivalNode">
   221         <xsd:complexType>
   232         <xsd:complexType>
   222           <xsd:attribute name="CAN_Device" type="xsd:string" use="optional" default="%(CAN_Device)s"/>
   233           <xsd:attribute name="CAN_Device" type="xsd:string" use="optional"/>
   223           <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="optional" default="%(CAN_Baudrate)s"/>
   234           <xsd:attribute name="CAN_Baudrate" type="xsd:string" use="optional"/>
   224           <xsd:attribute name="NodeId" type="xsd:string" use="optional" default="%(Master_NodeId)d"/>
   235           <xsd:attribute name="NodeId" type="xsd:integer" use="optional" default="1"/>
   225           <xsd:attribute name="Sync_TPDOs" type="xsd:boolean" use="optional" default="true"/>
   236           <xsd:attribute name="Sync_TPDOs" type="xsd:boolean" use="optional" default="true"/>
   226         </xsd:complexType>
   237         </xsd:complexType>
   227       </xsd:element>
   238       </xsd:element>
   228     </xsd:schema>
   239     </xsd:schema>
   229     """ % DEFAULT_SETTINGS
   240     """ 
   230     
   241     
   231     EditorType = NetworkEditor
   242     EditorType = NetworkEditor
   232     IconPath = os.path.join(CanFestivalPath, "objdictgen", "networkedit.png")
   243     IconPath = os.path.join(CanFestivalPath, "objdictgen", "networkedit.png")
   233     
   244     
   234     def __init__(self):
   245     def __init__(self):
   235         manager = NodeManager()
   246         manager = _NodeManager(self)
   236         NodeList.__init__(self, manager)
   247         NodeList.__init__(self, manager)
   237         self.LoadProject(self.CTNPath())
   248         self.LoadProject(self.CTNPath())
   238         self.SetNetworkName(self.BaseParams.getName())
   249         self.SetNetworkName(self.BaseParams.getName())
   239     
   250     
   240     def GetCanDevice(self):
   251     def GetCanDevice(self):
   241         return self.CanFestivalNode.getCan_Device()
   252         return self.CanFestivalNode.getCAN_Device()
   242     
   253     
   243     def SetParamsAttribute(self, path, value):
   254     def SetParamsAttribute(self, path, value):
   244         result = ConfigTreeNode.SetParamsAttribute(self, path, value)
   255         if path == "CanFestivalNode.NodeId":
       
   256             nodeid = self.CanFestivalNode.getNodeId()
       
   257             if value != nodeid:
       
   258                 slaves = self.GetSlaveIDs()
       
   259                 dir = (value - nodeid) / abs(value - nodeid)
       
   260                 while value in slaves and value >= 0:
       
   261                     value += dir
       
   262                 if value < 0:
       
   263                     value = nodeid
       
   264         
       
   265         value, refresh = ConfigTreeNode.SetParamsAttribute(self, path, value)
       
   266         refresh_network = False
   245         
   267         
   246         # Filter IEC_Channel and Name, that have specific behavior
   268         # Filter IEC_Channel and Name, that have specific behavior
   247         if path == "BaseParams.IEC_Channel" and self._View is not None:
   269         if path == "BaseParams.IEC_Channel" and self._View is not None:
   248             self._View.SetBusId(self.GetCurrentLocation())
   270             self._View.SetBusId(self.GetCurrentLocation())
   249         elif path == "BaseParams.Name":
   271         elif path == "BaseParams.Name":
   250             self.SetNetworkName(value)
   272             self.SetNetworkName(value)
   251         
   273             refresh_network = True
   252         return result
   274         elif path == "CanFestivalNode.NodeId":
   253     
   275             refresh_network = True
       
   276             
       
   277         if refresh_network and self._View is not None:
       
   278             wx.CallAfter(self._View.RefreshBufferState)
       
   279         return value, refresh
       
   280         
   254     _GeneratedMasterView = None
   281     _GeneratedMasterView = None
   255     def _ShowGeneratedMaster(self):
   282     def _ShowGeneratedMaster(self):
   256         self._OpenView("Generated master")
   283         self._OpenView("Generated master")
   257         
   284         
   258     def _OpenView(self, name=None, onlyopened=False):
   285     def _OpenView(self, name=None, onlyopened=False):
   268                 masterpath = os.path.join(buildpath, "MasterGenerated.od")
   295                 masterpath = os.path.join(buildpath, "MasterGenerated.od")
   269                 if not os.path.exists(masterpath):
   296                 if not os.path.exists(masterpath):
   270                     self.GetCTRoot().logger.write_error(_("Error: No Master generated\n"))
   297                     self.GetCTRoot().logger.write_error(_("Error: No Master generated\n"))
   271                     return
   298                     return
   272                 
   299                 
   273                 manager = MiniNodeManager(self, masterpath, self.CTNFullName() + ".generated_master")
   300                 manager = MiniNodeManager(self, masterpath, self.CTNFullName())
   274                 self._GeneratedMasterView = MasterViewer(app_frame.TabsOpened, manager, app_frame)
   301                 self._GeneratedMasterView = MasterViewer(app_frame.TabsOpened, manager, app_frame, name)
   275                 
   302                 
   276             if self._GeneratedMasterView is not None:
   303             if self._GeneratedMasterView is not None:
   277                 app_frame.EditProjectElement(self._GeneratedMasterView, name)
   304                 app_frame.EditProjectElement(self._GeneratedMasterView, self._GeneratedMasterView.GetInstancePath())
   278             
   305             
   279             return self._GeneratedMasterView
   306             return self._GeneratedMasterView
   280         else:
   307         else:
   281             ConfigTreeNode._OpenView(self, name, onlyopened)
   308             ConfigTreeNode._OpenView(self, name, onlyopened)
   282             if self._View is not None:
   309             if self._View is not None:
   353 class RootClass:
   380 class RootClass:
   354     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
   381     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
   355     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   382     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   356       <xsd:element name="CanFestivalInstance">
   383       <xsd:element name="CanFestivalInstance">
   357         <xsd:complexType>
   384         <xsd:complexType>
   358           <xsd:attribute name="CAN_Driver" type="xsd:string" use="optional" default="%(CAN_Driver)s"/>
   385           <xsd:attribute name="CAN_Driver" type="xsd:string" use="optional"/>
   359           <xsd:attribute name="Debug_mode" type="xsd:boolean" use="optional" default="false"/>
       
   360         </xsd:complexType>
   386         </xsd:complexType>
   361       </xsd:element>
   387       </xsd:element>
   362     </xsd:schema>
   388     </xsd:schema>
   363     """ % DEFAULT_SETTINGS
   389     """
   364     
   390     
   365     CTNChildrenTypes = [("CanOpenNode",_NodeListCTN, "CanOpen Master"),
   391     CTNChildrenTypes = [("CanOpenNode",_NodeListCTN, "CanOpen Master"),
   366                        ("CanOpenSlave",_SlaveCTN, "CanOpen Slave")]
   392                        ("CanOpenSlave",_SlaveCTN, "CanOpen Slave")]
   367     def GetParamsAttributes(self, path = None):
   393     def GetParamsAttributes(self, path = None):
   368         infos = ConfigTreeNode.GetParamsAttributes(self, path = None)
   394         infos = ConfigTreeNode.GetParamsAttributes(self, path = path)
   369         for element in infos:
   395         for element in infos:
   370             if element["name"] == "CanFestivalInstance":
   396             if element["name"] == "CanFestivalInstance":
   371                 for child in element["children"]:
   397                 for child in element["children"]:
   372                     if child["name"] == "CAN_Driver":
   398                     if child["name"] == "CAN_Driver":
   373                         DLL_LIST= getattr(local_canfestival_config,"DLL_LIST",None)
   399                         child["type"] = local_canfestival_config.DLL_LIST
   374                         if DLL_LIST is not None:
       
   375                             child["type"] = DLL_LIST  
       
   376         return infos
   400         return infos
   377     
   401     
   378     def GetCanDriver(self):
   402     def CTNGenerate_C(self, buildpath, locations):
   379         can_driver = self.CanFestivalInstance.getCAN_Driver()
   403         can_driver = self.CanFestivalInstance.getCAN_Driver()
   380         if sys.platform == 'win32':
   404         if not can_driver :
   381             if self.CanFestivalInstance.getDebug_mode() and os.path.isfile(os.path.join("%s"%(can_driver + '_DEBUG.dll'))):
   405             can_driver = local_canfestival_config.DLL_LIST[0]
   382                 can_driver += '_DEBUG.dll'
   406         can_drv_ext = self.GetCTRoot().GetBuilder().extension
   383             else:
   407         can_driver_name = "libcanfestival_" + can_driver + can_drv_ext
   384                 can_driver += '.dll'
       
   385         return can_driver
       
   386     
       
   387     def CTNGenerate_C(self, buildpath, locations):
       
   388         
   408         
   389         format_dict = {"locstr" : "_".join(map(str,self.GetCurrentLocation())),
   409         format_dict = {"locstr" : "_".join(map(str,self.GetCurrentLocation())),
   390                        "candriver" : self.GetCanDriver(),
   410                        "candriver" : can_driver_name,
   391                        "nodes_includes" : "",
   411                        "nodes_includes" : "",
   392                        "board_decls" : "",
   412                        "board_decls" : "",
   393                        "nodes_init" : "",
   413                        "nodes_init" : "",
   394                        "nodes_open" : "",
   414                        "nodes_open" : "",
   395                        "nodes_stop" : "",
   415                        "nodes_stop" : "",
   476         cf_main = open(filename).read() % format_dict
   496         cf_main = open(filename).read() % format_dict
   477         cf_main_path = os.path.join(buildpath, "CF_%(locstr)s.c"%format_dict)
   497         cf_main_path = os.path.join(buildpath, "CF_%(locstr)s.c"%format_dict)
   478         f = open(cf_main_path,'w')
   498         f = open(cf_main_path,'w')
   479         f.write(cf_main)
   499         f.write(cf_main)
   480         f.close()
   500         f.close()
   481         
   501 
   482         return [(cf_main_path, local_canfestival_config.getCFLAGS(CanFestivalPath))],local_canfestival_config.getLDFLAGS(CanFestivalPath), True
   502         res = [(cf_main_path, local_canfestival_config.getCFLAGS(CanFestivalPath))],local_canfestival_config.getLDFLAGS(CanFestivalPath), True
   483 
   503 
   484 
   504         can_driver_path = os.path.join(CanFestivalPath,"drivers",can_driver,can_driver_name)
       
   505         if os.path.exists(can_driver_path):
       
   506             res += ((can_driver_name, file(can_driver_path,"rb")),)
       
   507 
       
   508         return res
       
   509