103 self._plots[name] = data |
106 self._plots[name] = data |
104 self.plotsView.model().setStringList(lst) |
107 self.plotsView.model().setStringList(lst) |
105 |
108 |
106 if not self.plotsView.currentIndex().isValid(): |
109 if not self.plotsView.currentIndex().isValid(): |
107 self.plotsView.setCurrentIndex(self.plotsView.model().index(0, 0)) |
110 self.plotsView.setCurrentIndex(self.plotsView.model().index(0, 0)) |
|
111 |
|
112 def plotReceived(self, num): |
|
113 print "plot received", num |
|
114 self._receiving = False |
|
115 self.setReceivingLed() |
|
116 plot, timestamp = self.captureThread.getPlot(num) |
|
117 name = "plot_%s"%(num) |
|
118 lst = self.plotsView.model().stringList() |
|
119 lst.append(name) |
|
120 self._plots[name] = plot |
|
121 self.plotsView.model().setStringList(lst) |
|
122 |
|
123 def plotStarted(self): |
|
124 self._receiving = True |
|
125 self.setReceivingLed() |
108 |
126 |
109 def saveTriggered(self, checked=False): |
127 def saveTriggered(self, checked=False): |
110 print "save" |
128 print "save" |
111 def saveAsTriggered(self, checked=False): |
129 def saveAsTriggered(self, checked=False): |
112 print "saveAs" |
130 print "saveAs" |
113 |
131 |
114 def initializeGPIB(self): |
132 def initializeGPIB(self): |
|
133 self._online = False |
115 try: |
134 try: |
116 self.gpib_plotter = GPIBplotter(device=self._prefs.device, |
135 self.gpib_plotter = QGPIBplotter(device=self._prefs.device, |
117 address=self._prefs.address, |
136 address=self._prefs.address, |
118 ) |
137 ) |
119 except: |
138 self.captureThread = GPIBReceiver(self.gpib_plotter) |
|
139 self.connect(self.captureThread, SIGNAL('plotReceived(int)'), |
|
140 self.plotReceived) |
|
141 self.connect(self.captureThread, SIGNAL('plotStarted()'), |
|
142 self.plotStarted) |
|
143 self.captureThread.start() |
|
144 except Exception, e: |
|
145 #print e |
120 self.gpib_plotter = None |
146 self.gpib_plotter = None |
121 self._online = False |
|
122 self.setCaptureLed() |
147 self.setCaptureLed() |
123 |
148 |
124 def captureToggled(self, state): |
149 def captureToggled(self, state): |
125 if state: |
150 if state: |
126 if self.gpib_plotter is None: |
151 if self.gpib_plotter is None: |
127 self.initializeGPIB() |
152 self.initializeGPIB() |
128 if self.gpib_plotter is None: |
153 if self.gpib_plotter is None: |
129 QtGui.QMessageBox.critical(self, self.tr("GPIB error"), |
154 QtGui.QMessageBox.critical(self, self.tr("GPIB error"), |
130 self.tr("<b>Unable to initialize GPIB connection</b>.<br>Please check your GPIB dongle and settings.")) |
155 self.tr("<b>Unable to initialize GPIB connection</b>.<br>Please check your GPIB dongle and settings.")) |
131 self._online = False |
156 self._online = False |
132 else: |
157 self.setCaptureLed() |
133 # todo |
158 return |
134 self._online = True |
159 self._online = True |
135 else: |
160 self.captureThread.startCapture() |
|
161 else: |
|
162 if self.captureThread: |
|
163 self.captureThread.stopCapture() |
136 self._online = False |
164 self._online = False |
137 self.setCaptureLed() |
165 self.setCaptureLed() |
138 |
166 |
139 |
|
140 def setCaptureLed(self): |
167 def setCaptureLed(self): |
141 if self._online: |
168 if self._online: |
142 icn = QtGui.QIcon(':/icons/led_green.png') |
169 icn = QtGui.QIcon(':/icons/led_green.svg') |
143 else: |
170 else: |
144 icn = QtGui.QIcon(':/icons/led_green_off.png') |
171 icn = QtGui.QIcon(':/icons/led_green_off.svg') |
145 self.captureButton.setIcon(icn) |
172 self.captureButton.setIcon(icn) |
146 self.captureButton.setChecked(self._online) |
173 self.captureButton.setChecked(self._online) |
147 |
174 |
|
175 def setReceivingLed(self): |
|
176 if self._receiving: |
|
177 icn = QtGui.QIcon(':/icons/led_red.svg') |
|
178 else: |
|
179 icn = QtGui.QIcon(':/icons/led_red_off.svg') |
|
180 self.receivingButton.setIcon(icn) |
|
181 |
|
182 class QGPIBplotter(GPIBplotter): |
|
183 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, |
|
184 address=5): |
|
185 GPIBplotter.__init__(self, device, baudrate, timeout, address) |
|
186 self.emitter = None |
|
187 |
|
188 def plotStarted(self): |
|
189 if self.emitter: |
|
190 self.emitter.emit(SIGNAL('plotStarted()')) |
|
191 #self.emitter.msleep(1) |
|
192 |
|
193 class GPIBReceiver(QtCore.QThread): |
|
194 def __init__(self, cnx): |
|
195 QtCore.QThread.__init__(self) |
|
196 self.gpibplotter = cnx |
|
197 self.gpibplotter.emitter = self |
|
198 |
|
199 self._cancelmutex = QtCore.QMutex() |
|
200 self._cancel = False |
|
201 #self._nreceived = 0 |
|
202 self._plotsmutex = QtCore.QMutex() |
|
203 self._plots = [] |
|
204 self._startstopmutex = QtCore.QMutex() |
|
205 self._startstop = QtCore.QWaitCondition() |
|
206 self._capturing = False |
|
207 |
|
208 def cancel(self): |
|
209 self._cancelmutex.lock() |
|
210 self._cancel = True |
|
211 self._cancelmutex.unlock() |
|
212 |
|
213 def startCapture(self): |
|
214 self._startstop.wakeOne() |
|
215 |
|
216 def stopCapture(self): |
|
217 self._startstopmutex.lock() |
|
218 self._capturing = False |
|
219 self._startstopmutex.unlock() |
|
220 |
|
221 def run(self): |
|
222 while 1: |
|
223 self._cancelmutex.lock() |
|
224 if self._cancel: |
|
225 return |
|
226 self._cancelmutex.unlock() |
|
227 self._startstopmutex.lock() |
|
228 if not self._capturing: |
|
229 self._startstop.wait(self._startstopmutex) |
|
230 self._capturing = True |
|
231 self._startstopmutex.unlock() |
|
232 |
|
233 plot = self.gpibplotter.load_plot(wait_timeout=0.1) |
|
234 timestamp = time.time() |
|
235 if plot: |
|
236 self._plotsmutex.lock() |
|
237 self._plots.append((plot, timestamp)) |
|
238 n = len(self._plots) |
|
239 self._plotsmutex.unlock() |
|
240 self.emit(SIGNAL('plotReceived(int)'), n-1) |
|
241 self.msleep(10) |
|
242 |
|
243 def getPlot(self, num): |
|
244 self._plotsmutex.lock() |
|
245 try: |
|
246 return self._plots[num] |
|
247 finally: |
|
248 self._plotsmutex.unlock() |
|
249 |
|
250 |
|
251 |
|
252 |
148 if __name__ == '__main__': |
253 if __name__ == '__main__': |
149 a = QtGui.QApplication([]) |
254 a = QtGui.QApplication([]) |
150 w = QtHPGLPlotter() |
255 w = QtHPGLPlotter() |
151 w.show() |
256 w.show() |
152 a.exec_() |
257 a.exec_() |