Changeset 123 for branches/dev

Show
Ignore:
Timestamp:
06/05/07 19:07:48 (2 years ago)
Author:
dsmith
Message:

Python extension updates

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/dev/ChangeLog

    r120 r123  
     12007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> 
     2        python 
     3        * removed Wiimote_FromC function 
     4        * added bdaddr argument to Wiimote.init 
     5        * overloaded Wiimote.init to accept CObject (existing wiimote), 
     6          and logic to avoid closing it on dealloc 
     7 
    182007-06-01 Nick <nickishappy@gmail.com> 
    29        lswm 
  • branches/dev/python/Makefile.in

    r119 r123  
    1818 
    1919clean: 
    20         rm -r build 
     20        rm -rf build 
    2121 
    2222distclean: clean 
  • branches/dev/python/Wiimote.c

    r119 r123  
    2020 * 
    2121 * ChangeLog: 
     22 * 2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> 
     23 * * removed Wiimote_FromC function 
     24 * * added bdaddr argument to Wiimote.init 
     25 * * overloaded Wiimote.init to accept CObject (existing wiimote), 
     26 *   and logic to avoid closing it on dealloc 
     27 * 
    2228 * 2007-06-01 L. Donnie Smith <cwiid@abstrakraft.org> 
    2329 * * added Wiimote_FromC 
     
    4652#include "structmember.h" 
    4753#include <errno.h> 
     54#include <bluetooth/bluetooth.h> 
    4855#include "cwiid.h" 
    4956 
     
    5259        cwiid_wiimote_t *wiimote; 
    5360        PyObject *callback; 
     61        char close_on_dealloc; 
    5462} Wiimote; 
    5563 
     
    8593static cwiid_mesg_callback_t CallbackBridge; 
    8694PyObject *ConvertMesgArray(int mesg_count, union cwiid_mesg mesg[]); 
    87 PyObject *Wiimote_FromC(cwiid_wiimote_t *wiimote); 
    88 static int cwiid_start(Wiimote *self, int flags); 
    8995 
    9096static PyMethodDef Wiimote_Methods[] = 
     
    174180        self->wiimote = NULL; 
    175181        Py_INCREF(self->callback = Py_None); 
     182        self->close_on_dealloc = 0; 
    176183 
    177184        return (PyObject*) self; 
     
    180187static void Wiimote_dealloc(Wiimote *self) 
    181188{ 
    182         if (self->wiimote) { 
     189        if (self->close_on_dealloc && self->wiimote) { 
    183190                cwiid_close(self->wiimote); 
    184191        } 
     
    190197{ 
    191198        cwiid_wiimote_t *wiimote; 
    192         bdaddr_t bdaddr = *BDADDR_ANY; 
    193199 
    194200        /* Set up wiimote */ 
    195         if(!(wiimote = cwiid_open(&bdaddr, flags))) { 
    196                 PyErr_SetString(PyExc_IOError, "Could not open wiimote"); 
    197                 return -1; 
    198         } 
    199201 
    200202        /* keep pyobject with wiimote */ 
     
    207209static int Wiimote_init(Wiimote* self, PyObject* args, PyObject *kwds) 
    208210{ 
    209         static char *kwlist[] = {"flags", NULL}; 
     211        static char *kwlist[] = {"bdaddr", "flags", NULL}; 
     212        PyObject *PyObj; 
     213        cwiid_wiimote_t *wiimote = NULL; 
     214        char *str_bdaddr = NULL; 
     215        bdaddr_t bdaddr; 
    210216        int flags = 0; 
    211217 
    212         if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:cwiid.Wiimote.init", 
    213                                          kwlist, &flags)) { 
    214                 return -1; 
    215         } 
    216  
    217         if (cwiid_start(self, flags)) { 
    218                 return -1; 
    219         } 
    220  
     218        /* Overloaded function - if a single CObject is passed in, it's 
     219         * an existing CObject.  Otherwise, create a new one */ 
     220        if (PyTuple_Size(args) == 1) { 
     221                PyObj = PyTuple_GET_ITEM(args, 0); 
     222                if (PyCObject_Check(PyObj)) { 
     223                        wiimote = PyCObject_AsVoidPtr(PyObj); 
     224                        self->close_on_dealloc = 0; 
     225                } 
     226        } 
     227 
     228        if (!wiimote) { 
     229                if (!PyArg_ParseTupleAndKeywords(args, kwds, "|si:cwiid.Wiimote.init", 
     230                                                 kwlist, &str_bdaddr, &flags)) { 
     231                        return -1; 
     232                } 
     233 
     234                if (str_bdaddr) { 
     235                        if (str2ba(str_bdaddr, &bdaddr)) { 
     236                                PyErr_SetString(PyExc_IOError, "bad bdaddr"); 
     237                                return -1; 
     238                        } 
     239                } 
     240                else { 
     241                        bdaddr = *BDADDR_ANY; 
     242                } 
     243 
     244                if (!(wiimote = cwiid_open(&bdaddr, flags))) { 
     245                        PyErr_SetString(PyExc_IOError, "Could not open wiimote"); 
     246                        return -1; 
     247                } 
     248                else { 
     249                        self->close_on_dealloc = 1; 
     250                } 
     251        } 
     252 
     253        cwiid_set_data(wiimote, self); 
     254        self->wiimote = wiimote; 
    221255        return 0; 
    222256} 
     
    668702} 
    669703 
    670 PyObject *Wiimote_FromC(cwiid_wiimote_t *wiimote) 
    671 { 
    672         Wiimote *PyWiimote; 
    673  
    674         if (!(PyWiimote = (Wiimote *)PyObject_CallMethod((PyObject *)&Wiimote_Type, 
    675                                                          "__new__", "(O)", 
    676                                                          &Wiimote_Type))) { 
    677                 return NULL; 
    678         } 
    679  
    680         cwiid_set_data(wiimote, PyWiimote); 
    681         PyWiimote->wiimote = wiimote; 
    682  
    683         return (PyObject *)PyWiimote; 
    684 } 
    685  
    686704/* This is the function responsible for marshaling the cwiid messages from 
    687705 * C to python. It's rather complicated since it uses a complex C union 
     
    694712 *  
    695713 * Ex: 
    696  * mesgs =>[(CWIID_STATUS_MESG,{"battery":battery,"ext_type":ext_type}), 
    697  *          (CWIID_BTN_MESG,buttons),  
    698  *          (CWIID_ACC_MESG,(x,y,z)), 
    699  *          (CWIID_IR_MESG,[{"pos":(x,y),"size":size}, ...]), 
    700  *          (CWIID_NUNCHUK_MESG,{"stick":(x,y),"acc":(x,y,z), 
     714 * mesgs =>[(cwiid.STATUS_MESG,{"battery":battery,"ext_type":ext_type}), 
     715 *          (cwiid.BTN_MESG,buttons),  
     716 *          (cwiid.ACC_MESG,(x,y,z)), 
     717 *          (cwiid.IR_MESG,[{"pos":(x,y),"size":size}, ...]), 
     718 *          (cwiid.NUNCHUK_MESG,{"stick":(x,y),"acc":(x,y,z), 
    701719 *                               "buttons":buttons}, 
    702  *          (CWIID_CLASSIC_MESG,{"l_stick":(x,y),"r_stick":(x,y),"l":l,"r":r, 
     720 *          (cwiid.CLASSIC_MESG,{"l_stick":(x,y),"r_stick":(x,y),"l":l,"r":r, 
    703721 *                               "buttons":buttons}, 
    704  *          (CWIID_ERROR_MESG,error)] 
     722 *          (cwiid.ERROR_MESG,error)] 
    705723 */ 
    706724PyObject *ConvertMesgArray(int mesg_count, union cwiid_mesg mesg[]) 
  • branches/dev/python/cwiidmodule.c

    r118 r123  
    2020 * 
    2121 * ChangeLog: 
     22 * 2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> 
     23 * * removed Wiimote_FromC function 
     24 * 
    2225 * 2007-06-01 L. Donnie Smith <cwiid@abstrakraft.org> 
    2326 * * added CObjects for Wiimote_FromC and ConvertMesgArray 
     
    7477extern PyTypeObject Wiimote_Type; 
    7578extern PyObject *ConvertMesgArray(int, union cwiid_mesg []); 
    76 extern PyObject *Wiimote_FromC(cwiid_wiimote_t *); 
    7779 
    7880/* cwiid module initializer */ 
     
    200202        } 
    201203        PyModule_AddObject(Module, "ConvertMesgArray", CObj); 
    202  
    203         if (!(CObj = PyCObject_FromVoidPtr(Wiimote_FromC, NULL))) { 
    204                 return; 
    205         } 
    206         PyModule_AddObject(Module, "Wiimote_FromC", CObj); 
    207204} 
    208205