2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
20 from functools import partial
21 from fractions import Fraction
22 from .lowlevel import *
23 from . import lowlevel
26 __all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet']
28 class Error(Exception):
31 return sr_strerror(self.args[0])
38 if not name.lower() == name:
40 key_name = "SR_CONF_" + name.upper()
41 return getattr(lowlevel, key_name)
43 def gvariant_to_python(value):
44 type_string = g_variant_get_type_string(value)
45 if type_string == 't':
46 return g_variant_get_uint64(value)
47 if type_string == 'b':
48 return g_variant_get_bool(value)
49 if type_string == 'd':
50 return g_variant_get_double(value)
51 if type_string == 's':
52 return g_variant_get_string(value, None)
53 if type_string == '(tt)':
55 g_variant_get_uint64(g_variant_get_child_value(value, 0)),
56 g_variant_get_uint64(g_variant_get_child_value(value, 1)))
57 raise NotImplementedError(
58 "Can't convert GVariant type '%s' to a Python type." % type_string)
60 def python_to_gvariant(value):
61 if isinstance(value, int):
62 return g_variant_new_uint64(value)
63 if isinstance(value, bool):
64 return g_variant_new_boolean(value)
65 if isinstance(value, float):
66 return g_variant_new_double(value)
67 if isinstance(value, str):
68 return g_variant_new_string(value)
69 if isinstance(value, Fraction):
70 array = new_gvariant_ptr_array(2)
71 gvariant_ptr_array_setitem(array, 0, value.numerator)
72 gvariant_ptr_array_setitem(array, 1, value.denominator)
73 result = g_variant_new_tuple(array, 2)
74 delete_gvariant_ptr_array(array)
76 raise NotImplementedError(
77 "Can't convert Python '%s' to a GVariant." % type(value))
79 def callback_wrapper(session, callback, device_ptr, packet_ptr):
80 device = session.context._devices[int(device_ptr.this)]
81 packet = Packet(session, packet_ptr)
82 callback(device, packet)
84 class Context(object):
87 context_ptr_ptr = new_sr_context_ptr_ptr()
88 check(sr_init(context_ptr_ptr))
89 self.struct = sr_context_ptr_ptr_value(context_ptr_ptr)
101 driver_list = sr_driver_list()
102 for i in itertools.count():
103 driver_ptr = sr_dev_driver_ptr_array_getitem(driver_list, i)
105 self._drivers[driver_ptr.name] = Driver(self, driver_ptr)
110 class Driver(object):
112 def __init__(self, context, struct):
113 self.context = context
115 self._initialized = False
119 return self.struct.name
122 if not self._initialized:
123 check(sr_driver_init(self.context.struct, self.struct))
124 self._initialized = True
126 device_list = sr_driver_scan(self.struct, None)
127 device_list_item = device_list
128 while device_list_item:
129 ptr = device_list_item.data
130 device_ptr = gpointer_to_sr_dev_inst_ptr(ptr)
131 devices.append(Device(self, device_ptr))
132 device_list_item = device_list_item.next
133 g_slist_free(device_list)
136 class Device(object):
138 def __new__(cls, driver, struct):
139 address = int(struct.this)
140 if address not in driver.context._devices:
141 device = super(Device, cls).__new__(cls, driver, struct)
142 driver.context._devices[address] = device
143 return driver.context._devices[address]
145 def __init__(self, driver, struct):
149 def __getattr__(self, name):
150 key = config_key(name)
151 data = new_gvariant_ptr_ptr()
153 check(sr_config_get(self.driver.struct, key, data, self.struct))
154 except Error as error:
155 if error.errno == SR_ERR_NA:
156 raise NotImplementedError(
157 "Device does not implement %s" % name)
160 value = gvariant_ptr_ptr_value(data)
161 return gvariant_to_python(value)
163 def __setattr__(self, name, value):
165 key = config_key(name)
166 except AttributeError:
167 super(Device, self).__setattr__(name, value)
169 check(sr_config_set(self.struct, key, python_to_gvariant(value)))
173 return self.struct.vendor
177 return self.struct.model
181 return self.struct.version
183 class Session(object):
185 def __init__(self, context):
186 assert context.session is None
187 self.context = context
188 self.struct = sr_session_new()
189 context.session = self
192 check(sr_session_destroy())
194 def add_device(self, device):
195 check(sr_session_dev_add(device.struct))
197 def add_callback(self, callback):
198 wrapper = partial(callback_wrapper, self, callback)
199 check(sr_session_datafeed_python_callback_add(wrapper))
202 check(sr_session_start())
205 check(sr_session_run())
208 check(sr_session_stop())
210 class Packet(object):
212 def __init__(self, session, struct):
213 self.session = session
219 return self.struct.type
223 if self._payload is None:
224 pointer = self.struct.payload
225 if self.struct.type == SR_DF_LOGIC:
226 self._payload = Logic(self,
227 void_ptr_to_sr_datafeed_logic_ptr(pointer))
228 elif self.struct.type == SR_DF_ANALOG:
229 self._payload = Analog(self,
230 void_ptr_to_sr_datafeed_analog_ptr(pointer))
232 raise NotImplementedError(
233 "No Python mapping for packet type %ѕ" % self.struct.type)
238 def __init__(self, packet, struct):
245 if self._data is None:
246 self._data = cdata(self.struct.data, self.struct.length)
249 class Analog(object):
251 def __init__(self, packet, struct):
257 def num_samples(self):
258 return self.struct.num_samples
262 return self.struct.mq
266 return self.struct.unit
270 return self.struct.mqflags
274 if self._data is None:
275 self._data = float_array.frompointer(self.struct.data)
278 for symbol_name in dir(lowlevel):
280 if symbol_name.startswith(prefix):
281 name = symbol_name[len(prefix):]
282 setattr(Packet, name, getattr(lowlevel, symbol_name))