added a gpib plot receiver mockup for testing purpose. Seems to work.

Fri, 25 Jan 2008 20:25:39 +0100

author
David Douard <david.douard@logilab.fr>
date
Fri, 25 Jan 2008 20:25:39 +0100
changeset 27
0f8f2621418f
parent 26
e8f3c9276f3f
child 28
0a7270d439d0

added a gpib plot receiver mockup for testing purpose. Seems to work.

gpib_plotter.py file | annotate | diff | comparison | revisions
gpib_plotter_mockup.py file | annotate | diff | comparison | revisions
qgpib_plotter.py file | annotate | diff | comparison | revisions
--- a/gpib_plotter.py	Sun Jan 20 17:09:31 2008 +0100
+++ b/gpib_plotter.py	Fri Jan 25 20:25:39 2008 +0100
@@ -18,6 +18,9 @@
                  address=5):
         super(GPIBplotter, self).__init__(device, baudrate, timeout, address, mode=0)
 
+    def plotStarted(self):
+        pass
+
     def load_plot(self, wait_timeout=0):
         """
         Make a full plot process.
@@ -43,6 +46,7 @@
             if firstloop:
                 self._cnx.timeout = self._timeout
                 firstloop = False
+                self.plotStarted()
             if l == "":
                 if i == 0:# > (self._retries/2):
                     # ie. we just received new stuffs (i is reset in the else block)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpib_plotter_mockup.py	Fri Jan 25 20:25:39 2008 +0100
@@ -0,0 +1,35 @@
+import glob
+import os
+import time
+import random
+
+class GPIBplotter:#(GPIB):
+    """
+    A mockup thet will find in a directory some HPGL files and feed them randomly
+    """
+    def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
+                 address=5):
+        self._timeout = timeout
+        self.filenames = glob.glob('examples/*.plt')
+        self._next = random.randint(10,50)
+        self._num = 0
+
+    def plotStarted(self):
+        pass
+        
+    def load_plot(self, wait_timeout=0):
+        if wait_timeout:
+            time.sleep(wait_timeout)
+        self._num += 1
+        if self._num > self._next:
+            ret = open(random.choice(self.filenames)).read()
+            if len(ret)>0:
+                self.plotStarted()
+                self._num = 0
+                self._next = random.randint(10,100)
+                time.sleep(random.randint(1,3))
+                return ret
+            return None
+        return None
+    
+        
--- a/qgpib_plotter.py	Sun Jan 20 17:09:31 2008 +0100
+++ b/qgpib_plotter.py	Fri Jan 25 20:25:39 2008 +0100
@@ -1,11 +1,13 @@
 #
 
 import os, sys
+import time
 
 from PyQt4 import QtGui, QtCore, uic
 from PyQt4.QtCore import SIGNAL, Qt
 
-from gpib_plotter import GPIBplotter
+#from gpib_plotter import GPIBplotter
+from gpib_plotter_mockup import GPIBplotter
 from hpgl_qt import QHPGLPlotterWidget
 
 form_class, base_class = uic.loadUiType(os.path.join(os.path.dirname(__file__), "qhpgl_plotter.ui"))
@@ -21,6 +23,7 @@
     _pos = PreferenceItem(basetype=QtCore.QPoint)
     _size = PreferenceItem(basetype=QtCore.QSize)
     _appState = PreferenceItem(basetype=QtCore.QByteArray)
+
     
 class QtHPGLPlotter(QtGui.QMainWindow, form_class):
     def __init__(self, parent=None):
@@ -105,6 +108,21 @@
 
         if not self.plotsView.currentIndex().isValid():
             self.plotsView.setCurrentIndex(self.plotsView.model().index(0, 0))
+
+    def plotReceived(self, num):
+        print "plot received", num
+        self._receiving = False
+        self.setReceivingLed()
+        plot, timestamp = self.captureThread.getPlot(num)
+        name = "plot_%s"%(num)
+        lst = self.plotsView.model().stringList()
+        lst.append(name)            
+        self._plots[name] = plot
+        self.plotsView.model().setStringList(lst)
+
+    def plotStarted(self):
+        self._receiving = True
+        self.setReceivingLed()
         
     def saveTriggered(self, checked=False):
         print "save"
@@ -112,15 +130,22 @@
         print "saveAs"
         
     def initializeGPIB(self):
+        self._online = False
         try:
-            self.gpib_plotter = GPIBplotter(device=self._prefs.device,
+            self.gpib_plotter = QGPIBplotter(device=self._prefs.device,
                                             address=self._prefs.address,
                                             )
-        except:
+            self.captureThread = GPIBReceiver(self.gpib_plotter)
+            self.connect(self.captureThread, SIGNAL('plotReceived(int)'),
+                         self.plotReceived)
+            self.connect(self.captureThread, SIGNAL('plotStarted()'),
+                         self.plotStarted)
+            self.captureThread.start()
+        except Exception, e:
+            #print e
             self.gpib_plotter = None
-        self._online = False
         self.setCaptureLed()
-        
+
     def captureToggled(self, state):
         if state:
             if self.gpib_plotter is None:
@@ -129,22 +154,102 @@
                     QtGui.QMessageBox.critical(self, self.tr("GPIB error"),
                                                self.tr("<b>Unable to initialize GPIB connection</b>.<br>Please check your GPIB dongle and settings."))
                     self._online = False
-                else:
-                    # todo
-                    self._online = True
+                    self.setCaptureLed()
+                    return
+            self._online = True
+            self.captureThread.startCapture()
         else:
+            if self.captureThread:
+                self.captureThread.stopCapture()
             self._online = False
         self.setCaptureLed()
-            
-            
+        
     def setCaptureLed(self):
         if self._online:
-            icn = QtGui.QIcon(':/icons/led_green.png')
+            icn = QtGui.QIcon(':/icons/led_green.svg')
         else:
-            icn = QtGui.QIcon(':/icons/led_green_off.png')
+            icn = QtGui.QIcon(':/icons/led_green_off.svg')
         self.captureButton.setIcon(icn)
         self.captureButton.setChecked(self._online)
+
+    def setReceivingLed(self):
+        if self._receiving:
+            icn = QtGui.QIcon(':/icons/led_red.svg')
+        else:
+            icn = QtGui.QIcon(':/icons/led_red_off.svg')
+        self.receivingButton.setIcon(icn)
+
+class QGPIBplotter(GPIBplotter):
+    def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
+                 address=5):
+        GPIBplotter.__init__(self, device, baudrate, timeout, address)        
+        self.emitter = None
+
+    def plotStarted(self):
+        if self.emitter:
+            self.emitter.emit(SIGNAL('plotStarted()'))
+            #self.emitter.msleep(1)
         
+class GPIBReceiver(QtCore.QThread):
+    def __init__(self, cnx):
+        QtCore.QThread.__init__(self)
+        self.gpibplotter = cnx
+        self.gpibplotter.emitter = self
+        
+        self._cancelmutex = QtCore.QMutex()
+        self._cancel = False
+        #self._nreceived = 0
+        self._plotsmutex = QtCore.QMutex()
+        self._plots = []
+        self._startstopmutex = QtCore.QMutex()
+        self._startstop = QtCore.QWaitCondition()
+        self._capturing = False
+        
+    def cancel(self):
+        self._cancelmutex.lock()
+        self._cancel = True
+        self._cancelmutex.unlock()
+
+    def startCapture(self):
+        self._startstop.wakeOne()
+
+    def stopCapture(self):
+        self._startstopmutex.lock()
+        self._capturing = False
+        self._startstopmutex.unlock()
+
+    def run(self):
+        while 1:
+            self._cancelmutex.lock()
+            if self._cancel:
+                return
+            self._cancelmutex.unlock()
+            self._startstopmutex.lock()
+            if not self._capturing:
+                self._startstop.wait(self._startstopmutex)
+                self._capturing = True
+            self._startstopmutex.unlock()
+        
+            plot = self.gpibplotter.load_plot(wait_timeout=0.1)
+            timestamp = time.time()
+            if plot:
+                self._plotsmutex.lock()
+                self._plots.append((plot, timestamp))
+                n = len(self._plots)
+                self._plotsmutex.unlock()
+                self.emit(SIGNAL('plotReceived(int)'), n-1)
+            self.msleep(10)
+            
+    def getPlot(self, num):
+        self._plotsmutex.lock()
+        try:
+            return self._plots[num]
+        finally:
+            self._plotsmutex.unlock()
+            
+        
+            
+
 if __name__ == '__main__':
     a = QtGui.QApplication([])
     w = QtHPGLPlotter()

mercurial