Changeset 111

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

cwiidpy: messages, state

Files:

Legend:

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

    r110 r111  
    2020 * 
    2121 * ChangeLog: 
     22 * 2007-05-12 L. Donnie Smith <cwiid@abstrakraft.org> 
     23 * * added keywords to read 
     24 * * finished get_mesg 
     25 * * cleaned up types in get_state 
     26 * * finished processMesgs 
     27 * 
    2228 * 2007-05-09 L. Donnie Smith <cwiid@abstrakraft.org> 
    2329 * * finished get_state 
     
    2531 * * implemented write 
    2632 * * cleaned up types 
     33 * * removed notImplemented (no longer needed) 
    2734 * 
    2835 * 2007-05-07 L. Donnie Smith <cwiid@abstrakraft.org> 
     
    5259 
    5360/* Standard Includes */ 
     61#include <errno.h> 
    5462#include <stdlib.h> 
    5563 
     
    6977 
    7078static int Wiimote_init(Wiimote *self, PyObject *args, PyObject *kwds); 
    71 static PyObject *Wiimote_read(Wiimote *self, PyObject *args); 
     79static PyObject *Wiimote_read(Wiimote *self, PyObject *args, PyObject *kwds); 
    7280static PyObject *Wiimote_write(Wiimote *self, PyObject *args, PyObject *kwds); 
    7381static PyObject *Wiimote_command(Wiimote *self, PyObject *args); 
     
    188196{ 
    189197        /* {"__init__", constructorwii, METH_VARARGS, "cwiid(function)"}, */ 
    190         {"read", (PyCFunction)Wiimote_read, METH_VARARGS, "read from wiimote"}, 
     198        {"read", (PyCFunction)Wiimote_read, METH_VARARGS | METH_KEYWORDS, 
     199         "read from wiimote"}, 
    191200        {"write", (PyCFunction)Wiimote_write, METH_VARARGS | METH_KEYWORDS, 
    192201         "write to wiimote"}, 
     
    282291                /* No way to report errors from here, so just ignore them and hope 
    283292                 * for segfault */ 
    284                 Py_INCREF(value = Py_BuildValue("i", constant->value)); 
     293                value = Py_BuildValue("i", constant->value); 
    285294                PyModule_AddObject(m, constant->name, value); 
    286295        } 
     
    322331                return -1; 
    323332        } 
    324         cwiid_set_data(theMote,(void *)self); /* keep pyobject with wiimote */ 
     333        cwiid_set_data(theMote,self); /* keep pyobject with wiimote */ 
    325334        self->wiimote = theMote; /* keep wiimote with pyobject */ 
    326         self->callback = Py_None; 
    327335        return 0; 
    328336} 
     
    344352} 
    345353 
    346 static PyObject *Wiimote_read(Wiimote *self, PyObject *args) 
    347 
    348         /* Python types */ 
    349         PyObject* pyflags; 
    350         PyObject* pyoffset; 
    351         PyObject* pylength; 
    352  
    353         PyObject* pyRetBuf; 
    354  
    355         /* C types */ 
    356         uint8_t flags; 
    357         uint32_t offset; 
    358         uint32_t length; 
     354static PyObject *Wiimote_read(Wiimote *self, PyObject *args, PyObject *kwds) 
     355
     356        static char *kwlist[] = { "flags", "offset", "len", NULL }; 
     357        unsigned char flags; 
     358        unsigned int offset; 
     359        unsigned int len; 
    359360        void *buf; 
    360         int len; 
    361  
    362     PyArg_UnpackTuple(args, "read", 3, 3, &pyflags,&pyoffset,&pylength); 
    363     if(!(PyInt_Check(pyflags)  
    364                 && PyInt_Check(pyoffset)  
    365                 && PyInt_Check(pylength))) 
    366     { 
    367         PyErr_SetString(PyExc_TypeError, "arguments must be ints"); 
    368     } 
    369  
    370     /* marshal everything over */ 
    371     flags  = (uint8_t)  PyInt_AsLong(pyflags); 
    372     offset = (uint32_t) PyInt_AsLong(pyoffset); 
    373         length = (uint32_t) PyInt_AsLong(pylength); 
    374  
    375         if (!(pyRetBuf = PyBuffer_New(length))) { 
    376                 return NULL; 
    377         } 
    378         if (PyObject_AsWriteBuffer(pyRetBuf, &buf, &len)) { 
     361        PyObject *pyRetBuf; 
     362 
     363        if (!PyArg_ParseTupleAndKeywords(args, kwds, "BII", kwlist, &flags, 
     364                                         &offset, &len)) { 
     365                return NULL; 
     366        } 
     367 
     368        if (!(pyRetBuf = PyBuffer_New(len))) { 
     369                return NULL; 
     370        } 
     371        if (PyObject_AsWriteBuffer(pyRetBuf, &buf, (int *)&len)) { 
    379372                Py_DECREF(pyRetBuf); 
    380373                return NULL; 
    381374        } 
    382         if (cwiid_read(self->wiimote,flags,offset,length,buf)) { 
     375        if (cwiid_read(self->wiimote,flags,offset,len,buf)) { 
    383376                PyErr_SetString(PyExc_IOError, "Wiimote read error"); 
    384377                Py_DECREF(pyRetBuf); 
     
    394387        unsigned char flags; 
    395388        unsigned int offset; 
    396         char *buf; 
     389        void *buf; 
    397390        int len; 
    398391 
     
    485478static PyObject *Wiimote_get_mesg(Wiimote *self, PyObject *args) 
    486479{ 
    487         union cwiid_mesg **mesgs
     480        union cwiid_mesg *mesg
    488481        int mesg_count; 
    489482 
    490483        /* get the messages from Mr. Wiimote */ 
    491  
    492         return processMesgs(mesg_count, *mesgs); 
     484        if (cwiid_get_mesg(self->wiimote, &mesg_count, &mesg)) { 
     485                if (errno == EAGAIN) { 
     486                        Py_RETURN_NONE; 
     487                } 
     488                else { 
     489                        PyErr_SetString(PyExc_IOError, "get_mesg error"); 
     490                        return NULL; 
     491                } 
     492        } 
     493 
     494        return processMesgs(mesg_count, mesg); 
    493495} 
    494496 
     
    524526        } 
    525527 
    526         PyState = Py_BuildValue("{s:B,s:B,s:B,s:B,s:I,s:I}", 
     528        PyState = Py_BuildValue("{s:B,s:B,s:B,s:B,s:i,s:i}", 
    527529                                "rpt_mode", state.rpt_mode, 
    528530                                "led", state.led, 
     
    582584                for (i=0; i < CWIID_IR_SRC_COUNT; i++) { 
    583585                        PyObject *PyIrSrc; 
     586                        PyObject *PySize; 
    584587 
    585588                        if (state.ir_src[i].valid) { 
    586                                 PyIrSrc = Py_BuildValue("{s:{s:I,s:I},s:b}", 
     589                                PyIrSrc = Py_BuildValue("{s:{s:I,s:I}}", 
    587590                                                        "pos", 
    588591                                                          "x", state.ir_src[i].pos[CWIID_X], 
    589                                                           "y", state.ir_src[i].pos[CWIID_Y], 
    590                                                         "size", state.ir_src[i].size); 
     592                                                          "y", state.ir_src[i].pos[CWIID_Y]); 
    591593                                if (!PyIrSrc) { 
    592594                                        Py_DECREF(PyState); 
    593595                                        return NULL; 
     596                                } 
     597 
     598                                if (state.ir_src[i].size != -1) { 
     599                                        if (!(PySize = PyInt_FromLong( 
     600                                          (long)state.ir_src[i].size))) { 
     601                                                Py_DECREF(PyState); 
     602                                                Py_DECREF(PyIrSrc); 
     603                                                return NULL; 
     604                                        } 
     605                                        if (PyDict_SetItemString(PyIrSrc, "size", PySize)) { 
     606                                                Py_DECREF(PyState); 
     607                                                Py_DECREF(PyIrSrc); 
     608                                                Py_DECREF(PySize); 
     609                                                return NULL; 
     610                                        } 
     611 
     612                                        Py_DECREF(PySize); 
    594613                                } 
    595614                        } 
     
    632651        case CWIID_EXT_CLASSIC: 
    633652                if (state.rpt_mode & CWIID_RPT_CLASSIC) { 
    634                         PyExt = Py_BuildValue("{s:{s:b,s:b},s:{s:b,s:b},s:b,s:b,s:i}", 
     653                        PyExt = Py_BuildValue("{s:{s:B,s:B},s:{s:B,s:B},s:B,s:B,s:I}", 
    635654                                              "l_stick", 
    636655                                                "x", state.ext.classic.l_stick[CWIID_X], 
     
    716735        PyObject *amesg; /* A single message (type, [arguments]) */ 
    717736        PyObject *mesgVal; /* Dictionary of arguments for a message */ 
    718  
    719         int i; 
    720  
    721         mesglist = PyList_New(0); 
    722         Py_XINCREF(mesglist); 
    723         for (i = 0; i < mesg_count; i++) 
    724         { 
    725                 mesgVal = PyDict_New(); 
    726                 Py_XINCREF(mesgVal); 
     737        PyObject *PyIrList; 
     738        int i, j; 
     739 
     740        if (!(mesglist = PyList_New(mesg_count))) { 
     741                return NULL; 
     742        } 
     743 
     744        for (i = 0; i < mesg_count; i++) { 
    727745                switch (mesg[i].type) { 
    728746                case CWIID_MESG_STATUS: 
    729                         PyDict_SetItemString(mesgVal, "battery", 
    730                           Py_BuildValue("B", mesg[i].status_mesg.battery)); 
    731                         switch (mesg[i].status_mesg.ext_type) { 
    732                         case CWIID_EXT_NONE: 
    733                                 PyDict_SetItemString(mesgVal, "extension", 
    734                                   Py_BuildValue("s","none")); 
    735                                 break; 
    736                         case CWIID_EXT_NUNCHUK: 
    737                                 PyDict_SetItemString(mesgVal, "extension",  
    738                                   Py_BuildValue("s","nunchuck")); 
    739                                 break; 
    740                         case CWIID_EXT_CLASSIC: 
    741                                 PyDict_SetItemString(mesgVal, "extension",  
    742                                   Py_BuildValue("s","classic")); 
    743                                 break; 
    744                         default: 
     747                        mesgVal = Py_BuildValue("{s:B,s:i}", 
     748                                                "battery", mesg[i].status_mesg.battery, 
     749                                                "ext_type", mesg[i].status_mesg.ext_type); 
     750                        break; 
     751                case CWIID_MESG_BTN: 
     752                        mesgVal = Py_BuildValue("{s:I}", 
     753                                                "buttons", mesg[i].btn_mesg.buttons); 
     754                        break; 
     755                case CWIID_MESG_ACC: 
     756                        mesgVal = Py_BuildValue("{s:{s:B,s:B,s:B}}", 
     757                                                "acc", 
     758                                                  "x", mesg[i].acc_mesg.acc[CWIID_X], 
     759                                                  "y", mesg[i].acc_mesg.acc[CWIID_Y], 
     760                                                  "z", mesg[i].acc_mesg.acc[CWIID_Z]); 
     761                        break; 
     762                case CWIID_MESG_IR: 
     763                        mesgVal = NULL; 
     764 
     765                        if (!(PyIrList = PyList_New(CWIID_IR_SRC_COUNT))) { 
    745766                                break; 
    746767                        } 
    747                         break; 
    748                 case CWIID_MESG_BTN: 
    749                         PyDict_SetItemString(mesgVal, "buttons",  
    750                           Py_BuildValue("H", mesg[i].btn_mesg.buttons)); 
    751                         break; 
    752                 case CWIID_MESG_ACC: 
    753                         PyDict_SetItemString(mesgVal, "x",  
    754                           Py_BuildValue("B", mesg[i].acc_mesg.acc[0])); 
    755                         PyDict_SetItemString(mesgVal, "y",  
    756                           Py_BuildValue("B", mesg[i].acc_mesg.acc[1])); 
    757                         PyDict_SetItemString(mesgVal, "z",  
    758                           Py_BuildValue("B", mesg[i].acc_mesg.acc[2])); 
    759                         break; 
    760                 case CWIID_MESG_IR: 
     768 
     769                        for (j=0; j < CWIID_IR_SRC_COUNT; j++) { 
     770                                PyObject *PyIrSrc; 
     771                                PyObject *PySize; 
     772 
     773                                if (mesg[i].ir_mesg.src[j].valid) { 
     774                                        PyIrSrc = Py_BuildValue("{s:{s:I,s:I}}", 
     775                                                     "pos", 
     776                                                       "x", mesg[i].ir_mesg.src[j].pos[CWIID_X], 
     777                                                       "y", mesg[i].ir_mesg.src[j].pos[CWIID_Y]); 
     778 
     779                                        if (!PyIrSrc) { 
     780                                                Py_DECREF(PyIrList); 
     781                                                PyIrList = NULL; 
     782                                                break; 
     783                                        } 
     784 
     785                                        if (mesg[i].ir_mesg.src[j].size != -1) { 
     786                                                if (!(PySize = PyInt_FromLong( 
     787                                                  (long)mesg[i].ir_mesg.src[j].size))) { 
     788                                                        Py_DECREF(PyIrList); 
     789                                                        Py_DECREF(PyIrSrc); 
     790                                                        PyIrList = NULL; 
     791                                                        break; 
     792                                                } 
     793                                                if (PyDict_SetItemString(PyIrSrc, "size", PySize)) { 
     794                                                        Py_DECREF(PyIrList); 
     795                                                        Py_DECREF(PyIrSrc); 
     796                                                        Py_DECREF(PySize); 
     797                                                        PyIrList = NULL; 
     798                                                        break; 
     799                                                } 
     800 
     801                                                Py_DECREF(PySize); 
     802                                        } 
     803                                } 
     804                                else { 
     805                                        Py_INCREF(PyIrSrc = Py_None); 
     806                                } 
     807                                PyList_SET_ITEM(PyIrList, j, PyIrSrc); 
     808                        } 
     809 
     810                        if (!PyIrList) { 
     811                                break; 
     812                        } 
     813 
     814                        mesgVal = Py_BuildValue("{s:O}", "src", PyIrList); 
     815                        Py_DECREF(PyIrList); 
    761816                        break; 
    762817                case CWIID_MESG_NUNCHUK: 
     818                        mesgVal = Py_BuildValue("{s:{s:B,s:B},s:{s:B,s:B,s:B},s:I}", 
     819                                                "stick", 
     820                                                  "x", mesg[i].nunchuk_mesg.stick[CWIID_X], 
     821                                                  "y", mesg[i].nunchuk_mesg.stick[CWIID_Y], 
     822                                                "acc", 
     823                                                  "x", mesg[i].nunchuk_mesg.acc[CWIID_X], 
     824                                                  "y", mesg[i].nunchuk_mesg.acc[CWIID_Y], 
     825                                                  "z", mesg[i].nunchuk_mesg.acc[CWIID_Z], 
     826                                                "buttons", mesg[i].nunchuk_mesg.buttons); 
    763827                        break; 
    764828                case CWIID_MESG_CLASSIC: 
     829                        mesgVal = Py_BuildValue("{s:{s:B,s:B},s:{s:B,s:B},s:B,s:B,s:I}", 
     830                                     "l_stick", 
     831                                       "x", mesg[i].classic_mesg.l_stick[CWIID_X], 
     832                                       "y", mesg[i].classic_mesg.l_stick[CWIID_Y], 
     833                                     "r_stick", 
     834                                       "x", mesg[i].classic_mesg.r_stick[CWIID_X], 
     835                                       "y", mesg[i].classic_mesg.r_stick[CWIID_Y], 
     836                                     "l", mesg[i].classic_mesg.l, 
     837                                     "r", mesg[i].classic_mesg.r, 
     838                                     "buttons", mesg[i].classic_mesg.buttons); 
    765839                        break; 
    766840                case CWIID_MESG_ERROR: 
    767                         switch(mesg[i].error_mesg.error) { 
    768                         case CWIID_ERROR_DISCONNECT: 
    769                                 PyDict_SetItemString(mesgVal, "error", 
    770                                   Py_BuildValue("s", "Wiimote was disconnected")); 
    771                                 break; 
    772                         case CWIID_ERROR_COMM: 
    773                                 PyDict_SetItemString(mesgVal, "error", 
    774                                   Py_BuildValue("s", "Communication error occurred")); 
    775                                 break; 
    776                         default: 
    777                                 PyDict_SetItemString(mesgVal, "error", 
    778                                   Py_BuildValue("s","An Unknown error occurred")); 
    779                                 break; 
    780                         } 
     841                        mesgVal = Py_BuildValue("{s:i}", 
     842                                                "error",mesg[i].error_mesg.error); 
    781843                        break; 
    782844                default: 
    783                         PyDict_SetItemString(mesgVal, "error", 
    784                           Py_BuildValue("s","Unknown message arrived")); 
     845                        Py_INCREF(mesgVal = Py_None); 
    785846                        break; 
     847                } 
     848 
     849                if (!mesgVal) { 
     850                        return NULL; 
    786851                } 
    787852 
    788853                /* Finally Put the type next to the message in a tuple and 
    789854                 * append them to the list of messages */ 
    790                 amesg = Py_BuildValue("(iO)", mesg[i].type, mesgVal); 
    791                 Py_XINCREF(amesg); 
    792                 PyList_Append(mesglist, amesg); 
     855                if (!(amesg = Py_BuildValue("(iO)", mesg[i].type, mesgVal))) { 
     856                        Py_DECREF(mesgVal); 
     857                        return NULL; 
     858                } 
     859                Py_DECREF(mesgVal); 
     860                PyList_SET_ITEM(mesglist, i, amesg); 
    793861        } 
    794862