From 388bc5229f01a042505dc30aacece88739fdeffc Mon Sep 17 00:00:00 2001 From: Brent Stapleton Date: Thu, 11 Oct 2018 16:37:44 -0700 Subject: python: adding device_addr_t to Python API Add support for device_addr_t to the Python API. Most functions are exposed normally. The main exception is the constructor from a map of strings, which is replaced by a factory function called `make_device_addr`. --- host/lib/types/types_python.hpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'host/lib') diff --git a/host/lib/types/types_python.hpp b/host/lib/types/types_python.hpp index 4cda00d0d..434ae9171 100644 --- a/host/lib/types/types_python.hpp +++ b/host/lib/types/types_python.hpp @@ -7,12 +7,30 @@ #ifndef INCLUDED_UHD_TYPES_PYTHON_HPP #define INCLUDED_UHD_TYPES_PYTHON_HPP +#include #include +#include +#include +#include + + +//! Make a device_addr_t from a Python dict of strings +static uhd::device_addr_t make_device_addr(bp::dict& info) { + // Manually extract each key and each value, copy them to a map of strings, and return that. + std::map info_map; + auto keys = info.keys(); + for (int ii = 0; ii < bp::len(keys); ++ii) { + std::string key = bp::extract(keys[ii]); + info_map[key] = bp::extract(info[key]); + } + return uhd::device_addr_t(info_map); +} void export_types() { using stream_cmd_t = uhd::stream_cmd_t; using stream_mode_t = stream_cmd_t::stream_mode_t; + using str_map = std::map; bp::enum_("stream_mode") .value("start_cont", stream_cmd_t::STREAM_MODE_START_CONTINUOUS ) @@ -28,6 +46,20 @@ void export_types() .def_readwrite("time_spec" , &stream_cmd_t::time_spec ) .def_readwrite("stream_now", &stream_cmd_t::stream_now) ; + + bp::class_("device_addr", bp::init >()) + // Constructors + /* TODO: This calls the correct C++ constructor, but Python + dictionaries != str_maps, so we get a signature error */ + .def(bp::init()) + + // Methods + .def("__str__", &uhd::device_addr_t::to_pp_string) + .def("to_string", &uhd::device_addr_t::to_string) + .def("to_pp_string", &uhd::device_addr_t::to_pp_string) + ; + + bp::def("make_device_addr", make_device_addr); } #endif /* INCLUDED_UHD_TYPES_PYTHON_HPP */ -- cgit v1.2.3