import argparse
import datetime
import os.path
-import PyQt4.QtCore as QtCore
-import PyQt4.QtGui as QtGui
import re
import sigrok.core as sr
import sys
default_drivers = [('demo', {'analog_channels': 1})]
default_loglevel = sr.LogLevel.WARN
+def parse_cli():
+ parser = argparse.ArgumentParser(
+ description='Simple sigrok GUI for multimeters and dataloggers.',
+ epilog=textwrap.dedent('''\
+ The DRIVER string is the same as for sigrok-cli(1).
+
+ examples:
+
+ %(prog)s --driver tecpel-dmm-8061-ser:conn=/dev/ttyUSB0
+
+ %(prog)s --driver uni-t-ut61e:conn=1a86.e008
+ '''),
+ formatter_class=argparse.RawDescriptionHelpFormatter)
+
+ parser.add_argument('-d', '--driver',
+ action='append',
+ help='The driver to use')
+ parser.add_argument('-l', '--loglevel',
+ type=int,
+ help='Set loglevel (5 is most verbose)')
+ parser.add_argument('--pyside',
+ action='store_true',
+ default=False,
+ help='Force use of PySide (default is to use PyQt4)')
+ args = parser.parse_args()
+
+ result = {
+ 'drivers': default_drivers,
+ 'loglevel': default_loglevel,
+ 'pyside': args.pyside
+ }
+
+ if args.driver:
+ result['drivers'] = []
+ for d in args.driver:
+ m = re.match('(?P<name>[^:]+)(?P<opts>(:[^:=]+=[^:=]+)*)', d)
+ if not m:
+ sys.exit('error parsing option "{}"'.format(d))
+
+ opts = m.group('opts').split(':')[1:]
+ opts = [tuple(kv.split('=')) for kv in opts]
+ opts = dict(opts)
+
+ result['drivers'].append((m.group('name'), opts))
+
+ if args.loglevel != None:
+ try:
+ result['loglevel'] = sr.LogLevel.get(args.loglevel)
+ except:
+ sys.exit('error: invalid log level')
+
+ return result
+
+if __name__ == '__main__':
+ # The command line parsing and import of the Qt modules is done here,
+ # so that the modules are imported before the classes below derive
+ # from classes therein. The rest of the main function is at the bottom.
+
+ args = parse_cli()
+
+ global qt_signal
+
+ if args['pyside']:
+ from PySide import QtCore, QtGui
+ qt_signal = QtCore.Signal
+ else:
+ try:
+ from PyQt4 import QtCore, QtGui
+ qt_signal = QtCore.pyqtSignal
+ except:
+ sys.stderr.write('import of PyQt4 failed, using PySide\n')
+ from PySide import QtCore, QtGui
+ qt_signal = QtCore.Signal
+
class SamplingThread(QtCore.QObject):
'''A class that handles the reception of sigrok packets in the background.'''
'''Helper class that does the actual work in another thread.'''
'''Signal emitted when new data arrived.'''
- measured = QtCore.pyqtSignal(object)
+ measured = qt_signal(object)
def __init__(self, drivers, loglevel):
super(self.__class__, self).__init__()
self.thread().msleep(100)
# signal used to start the worker across threads
- _start_signal = QtCore.pyqtSignal()
+ _start_signal = qt_signal()
def __init__(self, drivers, loglevel):
super(self.__class__, self).__init__()
self.lblDevName.setText(device)
self.lblTime.setText(now)
-def parse_cli():
- parser = argparse.ArgumentParser(
- description='Simple sigrok GUI for multimeters and dataloggers.',
- epilog=textwrap.dedent('''\
- The DRIVER string is the same as for sigrok-cli(1).
-
- examples:
-
- %(prog)s --driver tecpel-dmm-8061-ser:conn=/dev/ttyUSB0
-
- %(prog)s --driver uni-t-ut61e:conn=1a86.e008
- '''),
- formatter_class=argparse.RawDescriptionHelpFormatter)
-
- parser.add_argument('-d', '--driver',
- action='append',
- help='The driver to use')
- parser.add_argument('-l', '--loglevel',
- type=int,
- help='Set loglevel (5 is most verbose)')
- args = parser.parse_args()
-
- result = {
- 'drivers': default_drivers,
- 'loglevel': default_loglevel
- }
-
- if args.driver:
- result['drivers'] = []
- for d in args.driver:
- m = re.match('(?P<name>[^:]+)(?P<opts>(:[^:=]+=[^:=]+)*)', d)
- if not m:
- sys.exit('error parsing option "{}"'.format(d))
-
- opts = m.group('opts').split(':')[1:]
- opts = [tuple(kv.split('=')) for kv in opts]
- opts = dict(opts)
-
- result['drivers'].append((m.group('name'), opts))
-
- if args.loglevel != None:
- try:
- result['loglevel'] = sr.LogLevel.get(args.loglevel)
- except:
- sys.exit('error: invalid log level')
-
- return result
-
if __name__ == '__main__':
- args = parse_cli()
-
thread = SamplingThread(args['drivers'], args['loglevel'])
app = QtGui.QApplication([])