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
*, 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
*);
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
, 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.");
57 x52_debug(self
->handle
, 0);
62 PyX52_X52_dealloc(PyX52_X52_object
*self
)
64 if (self
->handle
!= NULL
)
66 x52_close(self
->handle
);
69 self
->ob_type
->tp_free((PyObject
*)self
);
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
);
81 PyX52_X52_settext(PyX52_X52_object
*self
, PyObject
*args
)
83 int ok
= PyArg_ParseTuple(args
, "");
87 PyX52_X52_setbri(PyX52_X52_object
*self
, PyObject
*args
)
89 int ok
= PyArg_ParseTuple(args
, "");
93 PyX52_X52_setled(PyX52_X52_object
*self
, PyObject
*args
)
95 int ok
= PyArg_ParseTuple(args
, "");
99 PyX52_X52_settime(PyX52_X52_object
*self
, PyObject
*args
, PyObject
*kws
)
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
))
114 x52_settime(self
->handle
, h24
, hour
, minute
);
121 PyX52_X52_setoffs(PyX52_X52_object
*self
, PyObject
*args
)
123 int ok
= PyArg_ParseTuple(args
, "");
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.");
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
))
143 x52_setsecond(self
->handle
, second
);
150 PyX52_X52_setdate(PyX52_X52_object
*self
, PyObject
*args
, PyObject
*kws
)
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
))
164 x52_setdate(self
->handle
, year
, month
, day
);
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
)
185 "PyX52.X52", /*tp_name*/
186 sizeof(PyX52_X52_object
), /*tp_basicsize*/
188 (destructor
)PyX52_X52_dealloc
, /*tp_dealloc*/
195 0, /*tp_as_sequence*/
203 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
204 "X52 joystick object", /* tp_doc */
207 0, /* tp_richcompare */
208 0, /* tp_weaklistoffset */
211 PyX52_X52_methods
, /* tp_methods */
216 0, /* tp_descr_get */
217 0, /* tp_descr_set */
218 0, /* tp_dictoffset */
219 (initproc
)PyX52_X52_init
, /* tp_init */
224 static PyMethodDef PyX52_methods
[] = {
228 #ifndef PyMODINIT_FUNC
229 #define PyMODINIT_FUNC void
236 PyX52_X52_type
.tp_new
= (newfunc
)PyX52_X52_new
;
237 if (PyType_Ready(&PyX52_X52_type
) < 0)
240 m
= Py_InitModule3("PyX52", PyX52_methods
, "Provides functions for handling Saitek X52, X52 Pro and Yoke joystick devices");
244 Py_INCREF(&PyX52_X52_type
);
245 PyModule_AddObject(m
, "X52", (PyObject
*) &PyX52_X52_type
);
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
);