| 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-05-22 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 23 |
* * clarified variable names |
|---|
| 24 |
* |
|---|
| 25 |
* 2007-05-14 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 26 |
* * moved Wiimote class to separate files |
|---|
| 27 |
* |
|---|
| 28 |
* 2007-05-12 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 29 |
* * added keywords to read |
|---|
| 30 |
* * finished get_mesg |
|---|
| 31 |
* * cleaned up types in get_state |
|---|
| 32 |
* * finished processMesgs |
|---|
| 33 |
* |
|---|
| 34 |
* 2007-05-09 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 35 |
* * finished get_state |
|---|
| 36 |
* * fixed read buffer issue |
|---|
| 37 |
* * implemented write |
|---|
| 38 |
* * cleaned up types |
|---|
| 39 |
* * removed notImplemented (no longer needed) |
|---|
| 40 |
* |
|---|
| 41 |
* 2007-05-07 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 42 |
* * C-style comments |
|---|
| 43 |
* * changed struct name to Wiimote |
|---|
| 44 |
* * removed dlopen, unused includes |
|---|
| 45 |
* * spaces to tabs, misc stylistic changes to match CWiid code |
|---|
| 46 |
* * changed self types to Wiimote |
|---|
| 47 |
* * made bdaddr local |
|---|
| 48 |
* * improved error checking in Wiimote_init |
|---|
| 49 |
* * implemented disconnect, enable, disable |
|---|
| 50 |
* * partially implemented get_state |
|---|
| 51 |
* |
|---|
| 52 |
* 2007-05-07 Justin M. Tulloss <jmtulloss@gmail.com> |
|---|
| 53 |
* * Refactored according to dsmith's wishes, removed unnecessary locks |
|---|
| 54 |
* * implemented read |
|---|
| 55 |
* |
|---|
| 56 |
* 2007-04-26 Justin M. Tulloss <jmtulloss@gmail.com> |
|---|
| 57 |
* * Updated for new libcwiid API |
|---|
| 58 |
* |
|---|
| 59 |
* 2007-04-24 Justin M. Tulloss <jmtulloss@gmail.com> |
|---|
| 60 |
* * Initial Changelog |
|---|
| 61 |
*/ |
|---|
| 62 |
|
|---|
| 63 |
#include "Python.h" |
|---|
| 64 |
|
|---|
| 65 |
#include <stdlib.h> |
|---|
| 66 |
|
|---|
| 67 |
#include "cwiid.h" |
|---|
| 68 |
#include "structmember.h" |
|---|
| 69 |
|
|---|
| 70 |
/* externally defined types */ |
|---|
| 71 |
extern PyTypeObject Wiimote_Type; |
|---|
| 72 |
|
|---|
| 73 |
/* cwiid module initializer */ |
|---|
| 74 |
PyMODINIT_FUNC initcwiid(void); |
|---|
| 75 |
|
|---|
| 76 |
/* constants, enumerations */ |
|---|
| 77 |
#define CWIID_CONST_MACRO(a) {#a, CWIID_##a} |
|---|
| 78 |
static struct { |
|---|
| 79 |
char *name; |
|---|
| 80 |
int value; |
|---|
| 81 |
} cwiid_constants[] = { |
|---|
| 82 |
CWIID_CONST_MACRO(FLAG_MESG_IFC), |
|---|
| 83 |
CWIID_CONST_MACRO(FLAG_CONTINUOUS), |
|---|
| 84 |
CWIID_CONST_MACRO(FLAG_REPEAT_BTN), |
|---|
| 85 |
CWIID_CONST_MACRO(FLAG_NONBLOCK), |
|---|
| 86 |
CWIID_CONST_MACRO(RPT_STATUS), |
|---|
| 87 |
CWIID_CONST_MACRO(RPT_BTN), |
|---|
| 88 |
CWIID_CONST_MACRO(RPT_ACC), |
|---|
| 89 |
CWIID_CONST_MACRO(RPT_IR), |
|---|
| 90 |
CWIID_CONST_MACRO(RPT_NUNCHUK), |
|---|
| 91 |
CWIID_CONST_MACRO(RPT_CLASSIC), |
|---|
| 92 |
CWIID_CONST_MACRO(RPT_EXT), |
|---|
| 93 |
CWIID_CONST_MACRO(LED1_ON), |
|---|
| 94 |
CWIID_CONST_MACRO(LED2_ON), |
|---|
| 95 |
CWIID_CONST_MACRO(LED3_ON), |
|---|
| 96 |
CWIID_CONST_MACRO(LED4_ON), |
|---|
| 97 |
CWIID_CONST_MACRO(BTN_2), |
|---|
| 98 |
CWIID_CONST_MACRO(BTN_1), |
|---|
| 99 |
CWIID_CONST_MACRO(BTN_B), |
|---|
| 100 |
CWIID_CONST_MACRO(BTN_A), |
|---|
| 101 |
CWIID_CONST_MACRO(BTN_MINUS), |
|---|
| 102 |
CWIID_CONST_MACRO(BTN_HOME), |
|---|
| 103 |
CWIID_CONST_MACRO(BTN_LEFT), |
|---|
| 104 |
CWIID_CONST_MACRO(BTN_RIGHT), |
|---|
| 105 |
CWIID_CONST_MACRO(BTN_DOWN), |
|---|
| 106 |
CWIID_CONST_MACRO(BTN_UP), |
|---|
| 107 |
CWIID_CONST_MACRO(BTN_PLUS), |
|---|
| 108 |
CWIID_CONST_MACRO(NUNCHUK_BTN_Z), |
|---|
| 109 |
CWIID_CONST_MACRO(NUNCHUK_BTN_C), |
|---|
| 110 |
CWIID_CONST_MACRO(CLASSIC_BTN_UP), |
|---|
| 111 |
CWIID_CONST_MACRO(CLASSIC_BTN_LEFT), |
|---|
| 112 |
CWIID_CONST_MACRO(CLASSIC_BTN_ZR), |
|---|
| 113 |
CWIID_CONST_MACRO(CLASSIC_BTN_X), |
|---|
| 114 |
CWIID_CONST_MACRO(CLASSIC_BTN_A), |
|---|
| 115 |
CWIID_CONST_MACRO(CLASSIC_BTN_Y), |
|---|
| 116 |
CWIID_CONST_MACRO(CLASSIC_BTN_B), |
|---|
| 117 |
CWIID_CONST_MACRO(CLASSIC_BTN_ZL), |
|---|
| 118 |
CWIID_CONST_MACRO(CLASSIC_BTN_R), |
|---|
| 119 |
CWIID_CONST_MACRO(CLASSIC_BTN_PLUS), |
|---|
| 120 |
CWIID_CONST_MACRO(CLASSIC_BTN_HOME), |
|---|
| 121 |
CWIID_CONST_MACRO(CLASSIC_BTN_MINUS), |
|---|
| 122 |
CWIID_CONST_MACRO(CLASSIC_BTN_L), |
|---|
| 123 |
CWIID_CONST_MACRO(CLASSIC_BTN_DOWN), |
|---|
| 124 |
CWIID_CONST_MACRO(CLASSIC_BTN_RIGHT), |
|---|
| 125 |
CWIID_CONST_MACRO(RW_EEPROM), |
|---|
| 126 |
CWIID_CONST_MACRO(RW_REG), |
|---|
| 127 |
CWIID_CONST_MACRO(RW_DECODE), |
|---|
| 128 |
CWIID_CONST_MACRO(MAX_READ_LEN), |
|---|
| 129 |
CWIID_CONST_MACRO(X), |
|---|
| 130 |
CWIID_CONST_MACRO(Y), |
|---|
| 131 |
CWIID_CONST_MACRO(Z), |
|---|
| 132 |
CWIID_CONST_MACRO(IR_SRC_COUNT), |
|---|
| 133 |
CWIID_CONST_MACRO(IR_X_MAX), |
|---|
| 134 |
CWIID_CONST_MACRO(IR_Y_MAX), |
|---|
| 135 |
CWIID_CONST_MACRO(BATTERY_MAX), |
|---|
| 136 |
CWIID_CONST_MACRO(CLASSIC_L_STICK_MAX), |
|---|
| 137 |
CWIID_CONST_MACRO(CLASSIC_R_STICK_MAX), |
|---|
| 138 |
CWIID_CONST_MACRO(CLASSIC_LR_MAX), |
|---|
| 139 |
CWIID_CONST_MACRO(CMD_STATUS), |
|---|
| 140 |
CWIID_CONST_MACRO(CMD_LED), |
|---|
| 141 |
CWIID_CONST_MACRO(CMD_RUMBLE), |
|---|
| 142 |
CWIID_CONST_MACRO(CMD_RPT_MODE), |
|---|
| 143 |
CWIID_CONST_MACRO(MESG_STATUS), |
|---|
| 144 |
CWIID_CONST_MACRO(MESG_BTN), |
|---|
| 145 |
CWIID_CONST_MACRO(MESG_ACC), |
|---|
| 146 |
CWIID_CONST_MACRO(MESG_IR), |
|---|
| 147 |
CWIID_CONST_MACRO(MESG_NUNCHUK), |
|---|
| 148 |
CWIID_CONST_MACRO(MESG_CLASSIC), |
|---|
| 149 |
CWIID_CONST_MACRO(MESG_ERROR), |
|---|
| 150 |
CWIID_CONST_MACRO(MESG_UNKNOWN), |
|---|
| 151 |
CWIID_CONST_MACRO(EXT_NONE), |
|---|
| 152 |
CWIID_CONST_MACRO(EXT_NUNCHUK), |
|---|
| 153 |
CWIID_CONST_MACRO(EXT_CLASSIC), |
|---|
| 154 |
CWIID_CONST_MACRO(EXT_UNKNOWN), |
|---|
| 155 |
CWIID_CONST_MACRO(ERROR_DISCONNECT), |
|---|
| 156 |
CWIID_CONST_MACRO(ERROR_COMM), |
|---|
| 157 |
{NULL, 0} |
|---|
| 158 |
}; |
|---|
| 159 |
|
|---|
| 160 |
/* Associates cwiid functions with python ones */ |
|---|
| 161 |
static PyMethodDef Module_Methods[] = |
|---|
| 162 |
{ |
|---|
| 163 |
{NULL, NULL, 0, NULL} |
|---|
| 164 |
}; |
|---|
| 165 |
|
|---|
| 166 |
PyMODINIT_FUNC initcwiid(void) |
|---|
| 167 |
{ |
|---|
| 168 |
PyObject *Module; |
|---|
| 169 |
int i; |
|---|
| 170 |
|
|---|
| 171 |
PyEval_InitThreads(); |
|---|
| 172 |
|
|---|
| 173 |
if (PyType_Ready(&Wiimote_Type) < 0) { |
|---|
| 174 |
return; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
if (!(Module = Py_InitModule3("cwiid", Module_Methods, |
|---|
| 178 |
"CWiid Wiimote Interface"))) { |
|---|
| 179 |
return; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
Py_INCREF(&Wiimote_Type); |
|---|
| 183 |
PyModule_AddObject(Module, "Wiimote", (PyObject*)&Wiimote_Type); |
|---|
| 184 |
|
|---|
| 185 |
for (i = 0; cwiid_constants[i].name; i++) { |
|---|
| 186 |
/* No way to report errors from here, so just ignore them and hope |
|---|
| 187 |
* for segfault */ |
|---|
| 188 |
PyModule_AddIntConstant(Module, cwiid_constants[i].name, |
|---|
| 189 |
cwiid_constants[i].value); |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|