diff --git a/src/dynamic-graph-py.cc b/src/dynamic-graph-py.cc index a8b08a1e1bfc71e403490bc9368c3bf51f9918c9..3d78e0f06e44745c8bf8974eb439d46f810297c5 100644 --- a/src/dynamic-graph-py.cc +++ b/src/dynamic-graph-py.cc @@ -19,6 +19,8 @@ namespace dynamicgraph { namespace signalBase { extern PyObject* create(PyObject* self, PyObject* args); extern PyObject* getTime(PyObject* self, PyObject* args); + extern PyObject* getValue(PyObject* self, PyObject* args); + extern PyObject* setValue(PyObject* self, PyObject* args); } namespace entity { extern PyObject* create(PyObject* self, PyObject* args); @@ -99,6 +101,10 @@ static PyMethodDef dynamicGraphMethods[] = { "create a SignalBase C++ object"}, {"signalbase_get_time", dynamicgraph::python::signalBase::getTime, METH_VARARGS, "Get time of a SignalBase"}, + {"signal_base_get_value", dynamicgraph::python::signalBase::getValue, + METH_VARARGS, "Read the value of a signal"}, + {"signal_base_set_value", dynamicgraph::python::signalBase::setValue, + METH_VARARGS, "Set the value of a signal"}, // Entity {"create_entity", dynamicgraph::python::entity::create, METH_VARARGS, "create an Entity C++ object"}, diff --git a/src/dynamic_graph/signal_base.py b/src/dynamic_graph/signal_base.py index 864be1225d337911c0a6bf73070d75dc64314f16..e925e2c250d249dea5d53e097ac00fec6679786a 100644 --- a/src/dynamic_graph/signal_base.py +++ b/src/dynamic_graph/signal_base.py @@ -29,3 +29,17 @@ class SignalBase: """ return wrap.signalBaseGetTime(self.object) + @property + def value(self) : + """ + Read the value of a signal + """ + return wrap.signal_base_get_value(self.object) + + @value.setter + def value(self) : + """ + Set the signal as a constant signal with given value. + If the signal is plugged, it will be unplugged + """ + return wrap.signal_base_set_value(self.object, value) diff --git a/src/signal-base-py.cc b/src/signal-base-py.cc index a86d38b63c152c120f33e8ed487d5ca10c7e6309..40d85b9d74f972bac0f933da89f7095fefee50ac 100644 --- a/src/signal-base-py.cc +++ b/src/signal-base-py.cc @@ -5,8 +5,7 @@ #include <Python.h> #include <iostream> -//#include <sstream> -//#include <string> +#include <sstream> #include <dynamic-graph/signal-base.h> @@ -27,17 +26,17 @@ namespace dynamicgraph { PyObject* create(PyObject* self, PyObject* args) { char *name = NULL; - + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - + SignalBase<int>* obj = NULL; obj = new SignalBase<int>(std::string(name)); - + // Return the pointer return PyCObject_FromVoidPtr((void*)obj, destroy); } - + /** \brief Destroy an instance of InvertedPendulum */ @@ -46,7 +45,7 @@ namespace dynamicgraph { SignalBase<int>* obj = (SignalBase<int>*)self; delete obj; } - + PyObject* getTime(PyObject* self, PyObject* args) { void* pointer = NULL; @@ -58,10 +57,46 @@ namespace dynamicgraph { pointer = PyCObject_AsVoidPtr(object); SignalBase<int>* obj = (SignalBase<int>*)pointer; - + int time = obj->getTime(); return Py_BuildValue("i", time); } + + PyObject* getValue(PyObject* self, PyObject* args) + { + void* pointer = NULL; + PyObject* object = NULL; + if (!PyArg_ParseTuple(args,"O", &object)) + return NULL; + if (!PyCObject_Check(object)) + return NULL; + + pointer = PyCObject_AsVoidPtr(object); + SignalBase<int>* signal = (SignalBase<int>*)pointer; + + std::ostringstream value; + signal->get(value); + return Py_BuildValue("s", value.str()); + } + + PyObject* setValue(PyObject* self, PyObject* args) + { + void * pointer = NULL; + PyObject* object = NULL; + char* valueString = NULL; + + if (!PyArg_ParseTuple(args,"Os", &object, &valueString)) + return NULL; + if (!PyCObject_Check(object)) + return NULL; + + pointer = PyCObject_AsVoidPtr(object); + SignalBase<int>* signal = (SignalBase<int>*)pointer; + std::istringstream value(valueString); + + signal->set(value); + return Py_BuildValue(""); + } } } }