From: Jens Steinhauser Date: Wed, 19 Nov 2014 19:05:07 +0000 (+0100) Subject: Add a '--config' command line option. X-Git-Url: http://sigrok.org/gitweb/?p=sigrok-meter.git;a=commitdiff_plain;h=739a1d5425d336cb9dc5dba6bd5f2fb42b665ab6 Add a '--config' command line option. --- diff --git a/samplingthread.py b/samplingthread.py index d04d120..2c68622 100644 --- a/samplingthread.py +++ b/samplingthread.py @@ -46,9 +46,9 @@ class SamplingThread(QtCore.QObject): self.sampling = False - def parse_driverstring(self, ds): - '''Dissects the driver string and returns a tuple consiting of - the driver name and the options (as a dictionary).''' + def parse_configstring(self, cs): + '''Dissects a config string and returns the options as a + dictionary.''' def parse_option(k, v): '''Parse the value for a single option.''' @@ -61,25 +61,38 @@ class SamplingThread(QtCore.QObject): val = ck.parse_string(v) except: raise ValueError( - 'Invalid value "{}" for option "{}"'.format(v, k)) + 'Invalid value "{}" for option "{}".'.format(v, k)) return (k, val) - m = re.match('(?P[^:]+)(?P(:[^:=]+=[^:=]+)*)$', ds) - if not m: - raise ValueError('"{}" is not a valid driver string.'.format(ds)) + if not re.match('(([^:=]+=[^:=]+)(:[^:=]+=[^:=]+)*)?$', cs): + raise ValueError( + '"{}" is not a valid configuration string.'.format(cs)) - opts = m.group('opts').split(':')[1:] + if not cs: + return {} + + opts = cs.split(':') opts = [tuple(kv.split('=')) for kv in opts] opts = [parse_option(k, v) for (k, v) in opts] - opts = dict(opts) + return dict(opts) + + def parse_driverstring(self, ds): + '''Dissects the driver string and returns a tuple consiting of + the driver name and the options (as a dictionary).''' - return (m.group('name'), opts) + m = re.match('(?P[^:]+)(?P(:[^:=]+=[^:=]+)*)$', ds) + if not m: + raise ValueError('"{}" is not a valid driver string.'.format(ds)) + + opts = m.group('opts')[1:] + return (m.group('name'), self.parse_configstring(opts)) @QtCore.Slot() def start_sampling(self): devices = [] - for ds in self.drivers: + for (ds, cs) in self.drivers: + # process driver string try: (name, opts) = self.parse_driverstring(ds) if not name in self.context.drivers: @@ -89,12 +102,25 @@ class SamplingThread(QtCore.QObject): devs = driver.scan(**opts) if not devs: raise RuntimeError('No devices found.') - devices.append(devs[0]) + + device = devs[0] except Exception as e: self.error.emit( 'Error processing driver string:\n{}'.format(e)) return + # process configuration string + try: + cfgs = self.parse_configstring(cs) + for k, v in cfgs.items(): + device.config_set(sr.ConfigKey.get_by_identifier(k), v) + except Exception as e: + self.error.emit( + 'Error processing configuration string:\n{}'.format(e)) + return + + devices.append(device) + self.session = self.context.create_session() for dev in devices: self.session.add_device(dev) diff --git a/sigrok-meter b/sigrok-meter index 546c8b2..ffda947 100755 --- a/sigrok-meter +++ b/sigrok-meter @@ -26,26 +26,36 @@ import sigrok.core as sr import sys import textwrap -default_drivers = ['demo:analog_channels=4'] +default_drivers = [('demo:analog_channels=4', 'samplerate=4')] default_loglevel = 2 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). + The DRIVER string is the same as for sigrok-cli(1). The nth + CONFIG is applied to the nth DRIVER. If there are more drivers + than configs, the remaining drivers use the default configuration. examples: %(prog)s --driver tecpel-dmm-8061-ser:conn=/dev/ttyUSB0 %(prog)s --driver uni-t-ut61e:conn=1a86.e008 + + %(prog)s --driver demo:analog_channels=1 \\ + --config samplerate=10 '''), formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-d', '--driver', action='append', + default=[], help='The driver to use') + parser.add_argument('-c', '--config', + action='append', + default=[], + help='Specify device configuration options') parser.add_argument('-l', '--loglevel', type=int, default=default_loglevel, @@ -56,8 +66,19 @@ def parse_cli(): help='Force use of PySide (default is to use PyQt4)') args = parser.parse_args() + if len(args.config) > len(args.driver): + sys.exit('error: more configurations than drivers given') + + # merge drivers and configurations into a list of tuples + setattr(args, 'drivers', []) if not args.driver: - args.driver = default_drivers + args.drivers = default_drivers + sys.stderr.write('no driver given, using demo driver\n') + if args.driver: + args.config.extend([''] * (len(args.driver) - len(args.config))) + args.drivers = zip(args.driver, args.config) + del args.driver + del args.config return args @@ -78,7 +99,7 @@ if __name__ == '__main__': sys.exit('error: invalid log level') app = QtGui.QApplication([]) - s = mainwindow.MainWindow(context, args.driver) + s = mainwindow.MainWindow(context, args.drivers) s.show() sys.exit(app.exec_())