]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.i
korad-kaxxxxp: use ID text prefix with optional version for RND models
[libsigrok.git] / bindings / python / sigrok / core / classes.i
index 63a5143ebc96b8379b14a3fe86035641159719f4..eb557d0375335f561eb1e05d91f3536c38620c2f 100644 (file)
@@ -112,6 +112,9 @@ typedef guint pyg_flags_type;
     g_free(value);
 }
 
+/* Use the same typemap above for Glib::VariantContainerBase */
+%apply Glib::VariantBase { Glib::VariantContainerBase }
+
 /* Map from callable PyObject to LogCallbackFunction */
 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction {
     $1 = PyCallable_Check($input);
@@ -337,8 +340,15 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config
         return Glib::Variant<double>::create(PyFloat_AsDouble(input));
     else if (type == SR_T_INT32 && PyInt_Check(input))
         return Glib::Variant<gint32>::create(PyInt_AsLong(input));
-    else
-        throw sigrok::Error(SR_ERR_ARG);
+    else if ((type == SR_T_RATIONAL_VOLT) && PyTuple_Check(input) && (PyTuple_Size(input) == 2)) {
+        PyObject *numObj = PyTuple_GetItem(input, 0);
+        PyObject *denomObj = PyTuple_GetItem(input, 1);
+        if ((PyInt_Check(numObj) || PyLong_Check(numObj)) && (PyInt_Check(denomObj) || PyLong_Check(denomObj))) {
+          const std::vector<guint64> v = {(guint64)PyInt_AsLong(numObj), (guint64)PyInt_AsLong(denomObj)};
+          return Glib::Variant< std::vector<guint64> >::create(v);
+        }
+    }
+    throw sigrok::Error(SR_ERR_ARG);
 }
 
 /* Convert from a Python type to Glib::Variant, according to Option data type. */
@@ -568,5 +578,23 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
 }
 }
 
+/* Create logic packet from Python buffer. */
+%extend sigrok::Context
+{
+    std::shared_ptr<Packet> _create_logic_packet_buf(PyObject *buf, unsigned int unit_size)
+    {
+        Py_buffer view;
+        PyObject_GetBuffer(buf, &view, PyBUF_SIMPLE);
+        return $self->create_logic_packet(view.buf, view.len, unit_size);
+    }
+}
+
+%pythoncode
+{
+    def _Context_create_logic_packet(self, buf, unit_size):
+        return self._create_logic_packet_buf(buf, unit_size)
+
+    Context.create_logic_packet = _Context_create_logic_packet
+}
 
 %include "doc_end.i"