X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=samplingthread.py;h=fa95f424c43d5bcaef3f5cbefa7e332a7fc9bcb9;hb=bb56e1c66bdc7a33971667bd622477c9cc70fc2b;hp=2c686223c0e81c9373c0e1e8c1ba9a609ebfa3b2;hpb=739a1d5425d336cb9dc5dba6bd5f2fb42b665ab6;p=sigrok-meter.git diff --git a/samplingthread.py b/samplingthread.py index 2c68622..fa95f42 100644 --- a/samplingthread.py +++ b/samplingthread.py @@ -22,18 +22,19 @@ import qtcompat import re import sigrok.core as sr +import time QtCore = qtcompat.QtCore QtGui = qtcompat.QtGui class SamplingThread(QtCore.QObject): - '''A class that handles the reception of sigrok packets in the background.''' + '''Class that handles the reception of sigrok packets in the background.''' class Worker(QtCore.QObject): '''Helper class that does the actual work in another thread.''' '''Signal emitted when new data arrived.''' - measured = QtCore.Signal(object, object, object) + measured = QtCore.Signal(float, sr.classes.Device, sr.classes.Channel, tuple) '''Signal emmited in case of an error.''' error = QtCore.Signal(str) @@ -47,7 +48,7 @@ class SamplingThread(QtCore.QObject): self.sampling = False def parse_configstring(self, cs): - '''Dissects a config string and returns the options as a + '''Dissect a config string and return the options as a dictionary.''' def parse_option(k, v): @@ -78,7 +79,7 @@ class SamplingThread(QtCore.QObject): return dict(opts) def parse_driverstring(self, ds): - '''Dissects the driver string and returns a tuple consiting of + '''Dissect the driver string and return a tuple consisting of the driver name and the options (as a dictionary).''' m = re.match('(?P[^:]+)(?P(:[^:=]+=[^:=]+)*)$', ds) @@ -92,11 +93,11 @@ class SamplingThread(QtCore.QObject): def start_sampling(self): devices = [] for (ds, cs) in self.drivers: - # process driver string + # Process driver string. try: (name, opts) = self.parse_driverstring(ds) if not name in self.context.drivers: - raise RuntimeError('No driver called "{}".'.format(name)) + raise RuntimeError('No driver named "{}".'.format(name)) driver = self.context.drivers[name] devs = driver.scan(**opts) @@ -109,7 +110,7 @@ class SamplingThread(QtCore.QObject): 'Error processing driver string:\n{}'.format(e)) return - # process configuration string + # Process configuration string. try: cfgs = self.parse_configstring(cs) for k, v in cfgs.items(): @@ -142,6 +143,8 @@ class SamplingThread(QtCore.QObject): self.session.stop() def callback(self, device, packet): + now = time.time() + if not sr: # In rare cases it can happen that the callback fires while # the interpreter is shutting down. Then the sigrok module @@ -157,13 +160,13 @@ class SamplingThread(QtCore.QObject): # TODO: find a device with multiple channels in one packet channel = packet.payload.channels[0] - # the most recent value + # The most recent value. value = packet.payload.data[0][-1] - self.measured.emit(device, channel, + self.measured.emit(now, device, channel, (value, packet.payload.unit, packet.payload.mq_flags)) - # signal used to start the worker across threads + # Signal used to start the worker across threads. _start_signal = QtCore.Signal() def __init__(self, context, drivers): @@ -175,18 +178,18 @@ class SamplingThread(QtCore.QObject): self._start_signal.connect(self.worker.start_sampling) - # expose the signals of the worker + # Expose the signals of the worker. self.measured = self.worker.measured self.error = self.worker.error self.thread.start() def start(self): - '''Starts sampling''' + '''Start sampling.''' self._start_signal.emit() def stop(self): - '''Stops sampling and the background thread.''' + '''Stop sampling and stop the background thread.''' self.worker.stop_sampling() self.thread.quit() self.thread.wait()