root/trunk/python/cwiidmodule.c

Revision 125, 5.9 kB (checked in by dsmith, 2 years ago)

merged branches/dev into trunk

Line 
1 /*
2  * Copyright (C) 2007 Justin M. Tulloss <jmtulloss@gmail.com>
3  *
4  * Interface from Python to libcwiid
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * ChangeLog:
22  * 2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org>
23  * * removed Wiimote_FromC function
24  *
25  * 2007-06-01 L. Donnie Smith <cwiid@abstrakraft.org>
26  * * added CObjects for Wiimote_FromC and ConvertMesgArray
27  *
28  * 2007-05-22 L. Donnie Smith <cwiid@abstrakraft.org>
29  * * clarified variable names
30  *
31  * 2007-05-14 L. Donnie Smith <cwiid@abstrakraft.org>
32  * * moved Wiimote class to separate files
33  *
34  * 2007-05-12 L. Donnie Smith <cwiid@abstrakraft.org>
35  * * added keywords to read
36  * * finished get_mesg
37  * * cleaned up types in get_state
38  * * finished processMesgs
39  *
40  * 2007-05-09 L. Donnie Smith <cwiid@abstrakraft.org>
41  * * finished get_state
42  * * fixed read buffer issue
43  * * implemented write
44  * * cleaned up types
45  * * removed notImplemented (no longer needed)
46  *
47  * 2007-05-07 L. Donnie Smith <cwiid@abstrakraft.org>
48  * * C-style comments
49  * * changed struct name to Wiimote
50  * * removed dlopen, unused includes
51  * * spaces to tabs, misc stylistic changes to match CWiid code
52  * * changed self types to Wiimote
53  * * made bdaddr local
54  * * improved error checking in Wiimote_init
55  * * implemented disconnect, enable, disable
56  * * partially implemented get_state
57  *
58  * 2007-05-07 Justin M. Tulloss <jmtulloss@gmail.com>
59  * * Refactored according to dsmith's wishes, removed unnecessary locks
60  * * implemented read
61  *
62  * 2007-04-26 Justin M. Tulloss <jmtulloss@gmail.com>
63  * * Updated for new libcwiid API
64  *
65  * 2007-04-24 Justin M. Tulloss <jmtulloss@gmail.com>
66  * * Initial Changelog
67  */
68
69 #include "Python.h"
70
71 #include <stdlib.h>
72
73 #include "cwiid.h"
74 #include "structmember.h"
75
76 /* externally defined types */
77 extern PyTypeObject Wiimote_Type;
78 extern PyObject *ConvertMesgArray(int, union cwiid_mesg []);
79
80 /* cwiid module initializer */
81 PyMODINIT_FUNC initcwiid(void);
82
83 /* constants, enumerations */
84 #define CWIID_CONST_MACRO(a) {#a, CWIID_##a}
85 static struct {
86         char *name;
87         int value;
88 } cwiid_constants[] = {
89         CWIID_CONST_MACRO(FLAG_MESG_IFC),
90         CWIID_CONST_MACRO(FLAG_CONTINUOUS),
91         CWIID_CONST_MACRO(FLAG_REPEAT_BTN),
92         CWIID_CONST_MACRO(FLAG_NONBLOCK),
93         CWIID_CONST_MACRO(RPT_STATUS),
94         CWIID_CONST_MACRO(RPT_BTN),
95         CWIID_CONST_MACRO(RPT_ACC),
96         CWIID_CONST_MACRO(RPT_IR),
97         CWIID_CONST_MACRO(RPT_NUNCHUK),
98         CWIID_CONST_MACRO(RPT_CLASSIC),
99         CWIID_CONST_MACRO(RPT_EXT),
100         CWIID_CONST_MACRO(LED1_ON),
101         CWIID_CONST_MACRO(LED2_ON),
102         CWIID_CONST_MACRO(LED3_ON),
103         CWIID_CONST_MACRO(LED4_ON),
104         CWIID_CONST_MACRO(BTN_2),
105         CWIID_CONST_MACRO(BTN_1),
106         CWIID_CONST_MACRO(BTN_B),
107         CWIID_CONST_MACRO(BTN_A),
108         CWIID_CONST_MACRO(BTN_MINUS),
109         CWIID_CONST_MACRO(BTN_HOME),
110         CWIID_CONST_MACRO(BTN_LEFT),
111         CWIID_CONST_MACRO(BTN_RIGHT),
112         CWIID_CONST_MACRO(BTN_DOWN),
113         CWIID_CONST_MACRO(BTN_UP),
114         CWIID_CONST_MACRO(BTN_PLUS),
115         CWIID_CONST_MACRO(NUNCHUK_BTN_Z),
116         CWIID_CONST_MACRO(NUNCHUK_BTN_C),
117         CWIID_CONST_MACRO(CLASSIC_BTN_UP),
118         CWIID_CONST_MACRO(CLASSIC_BTN_LEFT),
119         CWIID_CONST_MACRO(CLASSIC_BTN_ZR),
120         CWIID_CONST_MACRO(CLASSIC_BTN_X),
121         CWIID_CONST_MACRO(CLASSIC_BTN_A),
122         CWIID_CONST_MACRO(CLASSIC_BTN_Y),
123         CWIID_CONST_MACRO(CLASSIC_BTN_B),
124         CWIID_CONST_MACRO(CLASSIC_BTN_ZL),
125         CWIID_CONST_MACRO(CLASSIC_BTN_R),
126         CWIID_CONST_MACRO(CLASSIC_BTN_PLUS),
127         CWIID_CONST_MACRO(CLASSIC_BTN_HOME),
128         CWIID_CONST_MACRO(CLASSIC_BTN_MINUS),
129         CWIID_CONST_MACRO(CLASSIC_BTN_L),
130         CWIID_CONST_MACRO(CLASSIC_BTN_DOWN),
131         CWIID_CONST_MACRO(CLASSIC_BTN_RIGHT),
132         CWIID_CONST_MACRO(RW_EEPROM),
133         CWIID_CONST_MACRO(RW_REG),
134         CWIID_CONST_MACRO(RW_DECODE),
135         CWIID_CONST_MACRO(MAX_READ_LEN),
136         CWIID_CONST_MACRO(X),
137         CWIID_CONST_MACRO(Y),
138         CWIID_CONST_MACRO(Z),
139         CWIID_CONST_MACRO(IR_SRC_COUNT),
140         CWIID_CONST_MACRO(IR_X_MAX),
141         CWIID_CONST_MACRO(IR_Y_MAX),
142         CWIID_CONST_MACRO(BATTERY_MAX),
143         CWIID_CONST_MACRO(CLASSIC_L_STICK_MAX),
144         CWIID_CONST_MACRO(CLASSIC_R_STICK_MAX),
145         CWIID_CONST_MACRO(CLASSIC_LR_MAX),
146         CWIID_CONST_MACRO(CMD_STATUS),
147         CWIID_CONST_MACRO(CMD_LED),
148         CWIID_CONST_MACRO(CMD_RUMBLE),
149         CWIID_CONST_MACRO(CMD_RPT_MODE),
150         CWIID_CONST_MACRO(MESG_STATUS),
151         CWIID_CONST_MACRO(MESG_BTN),
152         CWIID_CONST_MACRO(MESG_ACC),
153         CWIID_CONST_MACRO(MESG_IR),
154         CWIID_CONST_MACRO(MESG_NUNCHUK),
155         CWIID_CONST_MACRO(MESG_CLASSIC),
156         CWIID_CONST_MACRO(MESG_ERROR),
157         CWIID_CONST_MACRO(MESG_UNKNOWN),
158         CWIID_CONST_MACRO(EXT_NONE),
159         CWIID_CONST_MACRO(EXT_NUNCHUK),
160         CWIID_CONST_MACRO(EXT_CLASSIC),
161         CWIID_CONST_MACRO(EXT_UNKNOWN),
162         CWIID_CONST_MACRO(ERROR_DISCONNECT),
163         CWIID_CONST_MACRO(ERROR_COMM),
164         {NULL, 0}
165 };
166
167 /* Associates cwiid functions with python ones */
168 static PyMethodDef Module_Methods[] =
169 {
170         {NULL, NULL, 0, NULL}
171 };
172
173 PyMODINIT_FUNC initcwiid(void)
174 {
175         PyObject *Module;
176         PyObject *CObj;
177         int i;
178
179         PyEval_InitThreads();
180
181         if (PyType_Ready(&Wiimote_Type) < 0) {
182                 return;
183         }
184
185         if (!(Module = Py_InitModule3("cwiid", Module_Methods,
186           "CWiid Wiimote Interface"))) {
187                 return;
188         }
189
190         Py_INCREF(&Wiimote_Type);
191         PyModule_AddObject(Module, "Wiimote", (PyObject*)&Wiimote_Type);
192
193         for (i = 0; cwiid_constants[i].name; i++) {
194                 /* No way to report errors from here, so just ignore them and hope
195                  * for segfault */
196                 PyModule_AddIntConstant(Module, cwiid_constants[i].name,
197                                         cwiid_constants[i].value);
198         }
199
200         if (!(CObj = PyCObject_FromVoidPtr(ConvertMesgArray, NULL))) {
201                 return;
202         }
203         PyModule_AddObject(Module, "ConvertMesgArray", CObj);
204 }
205
Note: See TracBrowser for help on using the browser.