X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=8bccae1d5271aa98ebadf15baab4f07bcb3fa55d;hb=c1f9428a6c95e639e3d613738b6a2b08c8496ee0;hp=01201bd3d7e9ae5a63f2369595eb79c334f1d0f0;hpb=ae2cdde4d20e600480ae2cb86b7488fa13400812;p=libsigrok.git diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index 01201bd3..8bccae1d 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -45,6 +45,8 @@ which provides access to the error code and description." %module(docstring=DOCSTRING) classes %{ +#include "config.h" + #include #include #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION @@ -53,8 +55,6 @@ which provides access to the error code and description." PyObject *PyGObject_lib; PyObject *GLib; -#include "config.h" - #if PYGOBJECT_FLAGS_SIGNED typedef gint pyg_flags_type; #else @@ -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,17 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config return Glib::Variant::create(PyFloat_AsDouble(input)); else if (type == SR_T_INT32 && PyInt_Check(input)) return Glib::Variant::create(PyInt_AsLong(input)); - else - throw sigrok::Error(SR_ERR_ARG); + else if (type == SR_T_UINT32 && PyInt_Check(input)) + return Glib::Variant::create(PyInt_AsLong(input)); + 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 v = {(guint64)PyInt_AsLong(numObj), (guint64)PyInt_AsLong(denomObj)}; + return Glib::Variant< std::vector >::create(v); + } + } + throw sigrok::Error(SR_ERR_ARG); } /* Convert from a Python type to Glib::Variant, according to Option data type. */ @@ -359,6 +371,8 @@ Glib::VariantBase python_to_variant_by_option(PyObject *input, return Glib::Variant::create(PyFloat_AsDouble(input)); else if (type == G_VARIANT_TYPE_INT32 && PyInt_Check(input)) return Glib::Variant::create(PyInt_AsLong(input)); + else if (type == G_VARIANT_TYPE_UINT32 && PyInt_Check(input)) + return Glib::Variant::create(PyInt_AsLong(input)); else throw sigrok::Error(SR_ERR_ARG); } @@ -390,6 +404,7 @@ std::map dict_to_map_options(PyObject *dict, /* Ignore these methods, we will override them below. */ %ignore sigrok::Analog::data; +%ignore sigrok::Logic::data; %ignore sigrok::Driver::scan; %ignore sigrok::InputFormat::create_input; %ignore sigrok::OutputFormat::create_output; @@ -426,6 +441,16 @@ std::map dict_to_map_options(PyObject *dict, return (long) $self; } + std::string __str__() + { + return $self->name(); + } + + std::string __repr__() + { + return "Class." + $self->name(); + } + %pythoncode { def __eq__(self, other): @@ -538,4 +563,42 @@ std::map dict_to_map_options(PyObject *dict, } } +/* Return NumPy array from Logic::data(). */ +%extend sigrok::Logic +{ + PyObject * _data() + { + npy_intp dims[2]; + dims[0] = $self->data_length() / $self->unit_size(); + dims[1] = $self->unit_size(); + int typenum = NPY_UINT8; + void *data = $self->data_pointer(); + return PyArray_SimpleNewFromData(2, dims, typenum, data); + } + +%pythoncode +{ + data = property(_data) +} +} + +/* Create logic packet from Python buffer. */ +%extend sigrok::Context +{ + std::shared_ptr _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"