corrected case of Py_ParseTupleAndKeywords
[PyX52.git] / PyX52.c
blob8f77a69f0c7e428ef180f25b812ba4f838828230
1 #include <Python.h>
2 #include "structmember.h"
3 #include "datetime.h"
5 #include <time.h>
7 #include <x52pro.h>
9 typedef struct
11 PyObject_HEAD
12 struct x52 *handle;
13 } PyX52_X52_object;
15 static PyTypeObject PyX52_X52_type;
16 static PyMethodDef PyX52_methods[];
17 static PyMethodDef PyX52_X52_methods[];
19 static PyObject *PyX52_X52_new(PyTypeObject *, PyObject *);
20 static int PyX52_X52_init(PyX52_X52_object *, PyObject *);
21 static void PyX52_X52_dealloc(PyX52_X52_object *);
23 static PyObject *PyX52_X52_gettype(PyX52_X52_object *, PyObject *);
24 static PyObject *PyX52_X52_settext(PyX52_X52_object *, PyObject *);
25 static PyObject *PyX52_X52_setbri(PyX52_X52_object *, PyObject *);
26 static PyObject *PyX52_X52_setled(PyX52_X52_object *, PyObject *);
27 static PyObject *PyX52_X52_settime(PyX52_X52_object *, PyObject *, PyObject *);
28 static PyObject *PyX52_X52_setoffs(PyX52_X52_object *, PyObject *);
29 static PyObject *PyX52_X52_setsecond(PyX52_X52_object *, PyObject *, PyObject *);
30 static PyObject *PyX52_X52_setdate(PyX52_X52_object *, PyObject *, PyObject *);
32 static PyObject *
33 PyX52_X52_new(PyTypeObject *type, PyObject *args)
35 int ok = PyArg_ParseTuple(args, "");
37 PyX52_X52_object *self;
38 self = (PyX52_X52_object *)type->tp_alloc(type, 0);
39 if (self != NULL)
41 self->handle = NULL;
43 return (PyObject *)self;
46 static int
47 PyX52_X52_init(PyX52_X52_object *self, PyObject *args)
49 int ok = PyArg_ParseTuple(args, "");
51 self->handle = x52_init();
52 if (self->handle == NULL)
54 PyErr_SetString(PyExc_RuntimeError, "No compatible joysticks found.");
55 return -1;
57 x52_debug(self->handle, 0);
58 return 0;
61 static void
62 PyX52_X52_dealloc(PyX52_X52_object *self)
64 if (self->handle != NULL)
66 x52_close(self->handle);
68 self->handle = NULL;
69 self->ob_type->tp_free((PyObject *)self);
72 static PyObject *
73 PyX52_X52_gettype(PyX52_X52_object *self, PyObject *args)
75 int ok = PyArg_ParseTuple(args, "");
76 enum x52_type type = x52_gettype(self->handle);
77 return Py_BuildValue("i", type);
80 static PyObject *
81 PyX52_X52_settext(PyX52_X52_object *self, PyObject *args)
83 int ok = PyArg_ParseTuple(args, "");
86 static PyObject *
87 PyX52_X52_setbri(PyX52_X52_object *self, PyObject *args)
89 int ok = PyArg_ParseTuple(args, "");
92 static PyObject *
93 PyX52_X52_setled(PyX52_X52_object *self, PyObject *args)
95 int ok = PyArg_ParseTuple(args, "");
98 static PyObject *
99 PyX52_X52_settime(PyX52_X52_object *self, PyObject *args, PyObject *kws)
101 int h24 = 1;
103 struct tm *ltime;
104 time_t t = time(NULL);
105 localtime_r(&t, ltime);
107 int hour = ltime->tm_hour;
108 int minute = ltime->tm_min;
110 static char *kwlist[] = {"hour", "minute", "h24", NULL};
111 if (!PyArg_ParseTupleAndKeywords(args, kws, "|iii", kwlist, &hour, &minute, &h24))
112 return NULL;
114 x52_settime(self->handle, h24, hour, minute);
116 Py_INCREF(Py_None);
117 return Py_None;
120 static PyObject *
121 PyX52_X52_setoffs(PyX52_X52_object *self, PyObject *args)
123 int ok = PyArg_ParseTuple(args, "");
126 static PyObject *
127 PyX52_X52_setsecond(PyX52_X52_object *self, PyObject *args, PyObject *kws)
129 if (x52_gettype(self->handle) != DEV_YOKE)
130 PyErr_SetString(PyExc_RuntimeError, "Cannot call setsecond on this device.");
131 return NULL;
133 struct tm *ltime;
134 time_t t = time(NULL);
135 localtime_r(&t, ltime);
137 int second = ltime->tm_sec;
139 static char *kwlist[] = {"second", NULL};
140 if (!PyArg_ParseTupleAndKeywords(args, kws, "|iii", kwlist, &second))
141 return NULL;
143 x52_setsecond(self->handle, second);
145 Py_INCREF(Py_None);
146 return Py_None;
149 static PyObject *
150 PyX52_X52_setdate(PyX52_X52_object *self, PyObject *args, PyObject *kws)
152 struct tm *ltime;
153 time_t t = time(NULL);
154 localtime_r(&t, ltime);
156 int year = ltime->tm_year - 100;
157 int month = ltime->tm_mon;
158 int day = ltime->tm_mday;
160 static char *kwlist[] = {"year", "month", "day", NULL};
161 if (!PyArg_ParseTupleAndKeywords(args, kws, "|iii", kwlist, &year, &month, &day))
162 return NULL;
164 x52_setdate(self->handle, year, month, day);
166 Py_INCREF(Py_None);
167 return Py_None;
170 static PyMethodDef PyX52_X52_methods[] = {
171 {"gettype", (PyCFunction)PyX52_X52_gettype, METH_VARARGS, "Get the type of X52 device."},
172 {"settext", (PyCFunction)PyX52_X52_settext, METH_VARARGS, "Set the text on the MFD. (X52 and X52 Pro only.)"},
173 {"setbri", (PyCFunction)PyX52_X52_setbri, METH_VARARGS, "Set the brightness of either the LEDs or the MFD. (X52 and X52 Pro only.)"},
174 {"setled", (PyCFunction)PyX52_X52_setled, METH_VARARGS, "Set an LED's state. (X52 Pro only.)"},
175 {"settime", (PyCFunction)PyX52_X52_settime, METH_VARARGS | METH_KEYWORDS, "Set the MFD display time. If arguments are not supplied, sets the current time in 24 hour format. (X52, X52 Pro and Yoke.)"},
176 {"setoffs", (PyCFunction)PyX52_X52_setoffs, METH_VARARGS, "Set the time offset. (X52, X52 Pro.)"},
177 {"setsecond", (PyCFunction)PyX52_X52_setsecond, METH_VARARGS, "Set the second. If no arguments are supplied, current second is used. (Yoke only.)"},
178 {"setdate", (PyCFunction)PyX52_X52_setdate, METH_VARARGS | METH_KEYWORDS, "Set the date. If no arguments are supplied, current date is used. (X52, X52 Pro.)"},
179 {NULL, NULL, 0, NULL}
182 static PyTypeObject PyX52_X52_type = {
183 PyObject_HEAD_INIT(NULL)
184 0, /*ob_size*/
185 "PyX52.X52", /*tp_name*/
186 sizeof(PyX52_X52_object), /*tp_basicsize*/
187 0, /*tp_itemsize*/
188 (destructor)PyX52_X52_dealloc, /*tp_dealloc*/
189 0, /*tp_print*/
190 0, /*tp_getattr*/
191 0, /*tp_setattr*/
192 0, /*tp_compare*/
193 0, /*tp_repr*/
194 0, /*tp_as_number*/
195 0, /*tp_as_sequence*/
196 0, /*tp_as_mapping*/
197 0, /*tp_hash */
198 0, /*tp_call*/
199 0, /*tp_str*/
200 0, /*tp_getattro*/
201 0, /*tp_setattro*/
202 0, /*tp_as_buffer*/
203 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
204 "X52 joystick object", /* tp_doc */
205 0, /*tp_traverse*/
206 0, /* tp_clear */
207 0, /* tp_richcompare */
208 0, /* tp_weaklistoffset */
209 0, /* tp_iter */
210 0, /* tp_iternext */
211 PyX52_X52_methods, /* tp_methods */
212 0, /* tp_members */
213 0, /* tp_getset */
214 0, /* tp_base */
215 0, /* tp_dict */
216 0, /* tp_descr_get */
217 0, /* tp_descr_set */
218 0, /* tp_dictoffset */
219 (initproc)PyX52_X52_init, /* tp_init */
220 0, /* tp_alloc */
221 0, /* tp_new */
224 static PyMethodDef PyX52_methods[] = {
225 {NULL}
228 #ifndef PyMODINIT_FUNC
229 #define PyMODINIT_FUNC void
230 #endif
231 PyMODINIT_FUNC
232 initPyX52(void)
234 PyObject *m;
236 PyX52_X52_type.tp_new = (newfunc)PyX52_X52_new;
237 if (PyType_Ready(&PyX52_X52_type) < 0)
238 return;
240 m = Py_InitModule3("PyX52", PyX52_methods, "Provides functions for handling Saitek X52, X52 Pro and Yoke joystick devices");
241 if (m == NULL)
242 return;
244 Py_INCREF(&PyX52_X52_type);
245 PyModule_AddObject(m, "X52", (PyObject *) &PyX52_X52_type);
248 * Fire
253 * T1
254 * T2
255 * T3
256 * CO
260 PyModule_AddIntConstant(m, "LED_FIRE", 1);
261 PyModule_AddIntConstant(m, "LED_ARED", 2);
262 PyModule_AddIntConstant(m, "LED_AGREEN", 3);
263 PyModule_AddIntConstant(m, "LED_BRED", 4);
264 PyModule_AddIntConstant(m, "LED_BGREEN", 5);
265 PyModule_AddIntConstant(m, "LED_DRED", 6);
266 PyModule_AddIntConstant(m, "LED_DGREEN", 7);
267 PyModule_AddIntConstant(m, "LED_ERED", 8);
268 PyModule_AddIntConstant(m, "LED_EGREEN", 9);
269 PyModule_AddIntConstant(m, "LED_T1RED", 10);
270 PyModule_AddIntConstant(m, "LED_T1GREEN", 11);
271 PyModule_AddIntConstant(m, "LED_T2RED", 12);
272 PyModule_AddIntConstant(m, "LED_T2GREEN", 13);
273 PyModule_AddIntConstant(m, "LED_T3RED", 14);
274 PyModule_AddIntConstant(m, "LED_T3GREEN", 15);
275 PyModule_AddIntConstant(m, "LED_CORED", 16);
276 PyModule_AddIntConstant(m, "LED_COGREEN", 17);
277 PyModule_AddIntConstant(m, "LED_IRED", 18);
278 PyModule_AddIntConstant(m, "LED_IGREEN", 19);
280 PyModule_AddIntConstant(m, "TYPE_X52", DEV_X52);
281 PyModule_AddIntConstant(m, "TYPE_X52PRO", DEV_X52PRO);
282 PyModule_AddIntConstant(m, "TYPE_YOKE", DEV_YOKE);