Changeset 117 for branches/cwiidpy/Wiimote.c
- Timestamp:
- 05/22/07 16:57:41 (2 years ago)
- Files:
-
- branches/cwiidpy/Wiimote.c (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cwiidpy/Wiimote.c
r114 r117 20 20 * 21 21 * ChangeLog: 22 * 2007-05-22 L. Donnie Smith <cwiid@abstrakraft.org> 23 * * changed disconnect to close 24 * * replaced command with attributes for rpt_mode, rumble, led, 25 * added request_status method 26 * * fixed memory leak in get_mesg 27 * * added function names to argument parsing errors 28 * 22 29 * 2007-05-15 L. Donnie Smith <cwiid@abstrakraft.org> 23 30 * * revised message types … … 44 51 static void Wiimote_dealloc(Wiimote *self); 45 52 static int Wiimote_init(Wiimote *self, PyObject *args, PyObject *kwds); 46 static PyObject *Wiimote_ disconnect(Wiimote *self);47 static PyObject * 48 Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds);53 static PyObject *Wiimote_close(Wiimote *self); 54 55 static PyObject *Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds); 49 56 static PyObject * 50 57 Wiimote_disable(Wiimote *self, PyObject *args, PyObject *kwds); 51 static PyObject * 52 Wiimote_set_mesg_callback(Wiimote *self, PyObject *args, PyObject *kwds); 58 59 static int 60 Wiimote_set_mesg_callback(Wiimote *self, PyObject *args, void *closure); 53 61 static PyObject *Wiimote_get_mesg(Wiimote *self); 54 62 static PyObject *Wiimote_get_state(Wiimote *self, void *closure); 55 static PyObject * 56 Wiimote_command(Wiimote *self, PyObject *args, PyObject *kwds); 63 64 static PyObject *Wiimote_request_status(Wiimote *self); 65 static int Wiimote_set_led(Wiimote *self, PyObject *PyLed, void *closure); 66 static int 67 Wiimote_set_rumble(Wiimote *self, PyObject *PyRumble, void *closure); 68 static int 69 Wiimote_set_rpt_mode(Wiimote *self, PyObject *PyRptMode, void *closure); 70 57 71 static PyObject *Wiimote_read(Wiimote *self, PyObject *args, PyObject *kwds); 58 72 static PyObject *Wiimote_write(Wiimote *self, PyObject *args, PyObject *kwds); … … 65 79 static PyMethodDef Wiimote_Methods[] = 66 80 { 67 {" disconnect", (PyCFunction)Wiimote_disconnect, METH_NOARGS,68 " disconnect wiimote"},81 {"close", (PyCFunction)Wiimote_close, METH_NOARGS, 82 "close wiimote connection"}, 69 83 {"enable", (PyCFunction)Wiimote_enable, METH_VARARGS | METH_KEYWORDS, 70 84 "enable flags on wiimote"}, … … 75 89 {"get_mesg", (PyCFunction)Wiimote_get_mesg, METH_NOARGS, 76 90 "blocking call to get messages"}, 77 {" command", (PyCFunction)Wiimote_command, METH_VARARGS | METH_KEYWORDS,78 " send wiimote command"},91 {"request_status", (PyCFunction)Wiimote_request_status, METH_NOARGS, 92 "request status message"}, 79 93 {"read", (PyCFunction)Wiimote_read, METH_VARARGS | METH_KEYWORDS, 80 94 "read from wiimote"}, 81 95 {"write", (PyCFunction)Wiimote_write, METH_VARARGS | METH_KEYWORDS, 82 96 "write to wiimote"}, 83 {NULL, NULL }97 {NULL, NULL, 0, NULL} 84 98 }; 85 99 86 100 static PyGetSetDef Wiimote_GetSet[] = { 87 101 {"state", (getter)Wiimote_get_state, NULL, "Wiimote state", NULL}, 88 {NULL} 102 {"mesg_callback", NULL, (setter)Wiimote_set_mesg_callback, 103 "Wiimote message callback", NULL}, 104 {"led", NULL, (setter)Wiimote_set_led, "Wiimote led state", NULL}, 105 {"rumble", NULL, (setter)Wiimote_set_rumble, "Wiimote rumble state", NULL}, 106 {"rpt_mode", NULL, (setter)Wiimote_set_rpt_mode, "Wiimote report mode", 107 NULL}, 108 {NULL, NULL, NULL, NULL, NULL} 89 109 }; 90 110 … … 150 170 { 151 171 if (self->wiimote) { 152 cwiid_ disconnect(self->wiimote);172 cwiid_close(self->wiimote); 153 173 } 154 174 Py_XDECREF(self->callback); … … 162 182 163 183 /* Set up wiimote */ 164 if(!(wiimote = cwiid_ connect(&bdaddr, flags))) {165 PyErr_SetString(PyExc_IOError, "Could not connect towiimote");184 if(!(wiimote = cwiid_open(&bdaddr, flags))) { 185 PyErr_SetString(PyExc_IOError, "Could not open wiimote"); 166 186 return -1; 167 187 } … … 176 196 static int Wiimote_init(Wiimote* self, PyObject* args, PyObject *kwds) 177 197 { 178 static char *kwlist[] = { "flags", NULL};198 static char *kwlist[] = {"flags", NULL}; 179 199 int flags = 0; 180 200 181 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &flags)) { 201 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:cwiid.Wiimote.init", 202 kwlist, &flags)) { 182 203 return -1; 183 204 } … … 190 211 } 191 212 192 static PyObject *Wiimote_ disconnect(Wiimote *self)193 { 194 if (cwiid_ disconnect(self->wiimote)) {195 PyErr_SetString(PyExc_IOError, "Wiimote disconnecterror");213 static PyObject *Wiimote_close(Wiimote *self) 214 { 215 if (cwiid_close(self->wiimote)) { 216 PyErr_SetString(PyExc_IOError, "Wiimote close error"); 196 217 self->wiimote = NULL; 197 218 return NULL; … … 204 225 static PyObject *Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds) 205 226 { 206 static char *kwlist[] = { "flags", NULL};227 static char *kwlist[] = {"flags", NULL}; 207 228 int flags; 208 229 209 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { 230 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.enable", 231 kwlist, &flags)) { 210 232 return NULL; 211 233 } … … 221 243 static PyObject *Wiimote_disable(Wiimote *self, PyObject *args, PyObject *kwds) 222 244 { 223 static char *kwlist[] = { "flags", NULL};245 static char *kwlist[] = {"flags", NULL}; 224 246 int flags; 225 247 226 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { 248 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:cwiid.Wiimote.disable", 249 kwlist, &flags)) { 227 250 return NULL; 228 251 } … … 236 259 } 237 260 238 static PyObject * 239 Wiimote_set_mesg_callback(Wiimote *self, PyObject *args, PyObject *kwds) 240 { 241 static char *kwlist[] = { "callback", NULL }; 242 PyObject *NewCallback, *OldCallback; 243 244 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &NewCallback)) { 245 return NULL; 246 } 261 static int 262 Wiimote_set_mesg_callback(Wiimote *self, PyObject *NewCallback, 263 void *closure) 264 { 265 PyObject *OldCallback; 266 247 267 if (!PyCallable_Check(NewCallback)) { 248 268 PyErr_SetString(PyExc_TypeError, "callback must be callable!"); … … 253 273 if (cwiid_set_mesg_callback(self->wiimote, callbackBridge)) { 254 274 PyErr_SetString(PyExc_IOError, "set callback error"); 255 return NULL;275 return -1; 256 276 } 257 277 } … … 259 279 if (cwiid_set_mesg_callback(self->wiimote, NULL)) { 260 280 PyErr_SetString(PyExc_IOError, "set callback error"); 261 return NULL;281 return -1; 262 282 } 263 283 } … … 267 287 self->callback = NewCallback; 268 288 269 Py_RETURN_NONE;289 return 0; 270 290 } 271 291 … … 275 295 int mesg_count; 276 296 struct timespec t; 297 PyObject *PyMesg; 277 298 278 299 if (cwiid_get_mesg(self->wiimote, &mesg_count, &mesg, &t)) { … … 286 307 } 287 308 288 return processMesgs(mesg_count, mesg); 309 PyMesg = processMesgs(mesg_count, mesg); 310 311 free(mesg); 312 313 return PyMesg; 289 314 } 290 315 … … 456 481 } 457 482 458 static PyObject *Wiimote_command(Wiimote *self, PyObject *args, PyObject *kwds) 483 static PyObject *Wiimote_request_status(Wiimote *self) 484 { 485 if (cwiid_request_status(self->wiimote)) { 486 PyErr_SetString(PyExc_IOError, "Wiimote request status error"); 487 return NULL; 488 } 489 490 Py_RETURN_NONE; 491 } 492 493 static int Wiimote_set_led(Wiimote *self, PyObject *PyLed, void *closure) 494 { 495 long led; 496 497 if (((led = PyInt_AsLong(PyLed)) == -1) && PyErr_Occurred()) { 498 return -1; 499 } 500 501 if (cwiid_set_led(self->wiimote, (uint8_t)led)) { 502 PyErr_SetString(PyExc_IOError, "Wiimote set led error"); 503 return -1; 504 } 505 506 return 0; 507 } 508 509 static int 510 Wiimote_set_rumble(Wiimote *self, PyObject *PyRumble, void *closure) 511 { 512 long rumble; 513 514 if (((rumble = PyInt_AsLong(PyRumble)) == -1) && PyErr_Occurred()) { 515 return -1; 516 } 517 518 if (cwiid_set_rumble(self->wiimote, (uint8_t)rumble)) { 519 PyErr_SetString(PyExc_IOError, "Wiimote set rumble error"); 520 return -1; 521 } 522 523 return 0; 524 } 525 526 static int 527 Wiimote_set_rpt_mode(Wiimote *self, PyObject *PyRptMode, void *closure) 528 { 529 long rpt_mode; 530 531 if (((rpt_mode = PyInt_AsLong(PyRptMode)) == -1) && PyErr_Occurred()) { 532 return -1; 533 } 534 535 if (cwiid_set_rpt_mode(self->wiimote, (uint8_t)rpt_mode)) { 536 PyErr_SetString(PyExc_IOError, "Wiimote set rpt_mode error"); 537 return -1; 538 } 539 540 return 0; 541 } 542 543 /* static PyObject *Wiimote_command(Wiimote *self, PyObject *args, PyObject *kwds) 459 544 { 460 545 static char *kwlist[] = { "command", "flags", NULL }; … … 470 555 Py_RETURN_NONE; 471 556 } 557 */ 472 558 473 559 static PyObject *Wiimote_read(Wiimote *self, PyObject *args, PyObject *kwds) … … 480 566 PyObject *pyRetBuf; 481 567 482 if (!PyArg_ParseTupleAndKeywords(args, kwds, "BII ", kwlist, &flags,483 &offset, &len)) {568 if (!PyArg_ParseTupleAndKeywords(args, kwds, "BII:cwiid.Wiimote.read", 569 kwlist, &flags, &offset, &len)) { 484 570 return NULL; 485 571 } … … 509 595 int len; 510 596 511 if (!PyArg_ParseTupleAndKeywords(args, kwds, "BIt# ", kwlist, &flags,512 &offset, &buf, &len)) {597 if (!PyArg_ParseTupleAndKeywords(args, kwds, "BIt#:cwiid.Wiimote.write", 598 kwlist, &flags, &offset, &buf, &len)) { 513 599 return NULL; 514 600 } … … 526 612 union cwiid_mesg mesg[], struct timespec *t) 527 613 { 528 PyObject *argTuple; 529 PyObject *pyself; 530 /* PyObject *pyCallback; */ 614 PyObject *ArgTuple; 615 PyObject *PySelf; 531 616 PyGILState_STATE gstate; 532 617 533 618 gstate = PyGILState_Ensure(); 534 619 535 argTuple = processMesgs(mesg_count, mesg);620 ArgTuple = processMesgs(mesg_count, mesg); 536 621 537 622 /* Put id and the list of messages as the arguments to the callback */ 538 pyself = (PyObject *) cwiid_get_data(wiimote);539 PyObject_CallFunction(((Wiimote *) pyself)->callback, "(O)",argTuple);540 541 Py_XDECREF( argTuple);623 PySelf = (PyObject *) cwiid_get_data(wiimote); 624 PyObject_CallFunction(((Wiimote *)PySelf)->callback, "(O)", ArgTuple); 625 626 Py_XDECREF(ArgTuple); 542 627 PyGILState_Release(gstate); 543 628 }
