[add Servo control warner@lothar.com**20060826102939] { hunk ./_phidgetmodule.c 13 +static PyTypeObject PHIDGETSERVOHANDLEType; hunk ./_phidgetmodule.c 647 +// -------------------- CPhidgetServo + + +typedef struct { + PyObject_HEAD; + CPhidgetServoHandle phid; +} phidgetservohandle_t; + +static PyObject * +phidgetservohandle_new(PyTypeObject *type, PyObject *args, PyObject* kwargs) +{ + // this creates a new CPhidgetServoHandle and wraps it with a python + // object. + int rc; + phidgetservohandle_t *self; + + self = (phidgetservohandle_t *)PyObject_New(phidgetservohandle_t, + &PHIDGETSERVOHANDLEType); + if (!self) + return NULL; + rc = CPhidgetServo_create(&(self->phid)); + if (rc) + return raisePhidgetError(rc); + + return (PyObject *)self; +} + +static PyObject * +servo_getNumMotors(phidgetservohandle_t *self, PyObject *args) { + int rc, numMotors; + if (!PyArg_ParseTuple(args,"")) return NULL; + rc = CPhidgetServo_getNumMotors(self->phid, &numMotors); + if (rc) + return raisePhidgetError(rc); + return PyInt_FromLong(numMotors); +} + +static PyObject * +servo_getMotorPosition(phidgetservohandle_t *self, PyObject *args) { + int rc, index; + double pVal; + if (!PyArg_ParseTuple(args,"i", &index)) + return NULL; + rc = CPhidgetServo_getMotorPosition(self->phid, index, &pVal); + if (rc) + return raisePhidgetError(rc); + return PyFloat_FromDouble(pVal); +} + +static PyObject * +servo_setMotorPosition(phidgetservohandle_t *self, PyObject *args) { + int rc, index; + double newVal; + if (!PyArg_ParseTuple(args,"id", &index, &newVal)) + return NULL; + rc = CPhidgetServo_setMotorPosition(self->phid, index, newVal); + if (rc) + return raisePhidgetError(rc); + Py_INCREF(Py_None); + return Py_None; +} + +static PyMethodDef servo_methods[] = { + // these are the generic methods which work on all phidget handles + {"getDeviceName", (PyCFunction)phidget_getDeviceName, METH_VARARGS}, + {"getSerialNumber", (PyCFunction)phidget_getSerialNumber, METH_VARARGS}, + {"getDeviceVersion", (PyCFunction)phidget_getDeviceVersion, METH_VARARGS}, + {"getDeviceStatus", (PyCFunction)phidget_getDeviceStatus, METH_VARARGS}, + {"open", (PyCFunction)phidget_open, METH_VARARGS|METH_KEYWORDS}, +#if 0 + {"close", (PyCFunction)phidget_close, METH_VARARGS}, + {"set_OnDetach_Handler", (PyCFunction)phidget_set_OnDetach_Handler, METH_VARARGS}, + {"set_OnAttach_Handler", (PyCFunction)phidget_set_OnAttach_Handler, METH_VARARGS}, + {"set_OnError_Handler", (PyCFunction)phidget_set_OnError_Handler, METH_VARARGS}, + {"getTag", (PyCFunction)phidget_getTag, METH_VARARGS}, + {"setTag", (PyCFunction)phidget_setTag, METH_VARARGS}, +#endif + // these are servo -specific methods + + {"getNumMotors", (PyCFunction)servo_getNumMotors, METH_VARARGS}, + {"getMotorPosition", (PyCFunction)servo_getMotorPosition, METH_VARARGS}, + {"setMotorPosition", (PyCFunction)servo_setMotorPosition, METH_VARARGS}, +#if 0 + {"set_OnMotorPositionChange_Handler", + (PyCFunction)servo_set_OnMotorPositionChange_Handler, METH_VARARGS}, +#endif + + {NULL, NULL} +}; + +static PyObject * +servo_getattr(phidgethandle_t *self, char *name) { + return Py_FindMethod(servo_methods, (PyObject *)self, name); +} + +static PyTypeObject PHIDGETSERVOHANDLEType = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /*ob_size*/ + "phidget.servohandle", /*tp_name*/ + sizeof(phidgetservohandle_t), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + /* methods */ + (destructor) phidgethandle_dealloc, /*tp_dealloc*/ // generic + 0, /*print*/ + (getattrfunc)servo_getattr, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "CPhidgetServoHandle wrapper", /*tp_doc*/ +}; + hunk ./_phidgetmodule.c 902 + PHIDGETSERVOHANDLEType.tp_new = phidgetservohandle_new; hunk ./_phidgetmodule.c 921 + Py_INCREF(&PHIDGETSERVOHANDLEType); + PyModule_AddObject(m, "Servo", (PyObject *)&PHIDGETSERVOHANDLEType); + }