Add Python bindings

Signed-off-by: Eric Wild <ewild@sysmocom.de>
This commit is contained in:
Clayton Smith 2020-12-30 10:05:36 -05:00 committed by Eric Wild
parent f88dc7df2f
commit 1f724162e1
5 changed files with 106 additions and 0 deletions

View File

@ -18,8 +18,11 @@ include(GrPybind)
########################################################################
list(APPEND osmosdr_python_files
device_python.cc
sink_python.cc
source_python.cc
ranges_python.cc
time_spec_python.cc
python_bindings.cc)
GR_PYBIND_MAKE_OOT(osmosdr

View File

@ -0,0 +1,27 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
#include <osmosdr/device.h>
void bind_device(py::module& m)
{
using device_t = ::osmosdr::device_t;
py::class_<device_t>(m, "device_t")
.def(py::init<std::string&>(), py::arg("args") = "")
.def("to_pp_string", &device_t::to_pp_string)
.def("to_string", &device_t::to_string);
using devices_t = ::osmosdr::devices_t;
py::class_<devices_t>(m, "devices_t");
using device = ::osmosdr::device;
py::class_<device>(m, "device")
.def_static("find", &device::find, py::arg("hint") = device_t());
}

View File

@ -25,6 +25,10 @@ namespace py = pybind11;
void bind_source(py::module& m);
// ) END BINDING_FUNCTION_PROTOTYPES
void bind_device(py::module& m);
void bind_ranges(py::module& m);
void bind_time_spec(py::module& m);
// We need this hack because import_array() returns NULL
// for newer Python versions.
@ -54,4 +58,8 @@ PYBIND11_MODULE(osmosdr_python, m)
bind_sink(m);
bind_source(m);
// ) END BINDING_FUNCTION_CALLS
bind_device(m);
bind_ranges(m);
bind_time_spec(m);
}

View File

@ -0,0 +1,35 @@
#include <pybind11/pybind11.h>
namespace py = pybind11;
#include <osmosdr/ranges.h>
void bind_ranges(py::module& m)
{
m.attr("ALL_MBOARDS") = ::osmosdr::ALL_MBOARDS;
m.attr("ALL_CHANS") = ::osmosdr::ALL_CHANS;
using range_t = ::osmosdr::range_t;
py::class_<range_t>(m, "range_t")
.def(py::init<double>(), py::arg("value") = 0)
.def(py::init<double, double, double>(), py::arg("start"), py::arg("stop"), py::arg("step") = 0)
.def("start", &range_t::start)
.def("stop", &range_t::stop)
.def("step", &range_t::step)
.def("to_pp_string", &range_t::to_pp_string);
using meta_range_t = ::osmosdr::meta_range_t;
py::class_<meta_range_t>(m, "meta_range_t")
.def(py::init())
.def(py::init<double, double, double>(), py::arg("start"), py::arg("stop"), py::arg("step") = 0)
.def("start", &meta_range_t::start)
.def("stop", &meta_range_t::stop)
.def("step", &meta_range_t::step)
.def("clip", &meta_range_t::clip, py::arg("value"), py::arg("clip_step") = false)
.def("values", &meta_range_t::values)
.def("to_pp_string", &meta_range_t::to_pp_string);
}

View File

@ -0,0 +1,33 @@
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
namespace py = pybind11;
#include <osmosdr/time_spec.h>
void bind_time_spec(py::module& m)
{
using time_spec_t = ::osmosdr::time_spec_t;
py::class_<time_spec_t>(m, "time_spec_t")
.def_static("get_system_time", &time_spec_t::get_system_time)
.def(py::init<double>(), py::arg("secs") = 0)
.def(py::init<time_t, double>(), py::arg("full_secs"), py::arg("frac_secs") = 0)
.def(py::init<time_t, long, double>(), py::arg("full_secs"), py::arg("tick_count"), py::arg("tick_rate"))
.def_static("from_ticks", &time_spec_t::from_ticks, py::arg("ticks"), py::arg("tick_rate"))
.def("get_tick_count", &time_spec_t::get_tick_count, py::arg("tick_rate"))
.def("to_ticks", &time_spec_t::to_ticks, py::arg("tick_rate"))
.def("get_real_secs", &time_spec_t::get_real_secs)
.def("get_full_secs", &time_spec_t::get_full_secs)
.def("get_frac_secs", &time_spec_t::get_frac_secs)
.def(py::self + py::self)
.def(py::self += py::self)
.def(py::self - py::self)
.def(py::self -= py::self)
.def(py::self == py::self)
.def(py::self != py::self)
.def(py::self < py::self)
.def(py::self > py::self)
.def(py::self <= py::self)
.def(py::self >= py::self);
}