| 53 | | static int Wiimote_init(PyObject *self, PyObject *args, PyObject *kwds); |
|---|
| 54 | | static PyObject *Wiimote_read(PyObject *self, PyObject *args); |
|---|
| 55 | | static PyObject *Wiimote_write(PyObject *self, PyObject *args); |
|---|
| 56 | | static PyObject *Wiimote_command(PyObject *self, PyObject *args); |
|---|
| 57 | | static PyObject *Wiimote_disconnect(PyObject *self, PyObject *args); |
|---|
| 58 | | static PyObject *Wiimote_enable(PyObject *self, PyObject *args); |
|---|
| 59 | | static PyObject *Wiimote_disable(PyObject *self, PyObject *args); |
|---|
| 60 | | static PyObject *Wiimote_get_mesg(PyObject *self, PyObject *args); |
|---|
| 61 | | static PyObject *Wiimote_set_callback(PyObject *self, PyObject *args); |
|---|
| 62 | | static PyObject *Wiimote_get_state(PyObject *self, PyObject *args); |
|---|
| | 57 | /* Types */ |
|---|
| | 58 | typedef struct { |
|---|
| | 59 | PyObject_HEAD |
|---|
| | 60 | cwiid_wiimote_t *wiimote; |
|---|
| | 61 | PyObject *callback; |
|---|
| | 62 | } Wiimote; |
|---|
| | 63 | |
|---|
| | 64 | static int Wiimote_init(Wiimote *self, PyObject *args, PyObject *kwds); |
|---|
| | 65 | static PyObject *Wiimote_read(Wiimote *self, PyObject *args); |
|---|
| | 66 | static PyObject *Wiimote_write(Wiimote *self, PyObject *args); |
|---|
| | 67 | static PyObject *Wiimote_command(Wiimote *self, PyObject *args); |
|---|
| | 68 | static PyObject *Wiimote_disconnect(Wiimote *self); |
|---|
| | 69 | static PyObject * |
|---|
| | 70 | Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds); |
|---|
| | 71 | static PyObject * |
|---|
| | 72 | Wiimote_disable(Wiimote *self, PyObject *args, PyObject *kwds); |
|---|
| | 73 | static PyObject *Wiimote_get_mesg(Wiimote *self, PyObject *args); |
|---|
| | 74 | static PyObject *Wiimote_set_callback(Wiimote *self, PyObject *args); |
|---|
| | 75 | static PyObject *Wiimote_get_state(Wiimote *self, void *closure); |
|---|
| 181 | | {"read", Wiimote_read, METH_VARARGS, "read from wiimote"}, |
|---|
| 182 | | {"write", Wiimote_write, METH_VARARGS, "write to wiimote"}, |
|---|
| 183 | | {"command", Wiimote_command, METH_VARARGS, "send wiimote command"}, |
|---|
| 184 | | {"enable", Wiimote_enable, METH_VARARGS, "enable flags on wiimote"}, |
|---|
| 185 | | {"disable", Wiimote_disable, METH_VARARGS, "disable flags on wiimote"}, |
|---|
| 186 | | {"get_mesg", Wiimote_get_mesg, METH_VARARGS, "blocking call to get messages"}, |
|---|
| 187 | | {"set_callback", Wiimote_set_callback, METH_VARARGS, "setup a mesg processing callback"}, |
|---|
| 188 | | {"get_state", Wiimote_get_state, METH_VARARGS, "polling interface"}, |
|---|
| 189 | | {"disconnect", Wiimote_disconnect, METH_VARARGS, "disconnect wiimote"}, |
|---|
| | 185 | {"read", (PyCFunction)Wiimote_read, METH_VARARGS, "read from wiimote"}, |
|---|
| | 186 | {"write", (PyCFunction)Wiimote_write, METH_VARARGS, "write to wiimote"}, |
|---|
| | 187 | {"command", (PyCFunction)Wiimote_command, METH_VARARGS, |
|---|
| | 188 | "send wiimote command"}, |
|---|
| | 189 | {"enable", (PyCFunction)Wiimote_enable, METH_VARARGS | METH_KEYWORDS, |
|---|
| | 190 | "enable flags on wiimote"}, |
|---|
| | 191 | {"disable", (PyCFunction)Wiimote_disable, METH_VARARGS | METH_KEYWORDS, |
|---|
| | 192 | "disable flags on wiimote"}, |
|---|
| | 193 | {"get_mesg", (PyCFunction)Wiimote_get_mesg, METH_VARARGS, |
|---|
| | 194 | "blocking call to get messages"}, |
|---|
| | 195 | {"set_callback", (PyCFunction)Wiimote_set_callback, METH_VARARGS, |
|---|
| | 196 | "setup a mesg processing callback"}, |
|---|
| | 197 | {"disconnect", (PyCFunction)Wiimote_disconnect, METH_NOARGS, |
|---|
| | 198 | "disconnect wiimote"}, |
|---|
| 304 | | static int Wiimote_init(PyObject* self, PyObject* args, PyObject *kwds) |
|---|
| 305 | | { |
|---|
| 306 | | PyObject* pyFlags; |
|---|
| 307 | | |
|---|
| 308 | | /* Get out parameters */ |
|---|
| 309 | | if (PyArg_UnpackTuple(args, "constructor", 1, 1, &pyFlags)) { |
|---|
| 310 | | if (!PyInt_Check(pyFlags)) { |
|---|
| 311 | | PyErr_SetString(PyExc_TypeError, "Parameter must be int"); |
|---|
| 312 | | return 0; |
|---|
| 313 | | } |
|---|
| 314 | | } |
|---|
| 315 | | |
|---|
| 316 | | if (cwiid_start((Wiimote *)self, PyInt_AsLong(pyFlags)) < 0) { |
|---|
| 317 | | PyErr_SetString(PyExc_IOError, "Could not connect to wiimote"); |
|---|
| 318 | | } |
|---|
| 319 | | PyEval_InitThreads(); |
|---|
| | 324 | static int Wiimote_init(Wiimote* self, PyObject* args, PyObject *kwds) |
|---|
| | 325 | { |
|---|
| | 326 | static char *kwlist[] = { "flags", NULL }; |
|---|
| | 327 | int flags = 0; |
|---|
| | 328 | |
|---|
| | 329 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &flags)) { |
|---|
| | 330 | return -1; |
|---|
| | 331 | } |
|---|
| | 332 | |
|---|
| | 333 | if (cwiid_start(self, flags)) { |
|---|
| | 334 | return -1; |
|---|
| | 335 | } |
|---|
| 393 | | static PyObject *Wiimote_disconnect(PyObject *self, PyObject *args) |
|---|
| 394 | | { |
|---|
| 395 | | return notImplemented(); |
|---|
| 396 | | } |
|---|
| 397 | | |
|---|
| 398 | | static PyObject *Wiimote_enable(PyObject *self, PyObject *args) |
|---|
| 399 | | { |
|---|
| 400 | | return notImplemented(); |
|---|
| 401 | | } |
|---|
| 402 | | |
|---|
| 403 | | static PyObject *Wiimote_disable(PyObject *self, PyObject *args) |
|---|
| 404 | | { |
|---|
| 405 | | return notImplemented(); |
|---|
| 406 | | } |
|---|
| 407 | | |
|---|
| 408 | | static PyObject *Wiimote_get_mesg(PyObject *self, PyObject *args) |
|---|
| | 409 | static PyObject *Wiimote_disconnect(Wiimote *self) |
|---|
| | 410 | { |
|---|
| | 411 | if (cwiid_disconnect(self->wiimote)) { |
|---|
| | 412 | PyErr_SetString(PyExc_IOError, "Wiimote disconnect error"); |
|---|
| | 413 | self->wiimote = NULL; |
|---|
| | 414 | return NULL; |
|---|
| | 415 | } |
|---|
| | 416 | self->wiimote = NULL; |
|---|
| | 417 | |
|---|
| | 418 | Py_RETURN_NONE; |
|---|
| | 419 | } |
|---|
| | 420 | |
|---|
| | 421 | static PyObject *Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds) |
|---|
| | 422 | { |
|---|
| | 423 | static char *kwlist[] = { "flags", NULL }; |
|---|
| | 424 | int flags = 0; |
|---|
| | 425 | |
|---|
| | 426 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { |
|---|
| | 427 | return NULL; |
|---|
| | 428 | } |
|---|
| | 429 | |
|---|
| | 430 | if (cwiid_enable(self->wiimote, flags)) { |
|---|
| | 431 | PyErr_SetString(PyExc_IOError, "cwiid_enable error"); |
|---|
| | 432 | return NULL; |
|---|
| | 433 | } |
|---|
| | 434 | |
|---|
| | 435 | Py_RETURN_NONE; |
|---|
| | 436 | } |
|---|
| | 437 | |
|---|
| | 438 | static PyObject * |
|---|
| | 439 | Wiimote_disable(Wiimote *self, PyObject *args, PyObject *kwds) |
|---|
| | 440 | { |
|---|
| | 441 | static char *kwlist[] = { "flags", NULL }; |
|---|
| | 442 | int flags = 0; |
|---|
| | 443 | |
|---|
| | 444 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { |
|---|
| | 445 | return NULL; |
|---|
| | 446 | } |
|---|
| | 447 | |
|---|
| | 448 | if (cwiid_disable(self->wiimote, flags)) { |
|---|
| | 449 | PyErr_SetString(PyExc_IOError, "cwiid_disable error"); |
|---|
| | 450 | return NULL; |
|---|
| | 451 | } |
|---|
| | 452 | |
|---|
| | 453 | Py_RETURN_NONE; |
|---|
| | 454 | } |
|---|
| | 455 | |
|---|
| | 456 | static PyObject *Wiimote_get_mesg(Wiimote *self, PyObject *args) |
|---|
| 439 | | static PyObject *Wiimote_get_state(PyObject *self, PyObject *args) |
|---|
| 440 | | { |
|---|
| 441 | | return notImplemented(); |
|---|
| | 487 | static PyObject *Wiimote_get_state(Wiimote* self, void *closure) |
|---|
| | 488 | { |
|---|
| | 489 | struct cwiid_state state; |
|---|
| | 490 | PyObject *PyState; |
|---|
| | 491 | |
|---|
| | 492 | if (cwiid_get_state(self->wiimote, &state)) { |
|---|
| | 493 | PyErr_SetString(PyExc_IOError, "get state error"); |
|---|
| | 494 | return NULL; |
|---|
| | 495 | } |
|---|
| | 496 | |
|---|
| | 497 | PyState = Py_BuildValue("{s:b,s:b,s:b,s:b,s:h}", |
|---|
| | 498 | "rpt_mode", state.rpt_mode, |
|---|
| | 499 | "led", state.led, |
|---|
| | 500 | "rumble", state.rumble, |
|---|
| | 501 | "battery", state.battery, |
|---|
| | 502 | "ext_type", state.ext_type); |
|---|
| | 503 | |
|---|
| | 504 | if (state.rpt_mode | CWIID_RPT_BTN) { |
|---|
| | 505 | PyObject *PyBtn = Py_BuildValue("i", state.buttons); |
|---|
| | 506 | if (!PyBtn) { |
|---|
| | 507 | Py_DECREF(PyState); |
|---|
| | 508 | return NULL; |
|---|
| | 509 | } |
|---|
| | 510 | if (PyDict_SetItemString(PyState, "buttons", PyBtn)) { |
|---|
| | 511 | Py_DECREF(PyState); |
|---|
| | 512 | Py_DECREF(PyBtn); |
|---|
| | 513 | return NULL; |
|---|
| | 514 | } |
|---|
| | 515 | Py_DECREF(PyBtn); |
|---|
| | 516 | } |
|---|
| | 517 | |
|---|
| | 518 | if (state.rpt_mode | CWIID_RPT_ACC) { |
|---|
| | 519 | PyObject *PyAcc = Py_BuildValue("{s:i,s:i,s:i}", |
|---|
| | 520 | "x", state.acc[CWIID_X], |
|---|
| | 521 | "y", state.acc[CWIID_Y], |
|---|
| | 522 | "z", state.acc[CWIID_Z]); |
|---|
| | 523 | if (!PyAcc) { |
|---|
| | 524 | Py_DECREF(PyState); |
|---|
| | 525 | return NULL; |
|---|
| | 526 | } |
|---|
| | 527 | if (PyDict_SetItemString(PyState, "acc", PyAcc)) { |
|---|
| | 528 | Py_DECREF(PyState); |
|---|
| | 529 | Py_DECREF(PyAcc); |
|---|
| | 530 | return NULL; |
|---|
| | 531 | } |
|---|
| | 532 | Py_DECREF(PyAcc); |
|---|
| | 533 | } |
|---|
| | 534 | |
|---|
| | 535 | return PyState; |
|---|