-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjoy.py
More file actions
61 lines (50 loc) · 1.31 KB
/
joy.py
File metadata and controls
61 lines (50 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
"""
struct js_event {
__u32 time; /* event timestamp in milliseconds */
__s16 value; /* value */
__u8 type; /* event type */
__u8 number; /* axis/button number */
};
"""
import os
import time
from ctypes import *
class js_event(Structure):
_fields_ = [("time", c_uint32),
("value", c_int16),
("type", c_uint8),
("number", c_uint8)]
def __repr__(self):
return "js_event<time: %d, value: %6d, type: %3d, number: %2d>" % (
self.time, self.value, self.type, self.number)
class Joy():
def __init__(self, JoyDev='/dev/input/js0'):
self.joy = os.open(JoyDev, os.O_RDONLY|os.O_NONBLOCK)
#print self.joy
self.a = js_event()
self.Speed=0.
self.Angle=0.
self.update()
def update(self):
try:
data = os.read(self.joy, sizeof(self.a))
while len(data)>0:
memmove(addressof(self.a),data , sizeof(self.a))
#print self.a
if self.a.type==2:
if self.a.number == 1:
self.Speed=float(-self.a.value)/2**15
elif self.a.number == 2:
self.Angle=float(self.a.value)/2**15
data = os.read(self.joy, sizeof(self.a))
except OSError, e:
if e.errno != 11:
raise
def main():
joy=Joy()
while 1:
joy.update()
print "%6f, %6f" % (joy.Speed, joy.Angle)
if __name__ == "__main__":
main()