2 #include "structmember.h"
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
*);
21 static void PyX52_X52_dealloc(PyX52_X52_object
*);
23 static PyObject
*PyX52_X52_gettype(PyX52_X52_object
*);
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
*);
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);
43 return (PyObject
*)self
;
47 PyX52_X52_init(PyX52_X52_object
*self
)
49 self
->handle
= x52_init();
50 if (self
->handle
== NULL
)
52 PyErr_SetString(PyExc_RuntimeError
, "No compatible joysticks found.");
55 x52_debug(self
->handle
, 0);
60 PyX52_X52_dealloc(PyX52_X52_object
*self
)
62 if (self
->handle
!= NULL
)
64 x52_close(self
->handle
);
67 self
->ob_type
->tp_free((PyObject
*)self
);
71 PyX52_X52_gettype(PyX52_X52_object
*self
)
73 enum x52_type type
= x52_gettype(self
->handle
);
74 return Py_BuildValue("i", type
);
78 PyX52_X52_settext(PyX52_X52_object
*self
, PyObject
*args
)
80 if (x52_gettype(self
->handle
) == DEV_YOKE
)
82 PyErr_SetString(PyExc_RuntimeError
, "Cannot call settext on this device.");
86 int ok
= PyArg_ParseTuple(args
, "");
90 PyX52_X52_setbri(PyX52_X52_object
*self
, PyObject
*args
)
92 if (x52_gettype(self
->handle
) == DEV_YOKE
)
94 PyErr_SetString(PyExc_RuntimeError
, "Cannot call setbri on this device.");
98 int ok
= PyArg_ParseTuple(args
, "");
102 PyX52_X52_setled(PyX52_X52_object
*self
, PyObject
*args
)
104 if (x52_gettype(self
->handle
) != DEV_X52PRO
)
106 PyErr_SetString(PyExc_RuntimeError
, "Cannot call setled on this device.");
110 int ok
= PyArg_ParseTuple(args
, "");
114 PyX52_X52_settime(PyX52_X52_object
*self
, PyObject
*args
, PyObject
*kws
)
119 time_t t
= time(NULL
);
120 localtime_r(&t
, ltime
);
122 int hour
= ltime
->tm_hour
;
123 int minute
= ltime
->tm_min
;
125 static char *kwlist
[] = {"hour", "minute", "h24", NULL
};
126 if (!PyArg_ParseTupleAndKeywords(args
, kws
, "|iii", kwlist
, &hour
, &minute
, &h24
))
129 x52_settime(self
->handle
, h24
, hour
, minute
);
136 PyX52_X52_setoffs(PyX52_X52_object
*self
, PyObject
*args
)
138 if (x52_gettype(self
->handle
) == DEV_YOKE
)
140 PyErr_SetString(PyExc_RuntimeError
, "Cannot call setoffs on this device.");
145 int ok
= PyArg_ParseTuple(args
, "");
149 PyX52_X52_setsecond(PyX52_X52_object
*self
, PyObject
*args
, PyObject
*kws
)
151 if (x52_gettype(self
->handle
) != DEV_YOKE
)
153 PyErr_SetString(PyExc_RuntimeError
, "Cannot call setsecond on this device.");
158 time_t t
= time(NULL
);
159 localtime_r(&t
, ltime
);
161 int second
= ltime
->tm_sec
;
163 static char *kwlist
[] = {"second", NULL
};
164 if (!PyArg_ParseTupleAndKeywords(args
, kws
, "|iii", kwlist
, &second
))
167 x52_setsecond(self
->handle
, second
);
174 PyX52_X52_setdate(PyX52_X52_object
*self
, PyObject
*args
, PyObject
*kws
)
176 if (x52_gettype(self
->handle
) == DEV_YOKE
)
178 PyErr_SetString(PyExc_RuntimeError
, "Cannot call setdate on this device.");
183 time_t t
= time(NULL
);
184 localtime_r(&t
, ltime
);
186 int year
= ltime
->tm_year
- 100;
187 int month
= ltime
->tm_mon
;
188 int day
= ltime
->tm_mday
;
190 static char *kwlist
[] = {"year", "month", "day", NULL
};
191 if (!PyArg_ParseTupleAndKeywords(args
, kws
, "|iii", kwlist
, &year
, &month
, &day
))
194 if (!x52_setdate(self
->handle
, year
, month
, day
))
196 PyErr_SetString(PyExc_RuntimeError
, "Could not set date.");
204 static PyMethodDef PyX52_X52_methods
[] = {
205 {"gettype", (PyCFunction
)PyX52_X52_gettype
, METH_NOARGS
, "Get the type of X52 device."},
206 {"settext", (PyCFunction
)PyX52_X52_settext
, METH_VARARGS
, "Set the text on the MFD. (X52 and X52 Pro only.)"},
207 {"setbri", (PyCFunction
)PyX52_X52_setbri
, METH_VARARGS
, "Set the brightness of either the LEDs or the MFD. (X52 and X52 Pro only.)"},
208 {"setled", (PyCFunction
)PyX52_X52_setled
, METH_VARARGS
, "Set an LED's state. (X52 Pro only.)"},
209 {"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.)"},
210 {"setoffs", (PyCFunction
)PyX52_X52_setoffs
, METH_VARARGS
, "Set the time offset. (X52, X52 Pro.)"},
211 {"setsecond", (PyCFunction
)PyX52_X52_setsecond
, METH_VARARGS
, "Set the second. If no arguments are supplied, current second is used. (Yoke only.)"},
212 {"setdate", (PyCFunction
)PyX52_X52_setdate
, METH_VARARGS
| METH_KEYWORDS
, "Set the date. If no arguments are supplied, current date is used. (X52, X52 Pro.)"},
213 {NULL
, NULL
, 0, NULL
}
216 static PyTypeObject PyX52_X52_type
= {
217 PyObject_HEAD_INIT(NULL
)
219 "PyX52.X52", /*tp_name*/
220 sizeof(PyX52_X52_object
), /*tp_basicsize*/
222 (destructor
)PyX52_X52_dealloc
, /*tp_dealloc*/
229 0, /*tp_as_sequence*/
237 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
238 "X52 joystick object", /* tp_doc */
241 0, /* tp_richcompare */
242 0, /* tp_weaklistoffset */
245 PyX52_X52_methods
, /* tp_methods */
250 0, /* tp_descr_get */
251 0, /* tp_descr_set */
252 0, /* tp_dictoffset */
253 (initproc
)PyX52_X52_init
, /* tp_init */
255 (newfunc
)PyX52_X52_new
, /* tp_new */
258 static PyMethodDef PyX52_methods
[] = {
262 #ifndef PyMODINIT_FUNC
263 #define PyMODINIT_FUNC void
270 if (PyType_Ready(&PyX52_X52_type
) < 0)
273 m
= Py_InitModule3("PyX52", PyX52_methods
, "Provides functions for handling Saitek X52, X52 Pro and Yoke joystick devices.");
277 Py_INCREF(&PyX52_X52_type
);
278 PyModule_AddObject(m
, "X52", (PyObject
*) &PyX52_X52_type
);
293 PyModule_AddIntConstant(m
, "LED_FIRE", X52PRO_LED_FIRE
);
294 PyModule_AddIntConstant(m
, "LED_ARED", X52PRO_LED_ARED
);
295 PyModule_AddIntConstant(m
, "LED_AGREEN", X52PRO_LED_AGREEN
);
296 PyModule_AddIntConstant(m
, "LED_BRED", X52PRO_LED_BRED
);
297 PyModule_AddIntConstant(m
, "LED_BGREEN", X52PRO_LED_BGREEN
);
298 PyModule_AddIntConstant(m
, "LED_DRED", X52PRO_LED_DRED
);
299 PyModule_AddIntConstant(m
, "LED_DGREEN", X52PRO_LED_DGREEN
);
300 PyModule_AddIntConstant(m
, "LED_ERED", X52PRO_LED_ERED
);
301 PyModule_AddIntConstant(m
, "LED_EGREEN", X52PRO_LED_EGREEN
);
302 PyModule_AddIntConstant(m
, "LED_T1RED", X52PRO_LED_T1RED
);
303 PyModule_AddIntConstant(m
, "LED_T1GREEN", X52PRO_LED_T1GREEN
);
304 PyModule_AddIntConstant(m
, "LED_T2RED", X52PRO_LED_T2RED
);
305 PyModule_AddIntConstant(m
, "LED_T2GREEN", X52PRO_LED_T2GREEN
);
306 PyModule_AddIntConstant(m
, "LED_T3RED", X52PRO_LED_T3RED
);
307 PyModule_AddIntConstant(m
, "LED_T3GREEN", X52PRO_LED_T3GREEN
);
308 PyModule_AddIntConstant(m
, "LED_CORED", X52PRO_LED_CORED
);
309 PyModule_AddIntConstant(m
, "LED_COGREEN", X52PRO_LED_COGREEN
);
310 PyModule_AddIntConstant(m
, "LED_IRED", X52PRO_LED_IRED
);
311 PyModule_AddIntConstant(m
, "LED_IGREEN", X52PRO_LED_IGREEN
);
313 PyModule_AddIntConstant(m
, "TYPE_X52", DEV_X52
);
314 PyModule_AddIntConstant(m
, "TYPE_X52PRO", DEV_X52PRO
);
315 PyModule_AddIntConstant(m
, "TYPE_YOKE", DEV_YOKE
);