]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.py
python: Add ConfigKey.info and ConfigKey.<id> shortcuts.
[libsigrok.git] / bindings / python / sigrok / core / classes.py
index 6b55eb3f99754c7c431290898961874776097e59..6b5325baa58a98fd8fc6d35a45a2fb28fc008a04 100644 (file)
@@ -25,7 +25,8 @@ import itertools
 
 __all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet', 'Log',
     'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag', 'ConfigKey',
-    'ProbeType', 'Probe', 'ProbeGroup']
+    'ProbeType', 'Probe', 'ProbeGroup', 'InputFormat', 'OutputFormat',
+    'InputFile', 'Output']
 
 class Error(Exception):
 
@@ -434,6 +435,28 @@ class InputFormat(object):
     def description(self):
         return self.struct.description
 
+    def format_match(self, filename):
+        return bool(self.struct.call_format_match(filename))
+
+class InputFile(object):
+
+    def __init__(self, format, filename, **kwargs):
+        self.format = format
+        self.filename = filename
+        self.struct = sr_input()
+        self.struct.format = self.format.struct
+        self.struct.param = g_hash_table_new_full(
+            g_str_hash_ptr, g_str_equal_ptr, g_free_ptr, g_free_ptr)
+        for key, value in kwargs.items():
+            g_hash_table_insert(self.struct.param, g_strdup(key), g_strdup(str(value)))
+        check(self.format.struct.call_init(self.struct, self.filename))
+
+    def load(self):
+        check(self.format.struct.call_loadfile(self.struct, self.filename))
+
+    def __del__(self):
+        g_hash_table_destroy(self.struct.param)
+
 class OutputFormat(object):
 
     def __init__(self, context, struct):
@@ -448,6 +471,85 @@ class OutputFormat(object):
     def description(self):
         return self.struct.description
 
+class Output(object):
+
+    def __init__(self, format, device, param=None):
+        self.format = format
+        self.device = device
+        self.param = param
+        self.struct = sr_output()
+        self.struct.format = self.format.struct
+        self.struct.sdi = self.device.struct
+        self.struct.param = param
+        check(self.format.struct.call_init(self.struct))
+
+    def receive(self, packet):
+
+        output_buf_ptr = new_uint8_ptr_ptr()
+        output_len_ptr = new_uint64_ptr()
+        using_obsolete_api = False
+
+        if self.format.struct.event and packet.type in (
+                PacketType.TRIGGER, PacketType.FRAME_BEGIN,
+                PacketType.FRAME_END, PacketType.END):
+            check(self.format.struct.call_event(self.struct, packet.type.id,
+                output_buf_ptr, output_len_ptr))
+            using_obsolete_api = True
+        elif self.format.struct.data and packet.type.id == self.format.struct.df_type:
+            check(self.format.struct.call_data(self.struct,
+                packet.payload.struct.data, packet.payload.struct.length,
+                output_buf_ptr, output_len_ptr))
+            using_obsolete_api = True
+
+        if using_obsolete_api:
+            output_buf = uint8_ptr_ptr_value(output_buf_ptr)
+            output_len = uint64_ptr_value(output_len_ptr)
+            result = cdata(output_buf, output_len)
+            g_free(output_buf)
+            return result
+
+        if self.format.struct.receive:
+            out_ptr = new_gstring_ptr_ptr()
+            check(self.format.struct.call_receive(self.struct, self.device.struct,
+                packet.struct, out_ptr))
+            out = gstring_ptr_ptr_value(out_ptr)
+            if out:
+                result = out.str
+                g_string_free(out, True)
+                return result
+
+        return None
+
+    def __del__(self):
+        check(self.format.struct.call_cleanup(self.struct))
+
+class ConfigInfo(object):
+
+    def __new__(cls, key):
+        struct = sr_config_info_get(key.id)
+        if not struct:
+            return None
+        obj = super(ConfigInfo, cls).__new__(cls)
+        obj.key = key
+        obj.struct = struct
+        return obj
+
+    @property
+    def datatype(self):
+        return DataType(self.struct.datatype)
+
+    @property
+    def id(self):
+        return self.struct.id
+
+    @property
+    def name(self):
+        return self.struct.name
+
+    @property
+    def description(self):
+        return self.struct.description
+
 class EnumValue(object):
 
     _enum_values = {}
@@ -487,6 +589,9 @@ class QuantityFlag(EnumValue):
 class ConfigKey(EnumValue):
     pass
 
+class DataType(EnumValue):
+    pass
+
 class ProbeType(EnumValue):
     pass
 
@@ -498,8 +603,14 @@ for symbol_name in dir(lowlevel):
         ('SR_UNIT_', Unit),
         ('SR_MQFLAG_', QuantityFlag),
         ('SR_CONF_', ConfigKey),
+        ('SR_T_', DataType),
         ('SR_PROBE_', ProbeType)]:
         if symbol_name.startswith(prefix):
             name = symbol_name[len(prefix):]
             value = getattr(lowlevel, symbol_name)
-            setattr(cls, name, cls(value))
+            obj = cls(value)
+            setattr(cls, name, obj)
+            if cls is ConfigKey:
+                obj.info = ConfigInfo(obj)
+                if obj.info:
+                    setattr(cls, obj.info.id, obj)