2 ## This file is part of the sigrok-meter project.
4 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 ## Copyright (C) 2014 Jens Steinhauser <jens.steinhauser@gmail.com>
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 import sigrok.core as sr
26 QtCore = qtcompat.QtCore
27 QtGui = qtcompat.QtGui
29 class SamplingThread(QtCore.QObject):
30 '''A class that handles the reception of sigrok packets in the background.'''
32 class Worker(QtCore.QObject):
33 '''Helper class that does the actual work in another thread.'''
35 '''Signal emitted when new data arrived.'''
36 measured = QtCore.Signal(object, object, object)
38 '''Signal emmited in case of an error.'''
39 error = QtCore.Signal(str)
41 def __init__(self, context, drivers):
42 super(self.__class__, self).__init__()
44 self.context = context
45 self.drivers = drivers
49 def parse_driverstring(self, ds):
50 '''Dissects the driver string and returns a tuple consiting of
51 the driver name and the options (as a dictionary).'''
53 def parse_option(k, v):
54 '''Parse the value for a single option.'''
56 ck = sr.ConfigKey.get_by_identifier(k)
58 raise ValueError('No option named "{}".'.format(k))
61 val = ck.parse_string(v)
64 'Invalid value "{}" for option "{}"'.format(v, k))
68 m = re.match('(?P<name>[^:]+)(?P<opts>(:[^:=]+=[^:=]+)*)$', ds)
70 raise ValueError('"{}" is not a valid driver string.'.format(ds))
72 opts = m.group('opts').split(':')[1:]
73 opts = [tuple(kv.split('=')) for kv in opts]
74 opts = [parse_option(k, v) for (k, v) in opts]
77 return (m.group('name'), opts)
80 def start_sampling(self):
82 for ds in self.drivers:
84 (name, opts) = self.parse_driverstring(ds)
85 if not name in self.context.drivers:
86 raise RuntimeError('No driver called "{}".'.format(name))
88 driver = self.context.drivers[name]
89 devs = driver.scan(**opts)
91 raise RuntimeError('No devices found.')
92 devices.append(devs[0])
93 except Exception as e:
95 'Error processing driver string:\n{}'.format(e))
98 self.session = self.context.create_session()
100 self.session.add_device(dev)
102 self.session.add_datafeed_callback(self.callback)
107 # If sampling is 'True' here, it means that 'stop_sampling()' was
108 # not called, therefore 'session.run()' ended too early, indicating
111 self.error.emit('An error occured during the acquisition.')
113 def stop_sampling(self):
115 self.sampling = False
118 def callback(self, device, packet):
120 # In rare cases it can happen that the callback fires while
121 # the interpreter is shutting down. Then the sigrok module
122 # is already set to 'None'.
125 if packet.type != sr.PacketType.ANALOG:
128 if not len(packet.payload.channels):
131 # TODO: find a device with multiple channels in one packet
132 channel = packet.payload.channels[0]
134 # the most recent value
135 value = packet.payload.data[0][-1]
137 self.measured.emit(device, channel,
138 (value, packet.payload.unit, packet.payload.mq_flags))
140 # signal used to start the worker across threads
141 _start_signal = QtCore.Signal()
143 def __init__(self, context, drivers):
144 super(self.__class__, self).__init__()
146 self.worker = self.Worker(context, drivers)
147 self.thread = QtCore.QThread()
148 self.worker.moveToThread(self.thread)
150 self._start_signal.connect(self.worker.start_sampling)
152 # expose the signals of the worker
153 self.measured = self.worker.measured
154 self.error = self.worker.error
159 '''Starts sampling'''
160 self._start_signal.emit()
163 '''Stops sampling and the background thread.'''
164 self.worker.stop_sampling()