+ # signal used to start the worker across threads
+ _start_signal = QtCore.Signal()
+
+ def __init__(self, drivers, loglevel):
+ super(self.__class__, self).__init__()
+
+ self.worker = self.Worker(drivers, loglevel)
+ self.thread = QtCore.QThread()
+ self.worker.moveToThread(self.thread)
+
+ self._start_signal.connect(self.worker.start_sampling)
+
+ # expose the signals of the worker
+ self.measured = self.worker.measured
+ self.error = self.worker.error
+
+ self.thread.start()
+
+ def start(self):
+ '''Starts sampling'''
+ self._start_signal.emit()
+
+ def stop(self):
+ '''Stops sampling and the background thread.'''
+ self.worker.stop_sampling()
+ self.thread.quit()
+ # the timeout is needed when the demo device is used, because it
+ # produces so much outstanding data that quitting takes a long time
+ self.thread.wait(500)
+
+ def sr_pkg_version(self):
+ '''Returns the version number of the libsigrok package.'''
+ return self.worker.sr_pkg_version
+
+ def sr_lib_version(self):
+ '''Returns the version number fo the libsigrok library.'''
+ return self.worker.sr_lib_version
+
+class SigrokMeter(QtGui.QMainWindow):
+ '''The main window of the application.'''
+
+ def __init__(self, thread):
+ super(SigrokMeter, self).__init__()
+ self.setup_ui()
+
+ self.inf = float('inf')
+
+ self.thread = thread
+ self.thread.measured.connect(self.update, QtCore.Qt.QueuedConnection)
+ self.thread.error.connect(self.error)
+ self.thread.start()
+
+ def setup_ui(self):
+ self.setWindowTitle('sigrok-meter')
+ self.setMinimumHeight(130)
+ self.setMinimumWidth(260)
+
+ p = os.path.abspath(os.path.dirname(__file__))
+ p = os.path.join(p, 'sigrok-logo-notext.png')
+ self.setWindowIcon(QtGui.QIcon(p))
+
+ actionQuit = QtGui.QAction(self)
+ actionQuit.setText('&Quit')
+ actionQuit.setIcon(QtGui.QIcon.fromTheme('application-exit'))
+ actionQuit.setShortcut('Ctrl+Q')
+ actionQuit.triggered.connect(self.close)
+
+ actionAbout = QtGui.QAction(self)
+ actionAbout.setText('&About')
+ actionAbout.setIcon(QtGui.QIcon.fromTheme('help-about'))
+ actionAbout.triggered.connect(self.show_about)
+
+ menubar = self.menuBar()
+ menuFile = menubar.addMenu('&File')
+ menuFile.addAction(actionQuit)
+ menuHelp = menubar.addMenu('&Help')
+ menuHelp.addAction(actionAbout)
+
+ self.lblValue = QtGui.QLabel('waiting for data...')
+ self.lblValue.setAlignment(QtCore.Qt.AlignCenter)
+ font = self.lblValue.font()
+ font.setPointSize(font.pointSize() * 1.7)
+ font.setBold(True)
+ self.lblValue.setFont(font)
+ self.setCentralWidget(self.lblValue)
+ self.centralWidget().setContentsMargins(0, 0, 0, 0)
+
+ self.lblDevName = QtGui.QLabel()
+ self.lblDevName.setToolTip('Name of used measurement device.')
+ self.statusBar().insertWidget(0, self.lblDevName, 10)
+ self.lblTime = QtGui.QLabel()
+ self.lblTime.setToolTip('Time of the last measurement.')
+ self.statusBar().insertWidget(1, self.lblTime)
+
+ self.statusBar().setSizeGripEnabled(False)
+
+ @QtCore.Slot()
+ def show_about(self):
+ text = textwrap.dedent('''\
+ <div align="center">
+ <b>sigrok-meter</b><br/>
+ 0.1.0<br/>
+ Using libsigrok {} (lib version {}).<br/>
+ <a href='http://www.sigrok.org'>
+ http://www.sigrok.org</a><br/>
+ <br/>
+ This program comes with ABSOLUTELY NO WARRANTY;<br/>
+ for details visit
+ <a href='http://www.gnu.org/licenses/gpl.html'>
+ http://www.gnu.org/licenses/gpl.html</a>
+ </div>
+ '''.format(self.thread.sr_pkg_version(), self.thread.sr_lib_version()))
+
+ QtGui.QMessageBox.about(self, 'About sigrok-meter', text)
+
+ def format_unit(self, u):
+ units = {
+ sr.Unit.VOLT: 'V',
+ sr.Unit.AMPERE: 'A',
+ sr.Unit.OHM: u'\u03A9',
+ sr.Unit.FARAD: 'F',
+ sr.Unit.KELVIN: 'K',
+ sr.Unit.CELSIUS: u'\u00B0C',
+ sr.Unit.FAHRENHEIT: u'\u00B0F',
+ sr.Unit.HERTZ: 'Hz',
+ sr.Unit.PERCENTAGE: '%',
+ # sr.Unit.BOOLEAN
+ sr.Unit.SECOND: 's',
+ sr.Unit.SIEMENS: 'S',
+ sr.Unit.DECIBEL_MW: 'dBu',
+ sr.Unit.DECIBEL_VOLT: 'dBV',
+ # sr.Unit.UNITLESS
+ sr.Unit.DECIBEL_SPL: 'dB',
+ # sr.Unit.CONCENTRATION
+ sr.Unit.REVOLUTIONS_PER_MINUTE: 'rpm',
+ sr.Unit.VOLT_AMPERE: 'VA',
+ sr.Unit.WATT: 'W',
+ sr.Unit.WATT_HOUR: 'Wh',
+ sr.Unit.METER_SECOND: 'm/s',
+ sr.Unit.HECTOPASCAL: 'hPa',
+ sr.Unit.HUMIDITY_293K: '%rF',
+ sr.Unit.DEGREE: u'\u00B0',
+ sr.Unit.HENRY: 'H'
+ }
+
+ return units.get(u, '')
+
+ def format_mqflags(self, mqflags):
+ if sr.QuantityFlag.AC in mqflags:
+ s = 'AC'
+ elif sr.QuantityFlag.DC in mqflags:
+ s = 'DC'
+ else:
+ s = ''
+
+ return s
+
+ def format_mag(self, mag):
+ if mag == self.inf:
+ return u'\u221E'
+ return '{:f}'.format(mag)
+
+ @QtCore.Slot(object)
+ def update(self, data):
+ '''Updates the labels with new measurement values.'''
+
+ device, mag, unit, mqflags = data
+
+ unit_str = self.format_unit(unit)
+ mqflags_str = self.format_mqflags(mqflags)
+ mag_str = self.format_mag(mag)
+ value = ' '.join([mag_str, unit_str, mqflags_str])
+
+ n = datetime.datetime.now().time()
+ now = '{:02}:{:02}:{:02}.{:03}'.format(
+ n.hour, n.minute, n.second, n.microsecond / 1000)
+
+ self.lblValue.setText(value)
+ self.lblDevName.setText(device)
+ self.lblTime.setText(now)
+
+ @QtCore.Slot(str)
+ def error(self, msg):
+ '''Error handler for the sampling thread.'''
+ QtGui.QMessageBox.critical(self, 'Error', msg)
+ self.close()