Now debug is not a button anymore
authorEdouard TISSERANT <edouard.tisserant@gmail.com>
Sun, 06 Dec 2009 21:03:10 +0100
changeset 462 274e83a5534e
parent 461 bcbc472c0ba8
child 463 961bddcfc913
Now debug is not a button anymore
plugger.py
runtime/PLCObject.py
targets/Linux/plc_Linux_main.c
--- a/plugger.py	Sun Dec 06 19:39:24 2009 +0100
+++ b/plugger.py	Sun Dec 06 21:03:10 2009 +0100
@@ -1547,17 +1547,13 @@
         if(self.previous_plcstate != status):
             for args in {
                      "Started" :     [("_Run", False),
-                                      ("_Debug", False),
                                       ("_Stop", True)],
                      "Stopped" :     [("_Run", True),
-                                      ("_Debug", True),
                                       ("_Stop", False)],
                      "Empty" :       [("_Run", False),
-                                      ("_Debug", False),
                                       ("_Stop", False)],
                      "Broken" :      [],
                      "Disconnected" :[("_Run", False),
-                                      ("_Debug", False),
                                       ("_Stop", False),
                                       ("_Transfer", False),
                                       ("_Connect", True),
@@ -1578,13 +1574,6 @@
                 self.previous_plcstate, self.logger.write)(_("PLC is %s\n")%status)
             self.AppFrame.RefreshAll()
         
-    def _Run(self):
-        """
-        Start PLC
-        """
-        self._connector.StartPLC()
-        self.UpdateMethodsFromPLCStatus()
-
     def RegisterDebugVarToConnector(self):
         self.DebugTimer=None
         Idxs = []
@@ -1729,12 +1718,12 @@
             self.logger.write_warning(_("Debug Thread couldn't be killed"))
         self.DebugThread = None
 
-    def _Debug(self):
+    def _Run(self):
         """
         Start PLC (Debug Mode)
         """
         if self.GetIECProgramsAndVariables():
-            self._connector.StartPLC(debug=True)
+            self._connector.StartPLC()
             self.logger.write(_("Starting PLC (debug mode)\n"))
             if self.AppFrame:
                 self.AppFrame.ResetGraphicViewers()
@@ -1914,15 +1903,6 @@
          "shown" : False,
          "tooltip" : _("Start PLC"),
          "method" : "_Run"},
-        {"bitmap" : opjimg("Debug"),
-         "name" : _("Debug"),
-         "shown" : False,
-         "tooltip" : _("Start PLC (debug mode)"),
-         "method" : "_Debug"},
-#        {"bitmap" : opjimg("Debug"),
-#         "name" : "Do_Test_Debug",
-#         "tooltip" : "Test debug mode)",
-#         "method" : "_Do_Test_Debug"},
         {"bitmap" : opjimg("Stop"),
          "name" : _("Stop"),
          "shown" : False,
--- a/runtime/PLCObject.py	Sun Dec 06 19:39:24 2009 +0100
+++ b/runtime/PLCObject.py	Sun Dec 06 21:03:10 2009 +0100
@@ -143,6 +143,7 @@
 
             self._suspendDebug = self.PLClibraryHandle.suspendDebug
             self._suspendDebug.restype = None
+            self._suspendDebug.argtypes = [ctypes.c_int]
 
             self._resumeDebug = self.PLClibraryHandle.resumeDebug
             self._resumeDebug.restype = None
@@ -215,16 +216,12 @@
             self.website.PLCStopped()
         self.python_threads_vars = None
 
-    def PythonThreadProc(self, debug):
+    def PythonThreadProc(self):
         PLCprint("PythonThreadProc started")
         c_argv = ctypes.c_char_p * len(self.argv)
         error = None
         if self._LoadNewPLC():
             if self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
-                if debug:
-                    for idx in self._Idxs:
-                        self._RegisterDebugVariable(idx)
-                    self._resumeDebug()
                 self.PLCStatus = "Started"
                 self.StatusChange()
                 self.evaluator(self.PrepareRuntimePy)
@@ -253,11 +250,11 @@
         self._FreePLC()
         PLCprint("PythonThreadProc interrupted")
     
-    def StartPLC(self, debug=False):
+    def StartPLC(self):
         PLCprint("StartPLC")
         if self.CurrentPLCFilename is not None:
             self.PLCStatus = "Started"
-            self.PythonThread = Thread(target=self.PythonThreadProc, args=[debug])
+            self.PythonThread = Thread(target=self.PythonThreadProc)
             self.PythonThread.start()
             
     def StopPLC(self):
@@ -335,13 +332,18 @@
         Call ctype imported function to append 
         these indexes to registred variables in PLC debugger
         """
-        self._suspendDebug()
-        # keep a copy of requested idx
-        self._Idxs = idxs[:]
-        self._ResetDebugVariables()
-        for idx,iectype in idxs:
-            self._RegisterDebugVariable(idx)
-        self._resumeDebug()
+        if idxs:
+            # suspend but dont disable
+            self._suspendDebug(False)
+            # keep a copy of requested idx
+            self._Idxs = idxs[:]
+            self._ResetDebugVariables()
+            for idx,iectype in idxs:
+                self._RegisterDebugVariable(idx)
+            self._resumeDebug()
+        else:
+            self._suspendDebug(True)
+            self._Idxs =  []
 
     class IEC_STRING(ctypes.Structure):
         """
--- a/targets/Linux/plc_Linux_main.c	Sun Dec 06 19:39:24 2009 +0100
+++ b/targets/Linux/plc_Linux_main.c	Sun Dec 06 21:03:10 2009 +0100
@@ -85,7 +85,9 @@
     sigev.sigev_notify_function = PLC_timer_notify;
 
     pthread_mutex_init(&debug_wait_mutex, NULL);
+    pthread_mutex_init(&debug_mutex, NULL);
     pthread_mutex_init(&python_wait_mutex, NULL);
+    pthread_mutex_init(&python_mutex, NULL);
 
     pthread_mutex_lock(&debug_wait_mutex);
     pthread_mutex_lock(&python_wait_mutex);
@@ -95,10 +97,7 @@
         PLC_SetTimer(Ttick,Ttick);
 
         /* install signal handler for manual break */
-//        signal(SIGTERM, catch_signal);
         signal(SIGINT, catch_signal);
-
-        pthread_mutex_trylock(&debug_mutex);
     }else{
         return 1;
     }
@@ -107,7 +106,14 @@
 
 int TryEnterDebugSection(void)
 {
-    return pthread_mutex_trylock(&debug_mutex) == 0;
+    if (pthread_mutex_trylock(&debug_mutex) == 0){
+        /* Only enter if debug active */
+        if(__DEBUG){
+            return 1;
+        }
+    }
+    pthread_mutex_unlock(&debug_mutex);
+    return 0;
 }
 
 void LeaveDebugSection(void)
@@ -149,11 +155,12 @@
     pthread_mutex_unlock(&debug_wait_mutex);
 }
 
-void suspendDebug(void)
-{
-    __DEBUG = 0;
+void suspendDebug(int disable)
+{
     /* Prevent PLC to enter debug code */
     pthread_mutex_lock(&debug_mutex);
+    /*__DEBUG is protected by this mutex */
+    __DEBUG = !disable;
 }
 
 void resumeDebug(void)