Changeset 110

Show
Ignore:
Timestamp:
05/09/07 21:26:58 (2 years ago)
Author:
dsmith
Message:

cwiidpy: write

Files:

Legend:

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

    r109 r110  
    2020 * 
    2121 * ChangeLog: 
    22  * 2007-05-07 L. Donnie Smith <cwiid@abstrakraft.org> 
     22 * 2007-05-09 L. Donnie Smith <cwiid@abstrakraft.org> 
    2323 * * finished get_state 
     24 * * fixed read buffer issue 
     25 * * implemented write 
     26 * * cleaned up types 
     27 * 
    2428 * 2007-05-07 L. Donnie Smith <cwiid@abstrakraft.org> 
    2529 * * C-style comments 
     
    6670static int Wiimote_init(Wiimote *self, PyObject *args, PyObject *kwds); 
    6771static PyObject *Wiimote_read(Wiimote *self, PyObject *args); 
    68 static PyObject *Wiimote_write(Wiimote *self, PyObject *args); 
     72static PyObject *Wiimote_write(Wiimote *self, PyObject *args, PyObject *kwds); 
    6973static PyObject *Wiimote_command(Wiimote *self, PyObject *args); 
    7074static PyObject *Wiimote_disconnect(Wiimote *self); 
     
    173177static PyObject *processMesgs(int mesg_count, union cwiid_mesg mesg[]); 
    174178static int cwiid_start(Wiimote *self, int flags); 
    175 static PyObject *notImplemented(); 
    176179 
    177180/* Associates cwiid functions with python ones */ 
     
    186189        /* {"__init__", constructorwii, METH_VARARGS, "cwiid(function)"}, */ 
    187190        {"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"}, 
    189193        {"command", (PyCFunction)Wiimote_command, METH_VARARGS, 
    190194         "send wiimote command"}, 
     
    354358        uint32_t length; 
    355359        void *buf; 
     360        int len; 
    356361 
    357362    PyArg_UnpackTuple(args, "read", 3, 3, &pyflags,&pyoffset,&pylength); 
     
    362367        PyErr_SetString(PyExc_TypeError, "arguments must be ints"); 
    363368    } 
    364          
     369 
    365370    /* marshal everything over */ 
    366371    flags  = (uint8_t)  PyInt_AsLong(pyflags); 
     
    368373        length = (uint32_t) PyInt_AsLong(pylength); 
    369374 
    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 
    376388        return pyRetBuf; 
    377389} 
    378390 
    379 static PyObject *Wiimote_write(Wiimote *self, PyObject *args) 
    380 
    381         return notImplemented(); 
     391static 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; 
    382410} 
    383411 
     
    423451{ 
    424452        static char *kwlist[] = { "flags", NULL }; 
    425         int flags = 0
     453        int flags
    426454 
    427455        if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { 
     
    441469{ 
    442470        static char *kwlist[] = { "flags", NULL }; 
    443         int flags = 0
     471        int flags
    444472 
    445473        if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &flags)) { 
     
    496524        } 
    497525 
    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}", 
    499527                                "rpt_mode", state.rpt_mode, 
    500528                                "led", state.led, 
     
    505533 
    506534        if (state.rpt_mode & CWIID_RPT_BTN) { 
    507                 PyObject *PyBtn = Py_BuildValue("i", state.buttons); 
     535                PyObject *PyBtn = Py_BuildValue("I", state.buttons); 
    508536                if (!PyBtn) { 
    509537                        Py_DECREF(PyState); 
     
    519547 
    520548        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}", 
    522550                                                                    "x", state.acc[CWIID_X], 
    523551                                            "y", state.acc[CWIID_Y], 
     
    556584 
    557585                        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}", 
    559587                                                        "pos", 
    560588                                                          "x", state.ir_src[i].pos[CWIID_X], 
     
    578606        case CWIID_EXT_NUNCHUK: 
    579607                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}", 
    581609                                              "stick", 
    582610                                                "x", state.ext.nunchuk.stick[CWIID_X], 
     
    634662 
    635663        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; 
    643664} 
    644665