def on_pbStart_clicked(self): logthread('MainWindow.on_pbStart_clicked') self.widget.pbrProgress.setRange(0, 0) self.teErrors.setText("") self.worker.start.emit()
def on_draw(self, frame_id, y): if frame_id % 500 == 0: logthread('StripWidget.on_draw') t0 = time.time() if self.ax == None: self.ax = self.fig.add_subplot(111) self.ax.set_autoscale_on(True) #print('x = ' + str(len(self.x)) + ' y = ' + str(len(y))) self.img, = self.ax.plot(self.x, y) self.ax.set_xlabel(self.x_label, fontsize=14, color='black') self.ax.set_ylabel(self.y_label, fontsize=14, color='black') self.set_integration_text() else: self.img.set_ydata(y) self.ax.relim() self.ax.autoscale_view(True, True, True) #self.ax.hist(self.x, bins=self.bins, facecolor='red', alpha=0.75) self.ax.set_title(self.title + ' id ' + str(frame_id) + ' ('+ str(self.n) + ' frames)') self.canvas.draw() self.n += 1 dt = time.time() - t0 self.t0sum += dt if self.n % 100 == 0: logthread('StripWidget on_draw {0} frames with {1} sec/image'.format(self.n, self.t0sum/50.)) self.t0sum = 0.
def __init__(self, name, parent = None, n_integrate = 1, max_hist=100, converter_fnc=None): PlotWorker.__init__(self, name, parent, n_integrate, converter_fnc) logthread('StripWorker.__init__') self.y = [] for i in range(max_hist): self.y.append(0) self.max_hist = max_hist
def __init__(self, data, parent=None, sleep_nsec=0, filename='data.txt'): QThread.__init__(self, parent) logthread('DumpReader.__init__') self.data = data self.sleep_time = sleep_nsec self.recording = False self.f = None self.filename = filename
def on_start_recording(self): logthread('DumpReader.on_start_recording') if self.f != None: if not self.f.closed: self.f.close() self.f = open(self.filename,'a+') #a+ for appending self.start() self.recording = True
def startNow(self): logthread('Worker.startNow') self.continueWork = True try: self.do_heavy_task() except Exception as e: print(str(e))
def __init__(self): logthread('Worker.__init__') super().__init__() self.continueWork = True self.start.connect(self.startNow) self.process = None
def __init__(self, device, data, parent=None): Reader.__init__(self, device, data, parent) logthread('U12Reader.__init__') # define which channels are used self.ai_channels = [0,2,4,5] # start the thread self.start()
def __init__(self, name, parent=None, show=False, n_integrate = 1, max_hist=100, converter_fnc=None): PlotWidget.__init__(self,name, parent, show) logthread('StripWidget.__init__') self.d = None self.name = name self.worker = StripWorker(self.name, parent, n_integrate, max_hist, converter_fnc) self.worker.moveToThread( self.thread ) self.connect(self.worker, SIGNAL('data'), self.on_draw) self.x = [] for i in range(max_hist): self.x.append(i)
def on_ai_plot_widget(self): logthread('Mainwindow.on_ai_plot_widget') sending_button = self.sender() #name = sending_button.objectName() name = sending_button.text() m = re.match('.*\s(AI\d).*',name) if m != None: name = m.group(1) m = re.match('.*AI(\d).*',name) w = StripWidget(name, parent=None, show=True, n_integrate=1, max_hist=200, converter_fnc=self.ai_channels_converters[int(m.group(1))]) QObject.connect(self, SIGNAL('new_data'),w.worker.new_data) #QObject.connect(self, SIGNAL('quit'),w.worker.close) self.plot_widgets.append(w) else: print('on_ai_plot_widget name:\"' + name + '\" is not a valid AI name?')
def __init__(self, parent=None): logthread('MainWindow.__init__') QMainWindow.__init__(self, parent) self.widget = loadUi('window.ui', self) self.setFixedSize(self.size()) self.thread = QThread() self.thread.started.connect(self.threadStarted) self.thread.finished.connect(self.threadFinished) self.worker = Worker() self.worker.moveToThread(self.thread) self.thread.start() self.worker.qtextedit.connect(self.qtextedit_addtext) self.worker.stop.connect(self.endNow) self.worker.progress.connect(self.updateProgressBar)
def set_ao(self, ch, d): """Set analog output value. Args: ch (int): Channel id (0-1) d (float): analog value to be set. Returns: """ logthread('U12Reader.set_ao ' + str(ch) + ' ' + str(d)) if ch == 0: self.device.eAnalogOut(demo=0, analogOut0=d, analogOut1=-1.0) elif ch == 1: self.device.eAnalogOut(demo=0, analogOut0=-1.0, analogOut1=d) else: raise AnalogOutputError('Channel name is invalid: ' + str(id))
def new_data(self, frame_id ,data): """Process the data and send to GUI when done.""" if frame_id % 500 == 0: logthread('StripWorker.new_data') # look for data using name # I think the keys are QStrings so cant search normally for k,v in data.iteritems(): if k == self.name: self.y.pop(0) if self.converter_fnc != None: v = self.converter_fnc(v) self.y.append(v) self.n += 1 if self.n >= self.n_integrate: self.emit(SIGNAL('data'), frame_id, self.y) self.n = 0
def on_op(self): """Attempt to find operating point.""" # reset QGSET self.textbox_ao['AO1'].setText('-0.2') self.on_ao1_set() # set QRST_T+ to a large value to ensure switch will be closed self.textbox_ao['AO0'].setText('0.5') self.on_ao0_set() logthread('on_op') self.opwThread = OpWorker(self.opw_test, 0, step=0.001) self.opwThread.start() #self.opw = OpWorker(self.opw_test, 1.3) self.connect(self, SIGNAL('new_data'), self.opwThread.new_data) self.connect(self.opwThread, SIGNAL('opw_update'),self.opw_update) self.connect(self.opwThread, SIGNAL('opw_finished'),self.opw_finished)
def run(self): """Read data and emit it.""" logthread('DataReader.run') n = 0 i = 0 time_now = time.time() time_last = time_now dt = 0. while True: # timing stuff time_now = time.time() dt += time_now - time_last time_last = time_now # print some timing stuff if n % 50 == 0 and n > 0: dt /= 50.0 # fraction of the sleep time we get a delay dead_time = (dt/self.sleep_time -1.0)*100 emit_time = dt - self.sleep_time logthread('DataReader.run last flag emitted ' + str(i) + ' total ' + str(n) + ' emit_time ' + str(emit_time*1000) + 'ms dt ' + str(dt) + ' dead_time ' + '{0:.2f}%'.format(dead_time)) # reset dt = 0. self.data.mutex.lock() if self.data.data_flag != i: # new data, emit it d = self.data.get_data() #print('emit ' + str(len(d)) + ' data: ' + str(d)) self.emit( SIGNAL('new_data'), self.data.data_flag, d ) # update flag i = self.data.data_flag n += 1 self.data.mutex.unlock() # sleep a little time.sleep(self.sleep_time)
def run(self): """Read data and dump to file.""" logthread('DumpReader.run') n = 0 i = 0 time_now = time.time() dt = 0. np = 0 while True: if not self.recording: continue time_now = time.time() self.data.mutex.lock() if self.data.data_flag != i: d = self.data.get_data() self.f.write(' '.join(['{0:10.6f}'.format(time_now), str(d),'\n'])) # update flag i = self.data.data_flag n += 1 self.data.mutex.unlock() dt += time.time() - time_now # print some timing stuff if n % 500 == 0 and n > 0 and n != np: dt /= 500.0 wtime = dt - self.sleep_time logthread('DumpReader.run last flag emitted ' + str(i) + ' total ' + str(n) + ' time/write ' + str(wtime*1000) + 'ms') dt = 0. np = n if self.sleep_time != 0: time.sleep(self.sleep_time)
def run(self): """Read data.""" logthread('U12Reader.run') # number of reads n = 0 # timing dt = 0 time_now = 0 while True: # wait until state is correct if self.state != 'Running': #if Reader.debug: logthread('U12Reader thread waiting') self.sleep(1) continue # print some timing stuff if n % 500 == 0: logthread('U12Reader.run average time to read ' + '{0:.3f}ms'.format(dt/500*1000)) dt = 0 # timing stuff time_now = time.time() # read from device result_ao = self.read_ao(differential=True) dt += time.time() - time_now # update data object self.data.set_data(result_ao) #update counter n += 1
def on_pbStop_clicked(self): logthread('MainWindow.on_pbStop_clicked') self.widget.pbrProgress.setRange(0, 1) self.worker.stop.emit()
def set_integration(self,n): logthread('[PlotWidget]: set_integration to ' + str(n)) if self.worker != None: self.worker.set_integration( n ) self.set_integration_text()
def opw_test(self, data): logthread('mainwin.opw_test ' + str(data))
def threadFinished(self): logthread('MainWindow.threadFinished')
def on_record(self): """Record data from the labjack.""" logthread('on_record') self.fileWriter.on_start_recording()
def closeEvent(self, event: QCloseEvent): logthread('MainWindow.closeEvent') self.worker.continueWork = False self.thread.quit() self.thread.wait()
def threadStarted(self): logthread('MainWindow.threadStarted')
def on_actionProject1_triggered(self): logthread('MainWindow.on_actionProject1_triggered') from projectsettings1 import SettingsProject1 dialogSettingsProject1 = SettingsProject1() dialogSettingsProject1.exec_()
def on_actionProject2_triggered(self): logthread('MainWindow.on_actionProject2_triggered') from projectsettings2 import SettingsProject2 dialogSettingsProject2 = SettingsProject2() dialogSettingsProject2.exec_()
def updateProgressBar(self, progress): logthread('MainWindow.updateProgressBar') self.widget.pbrProgress.setValue(progress)
def endNow(self): logthread('MainWindow.endNow') self.worker.continueWork = False
def qtextedit_addtext(self, text): logthread('MainWindow.qtextedit_addtext args-{}'.format(str(text))) self.teErrors.setText(str(text))
def on_record_quit(self): """Stop record data from the labjack.""" logthread('on_record_quit') self.fileWriter.on_quit()
def __init__(self, data, parent=None, sleep_nsec=1.0): QThread.__init__(self, parent) logthread('DataReader.__init__') self.data = data self.sleep_time = sleep_nsec self.start()