]>
Commit | Line | Data |
---|---|---|
d8f6e041 | 1 | ## |
50985c20 | 2 | ## This file is part of the libsigrok project. |
d8f6e041 ML |
3 | ## |
4 | ## Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li> | |
5 | ## | |
6 | ## This program is free software: you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation, either version 3 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | from functools import partial | |
21 | from fractions import Fraction | |
1cad2115 ML |
22 | from .lowlevel import * |
23 | from . import lowlevel | |
d8f6e041 ML |
24 | import itertools |
25 | ||
9bbd6a6a ML |
26 | __all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet', 'Log', |
27 | 'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag'] | |
d8f6e041 ML |
28 | |
29 | class Error(Exception): | |
30 | ||
31 | def __str__(self): | |
32 | return sr_strerror(self.args[0]) | |
33 | ||
34 | def check(result): | |
35 | if result != SR_OK: | |
36 | raise Error(result) | |
37 | ||
38 | def config_key(name): | |
39 | if not name.lower() == name: | |
40 | raise AttributeError | |
41 | key_name = "SR_CONF_" + name.upper() | |
42 | return getattr(lowlevel, key_name) | |
43 | ||
44 | def gvariant_to_python(value): | |
45 | type_string = g_variant_get_type_string(value) | |
46 | if type_string == 't': | |
47 | return g_variant_get_uint64(value) | |
48 | if type_string == 'b': | |
49 | return g_variant_get_bool(value) | |
50 | if type_string == 'd': | |
51 | return g_variant_get_double(value) | |
52 | if type_string == 's': | |
53 | return g_variant_get_string(value, None) | |
54 | if type_string == '(tt)': | |
55 | return Fraction( | |
56 | g_variant_get_uint64(g_variant_get_child_value(value, 0)), | |
57 | g_variant_get_uint64(g_variant_get_child_value(value, 1))) | |
1cad2115 | 58 | raise NotImplementedError( |
d8f6e041 ML |
59 | "Can't convert GVariant type '%s' to a Python type." % type_string) |
60 | ||
61 | def python_to_gvariant(value): | |
62 | if isinstance(value, int): | |
63 | return g_variant_new_uint64(value) | |
64 | if isinstance(value, bool): | |
65 | return g_variant_new_boolean(value) | |
66 | if isinstance(value, float): | |
67 | return g_variant_new_double(value) | |
68 | if isinstance(value, str): | |
69 | return g_variant_new_string(value) | |
70 | if isinstance(value, Fraction): | |
71 | array = new_gvariant_ptr_array(2) | |
1e2bd8af ML |
72 | gvariant_ptr_array_setitem(array, 0, |
73 | g_variant_new_uint64(value.numerator)) | |
74 | gvariant_ptr_array_setitem(array, 1, | |
75 | g_variant_new_uint64(value.denominator)) | |
d8f6e041 ML |
76 | result = g_variant_new_tuple(array, 2) |
77 | delete_gvariant_ptr_array(array) | |
78 | return result | |
1cad2115 | 79 | raise NotImplementedError( |
d8f6e041 ML |
80 | "Can't convert Python '%s' to a GVariant." % type(value)) |
81 | ||
82 | def callback_wrapper(session, callback, device_ptr, packet_ptr): | |
83 | device = session.context._devices[int(device_ptr.this)] | |
84 | packet = Packet(session, packet_ptr) | |
85 | callback(device, packet) | |
86 | ||
87 | class Context(object): | |
88 | ||
89 | def __init__(self): | |
90 | context_ptr_ptr = new_sr_context_ptr_ptr() | |
91 | check(sr_init(context_ptr_ptr)) | |
92 | self.struct = sr_context_ptr_ptr_value(context_ptr_ptr) | |
93 | self._drivers = None | |
94 | self._devices = {} | |
95 | self.session = None | |
96 | ||
97 | def __del__(self): | |
98 | sr_exit(self.struct) | |
99 | ||
100 | @property | |
101 | def drivers(self): | |
102 | if not self._drivers: | |
103 | self._drivers = {} | |
104 | driver_list = sr_driver_list() | |
105 | for i in itertools.count(): | |
106 | driver_ptr = sr_dev_driver_ptr_array_getitem(driver_list, i) | |
107 | if driver_ptr: | |
108 | self._drivers[driver_ptr.name] = Driver(self, driver_ptr) | |
109 | else: | |
110 | break | |
111 | return self._drivers | |
112 | ||
113 | class Driver(object): | |
114 | ||
115 | def __init__(self, context, struct): | |
116 | self.context = context | |
117 | self.struct = struct | |
118 | self._initialized = False | |
119 | ||
120 | @property | |
121 | def name(self): | |
122 | return self.struct.name | |
123 | ||
124 | def scan(self): | |
125 | if not self._initialized: | |
126 | check(sr_driver_init(self.context.struct, self.struct)) | |
127 | self._initialized = True | |
d8f6e041 | 128 | device_list = sr_driver_scan(self.struct, None) |
05cfe114 ML |
129 | devices = [Device(self, gpointer_to_sr_dev_inst_ptr(ptr)) |
130 | for ptr in gslist_to_python(device_list)] | |
d8f6e041 ML |
131 | g_slist_free(device_list) |
132 | return devices | |
133 | ||
134 | class Device(object): | |
135 | ||
136 | def __new__(cls, driver, struct): | |
137 | address = int(struct.this) | |
138 | if address not in driver.context._devices: | |
139 | device = super(Device, cls).__new__(cls, driver, struct) | |
140 | driver.context._devices[address] = device | |
141 | return driver.context._devices[address] | |
142 | ||
143 | def __init__(self, driver, struct): | |
144 | self.driver = driver | |
145 | self.struct = struct | |
146 | ||
147 | def __getattr__(self, name): | |
148 | key = config_key(name) | |
149 | data = new_gvariant_ptr_ptr() | |
150 | try: | |
151 | check(sr_config_get(self.driver.struct, key, data, self.struct)) | |
152 | except Error as error: | |
0021b077 | 153 | if error.errno == SR_ERR_NA: |
1cad2115 | 154 | raise NotImplementedError( |
d8f6e041 ML |
155 | "Device does not implement %s" % name) |
156 | else: | |
157 | raise AttributeError | |
158 | value = gvariant_ptr_ptr_value(data) | |
159 | return gvariant_to_python(value) | |
160 | ||
161 | def __setattr__(self, name, value): | |
162 | try: | |
163 | key = config_key(name) | |
164 | except AttributeError: | |
165 | super(Device, self).__setattr__(name, value) | |
166 | return | |
167 | check(sr_config_set(self.struct, key, python_to_gvariant(value))) | |
168 | ||
169 | @property | |
170 | def vendor(self): | |
171 | return self.struct.vendor | |
172 | ||
173 | @property | |
174 | def model(self): | |
175 | return self.struct.model | |
176 | ||
177 | @property | |
178 | def version(self): | |
179 | return self.struct.version | |
180 | ||
181 | class Session(object): | |
182 | ||
183 | def __init__(self, context): | |
184 | assert context.session is None | |
185 | self.context = context | |
186 | self.struct = sr_session_new() | |
187 | context.session = self | |
188 | ||
189 | def __del__(self): | |
190 | check(sr_session_destroy()) | |
191 | ||
192 | def add_device(self, device): | |
193 | check(sr_session_dev_add(device.struct)) | |
194 | ||
195 | def add_callback(self, callback): | |
196 | wrapper = partial(callback_wrapper, self, callback) | |
197 | check(sr_session_datafeed_python_callback_add(wrapper)) | |
198 | ||
199 | def start(self): | |
200 | check(sr_session_start()) | |
201 | ||
202 | def run(self): | |
203 | check(sr_session_run()) | |
204 | ||
205 | def stop(self): | |
206 | check(sr_session_stop()) | |
207 | ||
208 | class Packet(object): | |
209 | ||
210 | def __init__(self, session, struct): | |
211 | self.session = session | |
212 | self.struct = struct | |
213 | self._payload = None | |
214 | ||
215 | @property | |
216 | def type(self): | |
9bbd6a6a | 217 | return PacketType(self.struct.type) |
d8f6e041 ML |
218 | |
219 | @property | |
220 | def payload(self): | |
221 | if self._payload is None: | |
222 | pointer = self.struct.payload | |
9bbd6a6a | 223 | if self.type == PacketType.LOGIC: |
d8f6e041 ML |
224 | self._payload = Logic(self, |
225 | void_ptr_to_sr_datafeed_logic_ptr(pointer)) | |
9bbd6a6a | 226 | elif self.type == PacketType.ANALOG: |
15574a3c ML |
227 | self._payload = Analog(self, |
228 | void_ptr_to_sr_datafeed_analog_ptr(pointer)) | |
d8f6e041 | 229 | else: |
1cad2115 | 230 | raise NotImplementedError( |
d8f6e041 ML |
231 | "No Python mapping for packet type %ѕ" % self.struct.type) |
232 | return self._payload | |
233 | ||
234 | class Logic(object): | |
235 | ||
236 | def __init__(self, packet, struct): | |
237 | self.packet = packet | |
238 | self.struct = struct | |
239 | self._data = None | |
240 | ||
241 | @property | |
242 | def data(self): | |
243 | if self._data is None: | |
244 | self._data = cdata(self.struct.data, self.struct.length) | |
245 | return self._data | |
246 | ||
15574a3c ML |
247 | class Analog(object): |
248 | ||
249 | def __init__(self, packet, struct): | |
250 | self.packet = packet | |
251 | self.struct = struct | |
252 | self._data = None | |
253 | ||
254 | @property | |
255 | def num_samples(self): | |
256 | return self.struct.num_samples | |
257 | ||
c2ec42ce UH |
258 | @property |
259 | def mq(self): | |
9bbd6a6a | 260 | return Quantity(self.struct.mq) |
c2ec42ce UH |
261 | |
262 | @property | |
263 | def unit(self): | |
9bbd6a6a | 264 | return Unit(self.struct.unit) |
c2ec42ce UH |
265 | |
266 | @property | |
267 | def mqflags(self): | |
9bbd6a6a | 268 | return QuantityFlag.set_from_mask(self.struct.mqflags) |
c2ec42ce | 269 | |
15574a3c ML |
270 | @property |
271 | def data(self): | |
272 | if self._data is None: | |
273 | self._data = float_array.frompointer(self.struct.data) | |
274 | return self._data | |
275 | ||
816aed6c UH |
276 | class Log(object): |
277 | ||
816aed6c UH |
278 | @property |
279 | def level(self): | |
9bbd6a6a | 280 | return LogLevel(sr_log_loglevel_get()) |
816aed6c UH |
281 | |
282 | @level.setter | |
283 | def level(self, l): | |
9bbd6a6a | 284 | check(sr_log_loglevel_set(l.id)) |
816aed6c UH |
285 | |
286 | @property | |
287 | def domain(self): | |
288 | return sr_log_logdomain_get() | |
289 | ||
290 | @domain.setter | |
291 | def domain(self, d): | |
292 | check(sr_log_logdomain_set(d)) | |
293 | ||
9bbd6a6a ML |
294 | class EnumValue(object): |
295 | ||
296 | _enum_values = {} | |
297 | ||
298 | def __new__(cls, id): | |
299 | if cls not in cls._enum_values: | |
300 | cls._enum_values[cls] = {} | |
301 | if id not in cls._enum_values[cls]: | |
302 | value = super(EnumValue, cls).__new__(cls) | |
303 | value.id = id | |
304 | cls._enum_values[cls][id] = value | |
305 | return cls._enum_values[cls][id] | |
306 | ||
307 | class LogLevel(EnumValue): | |
308 | pass | |
309 | ||
310 | class PacketType(EnumValue): | |
311 | pass | |
312 | ||
313 | class Quantity(EnumValue): | |
314 | pass | |
315 | ||
316 | class Unit(EnumValue): | |
317 | pass | |
318 | ||
319 | class QuantityFlag(EnumValue): | |
320 | ||
321 | @classmethod | |
322 | def set_from_mask(cls, mask): | |
323 | result = set() | |
324 | while mask: | |
325 | new_mask = mask & (mask - 1) | |
326 | result.add(cls(mask ^ new_mask)) | |
327 | mask = new_mask | |
328 | return result | |
329 | ||
d8f6e041 | 330 | for symbol_name in dir(lowlevel): |
9bbd6a6a ML |
331 | for prefix, cls in [ |
332 | ('SR_LOG_', LogLevel), | |
333 | ('SR_DF_', PacketType), | |
334 | ('SR_MQ_', Quantity), | |
335 | ('SR_UNIT_', Unit), | |
336 | ('SR_MQFLAG_', QuantityFlag)]: | |
337 | if symbol_name.startswith(prefix): | |
338 | name = symbol_name[len(prefix):] | |
339 | value = getattr(lowlevel, symbol_name) | |
340 | setattr(cls, name, cls(value)) |