Changeset 110
- Timestamp:
- 05/09/07 21:26:58 (2 years ago)
- Files:
-
- branches/cwiidpy/cwiidmodule.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cwiidpy/cwiidmodule.c
r109 r110 20 20 * 21 21 * ChangeLog: 22 * 2007-05-0 7L. Donnie Smith <cwiid@abstrakraft.org>22 * 2007-05-09 L. Donnie Smith <cwiid@abstrakraft.org> 23 23 * * finished get_state 24 * * fixed read buffer issue 25 * * implemented write 26 * * cleaned up types 27 * 24 28 * 2007-05-07 L. Donnie Smith <cwiid@abstrakraft.org> 25 29 * * C-style comments … … 66 70 static int Wiimote_init(Wiimote *self, PyObject *args, PyObject *kwds); 67 71 static PyObject *Wiimote_read(Wiimote *self, PyObject *args); 68 static PyObject *Wiimote_write(Wiimote *self, PyObject *args );72 static PyObject *Wiimote_write(Wiimote *self, PyObject *args, PyObject *kwds); 69 73 static PyObject *Wiimote_command(Wiimote *self, PyObject *args); 70 74 static PyObject *Wiimote_disconnect(Wiimote *self); … … 173 177 static PyObject *processMesgs(int mesg_count, union cwiid_mesg mesg[]); 174 178 static int cwiid_start(Wiimote *self, int flags); 175 static PyObject *notImplemented();176 179 177 180 /* Associates cwiid functions with python ones */ … … 186 189 /* {"__init__", constructorwii, METH_VARARGS, "cwiid(function)"}, */ 187 190 {"read", (PyCFunction)Wiimote_read, METH_VARARGS, "read from wiimote"}, 188 {"write", (PyCFunction)Wiimote_write, METH_VARARGS, "write to wiimote"}, 191 {"write", (PyCFunction)Wiimote_write, METH_VARARGS | METH_KEYWORDS, 192 "write to wiimote"}, 189 193 {"command", (PyCFunction)Wiimote_command, METH_VARARGS, 190 194 "send wiimote command"}, … … 354 358 uint32_t length; 355 359 void *buf; 360 int len; 356 361 357 362 PyArg_UnpackTuple(args, "read", 3, 3, &pyflags,&pyoffset,&pylength); … … 362 367 PyErr_SetString(PyExc_TypeError, "arguments must be ints"); 363 368 } 364 369 365 370 /* marshal everything over */ 366 371 flags = (uint8_t) PyInt_AsLong(pyflags); … … 368 373 length = (uint32_t) PyInt_AsLong(pylength); 369 374 370 /* TODO: More error checking */ 371 buf = malloc(length); 372 cwiid_read(self->wiimote,flags,offset,length,buf); 373 pyRetBuf = PyBuffer_FromMemory((char*)buf, length); 374 375 Py_XINCREF(pyRetBuf); 375 if (!(pyRetBuf = PyBuffer_New(length))) { 376 return NULL; 377 } 378 if (PyObject_AsWriteBuffer(pyRetBuf, &buf, &len)) { 379 Py_DECREF(pyRetBuf); 380 return NULL; 381 } 382 if (cwiid_read(self->wiimote,flags,offset,length,buf)) { 383 PyErr_SetString(PyExc_IOError, "Wiimote read error"); 384 Py_DECREF(pyRetBuf); 385 return NULL; 386 } 387 376 388 return pyRetBuf; 377 389 } 378 390 379 static PyObject *Wiimote_write(Wiimote *self, PyObject *args) 380 { 381 return notImplemented(); 391 static PyObject *Wiimote_write(Wiimote *self, PyObject *args, PyObject *kwds) 392 { 393 static char *kwlist[] = { "flags", "offset", "buffer", NULL }; 394 unsigned char flags; 395 unsigned int offset; 396 char *buf; 397 int len; 398 399 if (!PyArg_ParseTupleAndKeywords(args, kwds, "BIt#", kwlist, &flags, 400 &offset, &buf, &len)) { 401 return NULL; 402 } 403 404 if (cwiid_write(self->wiimote, flags, offset, len, buf)) { 405 PyErr_SetString(PyExc_IOError, "Wiimote write error"); 406 return NULL; 407 } 408 409 Py_RETURN_NONE; 382 410 } 383 411 … … 423 451 { 424 452 static char *kwlist[] = { "flags", NULL }; 425 int flags = 0;453 int flags; 426 454 427 455 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { … … 441 469 { 442 470 static char *kwlist[] = { "flags", NULL }; 443 int flags = 0;471 int flags; 444 472 445 473 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { … … 496 524 } 497 525 498 PyState = Py_BuildValue("{s: b,s:b,s:b,s:b,s:i,s:i}",526 PyState = Py_BuildValue("{s:B,s:B,s:B,s:B,s:I,s:I}", 499 527 "rpt_mode", state.rpt_mode, 500 528 "led", state.led, … … 505 533 506 534 if (state.rpt_mode & CWIID_RPT_BTN) { 507 PyObject *PyBtn = Py_BuildValue(" i", state.buttons);535 PyObject *PyBtn = Py_BuildValue("I", state.buttons); 508 536 if (!PyBtn) { 509 537 Py_DECREF(PyState); … … 519 547 520 548 if (state.rpt_mode & CWIID_RPT_ACC) { 521 PyObject *PyAcc = Py_BuildValue("{s: b,s:b,s:b}",549 PyObject *PyAcc = Py_BuildValue("{s:B,s:B,s:B}", 522 550 "x", state.acc[CWIID_X], 523 551 "y", state.acc[CWIID_Y], … … 556 584 557 585 if (state.ir_src[i].valid) { 558 PyIrSrc = Py_BuildValue("{s:{s: i,s:i},s:b}",586 PyIrSrc = Py_BuildValue("{s:{s:I,s:I},s:b}", 559 587 "pos", 560 588 "x", state.ir_src[i].pos[CWIID_X], … … 578 606 case CWIID_EXT_NUNCHUK: 579 607 if (state.rpt_mode & CWIID_RPT_NUNCHUK) { 580 PyExt = Py_BuildValue("{s:{s: b,s:b},s:{s:b,s:b,s:b},s:i}",608 PyExt = Py_BuildValue("{s:{s:B,s:B},s:{s:B,s:B,s:B},s:I}", 581 609 "stick", 582 610 "x", state.ext.nunchuk.stick[CWIID_X], … … 634 662 635 663 return PyState; 636 }637 638 static PyObject *notImplemented()639 {640 PyErr_SetString(PyExc_NotImplementedError, "This has not yet been implemented");641 642 Py_RETURN_NONE;643 664 } 644 665
