Changeset 107

Show
Ignore:
Timestamp:
05/07/07 22:22:52 (2 years ago)
Author:
dsmith
Message:

more cwiidpy

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cwiidpy/cwiidmodule.c

    r106 r107  
    2424 * * changed struct name to Wiimote 
    2525 * * removed dlopen, unused includes 
    26  * * spaces to tabs 
    27  * * stylistic changes to match CWiid code 
    28  * * removed function casts to catch function type errors 
     26 * * spaces to tabs, misc stylistic changes to match CWiid code 
     27 * * changed self types to Wiimote 
     28 * * made bdaddr local 
     29 * * improved error checking in Wiimote_init 
     30 * * implemented disconnect, enable, disable 
     31 * * partially implemented get_state 
    2932 * 
    3033 * 2007-05-07 Justin M. Tulloss <jmtulloss@gmail.com> 
    3134 * * Refactored according to dsmith's wishes, removed unnecessary locks 
     35 * * implemented read 
    3236 * 
    3337 * 2007-04-26 Justin M. Tulloss <jmtulloss@gmail.com> 
     
    5155PyMODINIT_FUNC initcwiid(void); 
    5256 
    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 */ 
     58typedef struct { 
     59        PyObject_HEAD 
     60        cwiid_wiimote_t *wiimote; 
     61        PyObject *callback; 
     62} Wiimote; 
     63 
     64static int Wiimote_init(Wiimote *self, PyObject *args, PyObject *kwds); 
     65static PyObject *Wiimote_read(Wiimote *self, PyObject *args); 
     66static PyObject *Wiimote_write(Wiimote *self, PyObject *args); 
     67static PyObject *Wiimote_command(Wiimote *self, PyObject *args); 
     68static PyObject *Wiimote_disconnect(Wiimote *self); 
     69static PyObject * 
     70        Wiimote_enable(Wiimote *self, PyObject *args, PyObject *kwds); 
     71static PyObject * 
     72        Wiimote_disable(Wiimote *self, PyObject *args, PyObject *kwds); 
     73static PyObject *Wiimote_get_mesg(Wiimote *self, PyObject *args); 
     74static PyObject *Wiimote_set_callback(Wiimote *self, PyObject *args); 
     75static PyObject *Wiimote_get_state(Wiimote *self, void *closure); 
    6376 
    6477#define CONST_MACRO(a) {#a, CWIID_##a} 
     
    6780        int value; 
    6881}; 
     82 
    6983static struct cwiid_constant cwiid_constants[] = { 
    7084        CONST_MACRO(FLAG_MESG_IFC), 
     
    146160}; 
    147161 
    148 /* Types */ 
    149 typedef struct { 
    150         PyObject_HEAD 
    151         cwiid_wiimote_t *wiimote; 
    152         PyObject *callback; 
    153 } Wiimote; 
    154  
    155162/* Type private functions */ 
    156163static PyObject * 
    157164        Wiimote_new(PyTypeObject *type, PyObject *args, PyObject *kwds); 
    158 static void Wiimote_dealloc(PyObject *self); 
     165static void Wiimote_dealloc(Wiimote *self); 
    159166 
    160167/* Helper functions */ 
     
    166173static PyObject *notImplemented(); 
    167174 
    168 static bdaddr_t btAddr; 
    169  
    170  
    171175/* Associates cwiid functions with python ones */ 
    172176static PyMethodDef modMethods[] =  
     
    179183{ 
    180184        /* {"__init__", constructorwii, METH_VARARGS, "cwiid(function)"}, */ 
    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"}, 
    190199        {NULL, NULL} 
    191200}; 
     
    197206}; 
    198207 
     208static PyGetSetDef Wiimote_GetSet[] = { 
     209        {"state", (getter)Wiimote_get_state, NULL, "Wiimote state", NULL}, 
     210        {NULL} 
     211}; 
    199212 
    200213/* Defines a new type in python */ 
     
    205218        sizeof(Wiimote),                /* tp_basicsize */ 
    206219        0,                                              /* tp_itemsize */ 
    207         Wiimote_dealloc,              /* tp_dealloc */ 
     220        (destructor)Wiimote_dealloc,  /* tp_dealloc */ 
    208221        0,                                              /* tp_print */ 
    209222        0,                                              /* tp_getattr */ 
     
    230243        Wiimote_Methods,                /* tp_methods */ 
    231244        Wiimote_Members,                /* tp_members */ 
    232         0,                                            /* tp_getset */ 
     245        Wiimote_GetSet,                       /* tp_getset */ 
    233246        0,                                              /* tp_base */ 
    234247        0,                                              /* tp_dict */ 
     
    236249        0,                                              /* tp_descr_set */ 
    237250        0,                                              /* tp_dictoffset */ 
    238         Wiimote_init,                 /* tp_init */ 
     251        (initproc)Wiimote_init,       /* tp_init */ 
    239252        0,                                              /* tp_alloc */ 
    240253        Wiimote_new,                    /* tp_new */ 
     
    246259        struct cwiid_constant *constant; 
    247260 
     261        PyEval_InitThreads(); 
     262 
    248263        if (PyType_Ready(&Wiimote_Type) < 0) { 
    249264                return; 
    250265        } 
    251266 
    252         /* Open the cwiid library */ 
    253267        if (!(m = Py_InitModule3("cwiid", modMethods,  
    254268          "Module for accessing the wiimote through cwiid"))) { 
     
    273287        Wiimote* self; 
    274288 
    275         self = (Wiimote *) type->tp_alloc(type, 0); 
     289        if (!(self = (Wiimote *) type->tp_alloc(type, 0))) { 
     290                return NULL; 
     291        } 
     292 
     293        self->wiimote = NULL; 
     294        Py_INCREF(self->callback = Py_None); 
    276295 
    277296        return (PyObject*) self; 
    278297} 
    279298 
    280 static void Wiimote_dealloc(PyObject *self) 
    281 { 
    282         Wiimote *wm = (Wiimote *)self; 
    283  
    284         cwiid_disconnect(wm->wiimote); 
    285         Py_XDECREF(wm->callback); 
    286         self->ob_type->tp_free((PyObject*)self); 
     299static void Wiimote_dealloc(Wiimote *self) 
     300{ 
     301        if (self->wiimote) { 
     302                cwiid_disconnect(self->wiimote); 
     303        } 
     304        Py_XDECREF(self->callback); 
     305        self->ob_type->tp_free((PyObject *)self); 
    287306} 
    288307 
     
    290309{ 
    291310        cwiid_wiimote_t *theMote; 
    292         btAddr = *BDADDR_ANY; 
     311        bdaddr_t bdaddr = *BDADDR_ANY; 
    293312 
    294313        /* Set up wiimote */ 
    295         if(!(theMote = cwiid_connect(&btAddr, flags))) { 
     314        if(!(theMote = cwiid_connect(&bdaddr, flags))) { 
     315                PyErr_SetString(PyExc_IOError, "Could not connect to wiimote"); 
    296316                return -1; 
    297317        } 
     
    302322} 
    303323 
    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(); 
     324static 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        } 
    320336 
    321337        return 0; 
    322338} 
    323339 
    324 static PyObject *Wiimote_read(PyObject *self, PyObject *args) 
     340static PyObject *Wiimote_read(Wiimote *self, PyObject *args) 
    325341{ 
    326342        /* Python types */ 
     
    352368        /* TODO: More error checking */ 
    353369        buf = malloc(length); 
    354         cwiid_read(((Wiimote *)self)->wiimote,flags,offset,length,buf); 
     370        cwiid_read(self->wiimote,flags,offset,length,buf); 
    355371        pyRetBuf = PyBuffer_FromMemory((char*)buf, length); 
    356372        free(buf); 
     
    360376} 
    361377 
    362 static PyObject *Wiimote_write(PyObject *self, PyObject *args) 
     378static PyObject *Wiimote_write(Wiimote *self, PyObject *args) 
    363379{ 
    364380        return notImplemented(); 
    365381} 
    366382 
    367 static PyObject *Wiimote_command(PyObject *self, PyObject *args) 
     383static PyObject *Wiimote_command(Wiimote *self, PyObject *args) 
    368384{ 
    369385        /* Python types */ 
     
    385401 
    386402        /* finally, send the command to the wiimote */ 
    387         cwiid_command(((Wiimote *)self)->wiimote, command, flags); 
     403        cwiid_command(self->wiimote, command, flags); 
    388404        /* PyGILState_Release(gstate); */ 
    389405 
     
    391407} 
    392408 
    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) 
     409static 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 
     421static 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 
     438static 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 
     456static PyObject *Wiimote_get_mesg(Wiimote *self, PyObject *args) 
    409457{ 
    410458        union cwiid_mesg **mesgs; 
     
    416464} 
    417465 
    418 static PyObject *Wiimote_set_callback(PyObject *self, PyObject *args) 
     466static PyObject *Wiimote_set_callback(Wiimote *self, PyObject *args) 
    419467{ 
    420468        PyObject *pyCallback; 
     
    428476        /* Set this callback as an attribute in the class */ 
    429477        /* wasn't a callback before */  
    430         if (((Wiimote *)self)->callback== Py_None) { 
    431                 cwiid_set_mesg_callback(((Wiimote *)self)->wiimote,  
     478        if (self->callback== Py_None) { 
     479                cwiid_set_mesg_callback(self->wiimote,  
    432480                                        (cwiid_mesg_callback_t *) callbackBridge); 
    433481        } 
    434         ((Wiimote *)self)->callback = pyCallback; 
     482        self->callback = pyCallback; 
    435483 
    436484        Py_RETURN_NONE; 
    437485} 
    438486 
    439 static PyObject *Wiimote_get_state(PyObject *self, PyObject *args) 
    440 
    441         return notImplemented(); 
     487static 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; 
    442536} 
    443537