-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoystick.py
More file actions
133 lines (96 loc) · 3.1 KB
/
joystick.py
File metadata and controls
133 lines (96 loc) · 3.1 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
from pygame import *
import os, sys
import threading
import time
import json
from ws4py.client.threadedclient import WebSocketClient
class DummyClient(WebSocketClient):
def opened(self):
def closed(self, code, reason=None):
print "closed socket"
def received_message(self, m):
print m
def main():
init()
os.environ["SDL_VIDEODRIVER"] = "dummy"
win = display.set_mode([1,1])
msg = {'D': '0', 'T': '0', 'V': '0'}
#let's turn on the joysticks just so we can play with em
for x in range(joystick.get_count()):
j = joystick.Joystick(x)
j.init()
print 'Enabled joystick: ' , j.get_name(), x
if not joystick.get_count():
print 'No Joysticks to Initialize'
going = True
while going:
msg = {'D': 0, 'T': 0, 'V': 0}
txt = {}
for e in event.get():
txt = e.dict
if e.type == JOYAXISMOTION:
#txt = '%s: %s' % (event.event_name(e.type), e.dict)
#txt = 't: %s' % (e.dict)
if txt['joy'] == 0:
msg['D'] = 1
elif txt['joy'] == 1:
msg['D'] = 2
if txt['axis'] == 0:
msg['T'] = 1
elif txt['axis'] == 1:
msg['T'] = 2
msg['V'] = int(txt['value'])
jsonObj = json.dumps(msg)
ws.send(jsonObj)
print jsonObj
elif e.type == JOYBUTTONDOWN:
if txt['joy'] == 0:
msg['D'] = 1
elif txt['joy'] == 1:
msg['D'] = 2
msg['T'] = 3
msg['V'] = 1
jsonObj = json.dumps(msg)
ws.send(jsonObj)
print jsonObj
elif e.type == KEYDOWN:
msg['D'] = 3
if txt['key'] == 273:
msg['T'] = 1
msg['V'] = 1
elif txt['key'] == 274:
msg['T'] = 1
msg['V'] = -1
elif txt['key'] == 275:
msg['T'] = 2
msg['V'] = 1
elif txt['key'] == 276:
msg['T'] = 2
msg['V'] = -1
elif txt['key'] == 32:
msg['T'] = 3
msg['V'] = 1
elif txt['key'] == 27:
print "KILL! KILL IT WITH FIRE!!"
exit()
jsonObj = json.dumps(msg)
ws.send(jsonObj)
print jsonObj
if __name__ == '__main__':
try:
#start websocket
ws = DummyClient('ws://localhost:8888/ws', protocols=['http-only', 'chat'])
ws.connect()
print "Connected to websocket"
ws.send("{\"input\":true, \"output\":false}")
main()
#CTRL-C
except KeyboardInterrupt:
print "User cancelled"
ws.close()
except:
print "Unexpected error:", sys.exc_info()[0]
raise
finally:
exit()