Ça commence à le faire.
[minwii.git] / src / pywiiuse / pygame_wiimouse.py
1 # -*- coding: utf-8 -*-
2 '''
3 wiimote -> mouse interface
4 $Id$
5 $URL$
6 '''
7
8 import pygame
9 from threading import Thread
10 from Queue import Queue, Empty
11 import time
12
13 # events to use. Is there a way to get ones known to be unused?
14
15
16 wiiuse = None # import within the thread, why do I have to do this?
17
18 class wiimote_thread(Thread):
19 '''Manage the wiiuse interface'''
20 def __init__(self, nmotes=1, timeout=5):
21 Thread.__init__(self, name='wiimote')
22 self.queue = Queue()
23 self.startup = Queue()
24 self.nmotes = nmotes
25 self.timeout = timeout
26 self.setDaemon(1)
27 self._paused = False
28 self.start()
29 self.startup.get(True) # wait for the thread to get started and acquire the motes
30
31 def run(self):
32 '''This runs in a separate thread'''
33 global wiiuse
34 import PyWiiUse as wiiuse # import here to avoid thread problems on windows
35 self.wiimotes = wiiuse.init(self.nmotes)
36 found = wiiuse.find(self.wiimotes, self.nmotes, self.timeout)
37 self.actual_nmotes = wiiuse.connect(self.wiimotes, self.nmotes)
38
39 self.previousPositions = {}
40
41 for i in range(self.nmotes):
42 wiiuse.set_leds(self.wiimotes[i], wiiuse.LED[i])
43 self.previousPositions[self.wiimotes[i][0].unid] = (0, 0)
44
45 self.go = self.actual_nmotes != 0
46
47 self.startup.put(self.go)
48
49 while self.go:
50 if self._paused : continue
51 try :
52 if wiiuse.poll(self.wiimotes, self.nmotes) :
53 for i in range(self.nmotes):
54 m = self.wiimotes[i]
55 if m[0].event == wiiuse.EVENT:
56 self.event_cb(m)
57 except :
58 pass
59
60 #try:
61 # wiiuse.poll(self.wiimotes, self.nmotes)
62 #except:
63 # pass
64
65 # allow executing functions in this thread
66 while True:
67 try:
68 func, args = self.queue.get_nowait()
69 except Empty:
70 break
71 print 'do:', func.__name__, args
72 func(*args)
73
74 def pause(self) :
75 self._paused = True
76
77 def resume(self) :
78 self._paused = False
79
80 def do(self, func, *args):
81 '''Run the function in the thread handling the wiimote'''
82 self.queue.put((func, args))
83
84 def event_cb(self, wmp):
85 '''Called when the library has some data for the user.'''
86 wm = wmp[0]
87 # if wm.btns:
88 # for name,b in wiiuse.button.items():
89 # if wiiuse.is_just_pressed(wm, b):
90 # pygame.event.post(pygame.event.Event(WIIMOTE_BUTTON_PRESS, button=name,
91 # time=time.time(),
92 # id=wm.unid))
93 #
94 # if wm.btns_released:
95 # for name,b in wiiuse.button.items():
96 # if wiiuse.is_released(wm, b):
97 # pygame.event.post(pygame.event.Event(WIIMOTE_BUTTON_RELEASE, button=name,
98 # time=time.time(),
99 # id=wm.unid))
100 #
101 # if True:
102 # pygame.event.post(pygame.event.Event(WIIMOTE_ACCEL,
103 # orient=(wm.orient.roll, wm.orient.pitch,
104 # wm.orient.yaw),
105 # accel=(wm.gforce.x, wm.gforce.y, wm.gforce.z),
106 # time=time.time(),
107 # id=wm.unid))
108 if True:
109 #dots = [ (wm.ir.dot[i].visible, wm.ir.dot[i].x, wm.ir.dot[i].y) for i in range(4) ]
110 #pygame.event.post(pygame.event.Event(WIIMOTE_IR,
111 # dots=dots,
112 # cursor=(wm.ir.x, wm.ir.y, wm.ir.z),
113 # time=time.time(),
114 # id=wm.unid))
115
116 dots = [ (wm.ir.dot[i].visible, wm.ir.dot[i].x, wm.ir.dot[i].y) for i in range(4) ]
117 cursor=(wm.ir.x, wm.ir.y, wm.ir.z)
118 pos = cursor[:2]
119 previousPos = self.previousPositions[wm.unid]
120 rel = (pos[0] - previousPos[0], pos[1] - previousPos[1])
121 self.previousPositions[wm.unid] = pos
122
123 evt = pygame.event.Event(pygame.MOUSEMOTION,
124 pos = pos,
125 rel = rel,
126 buttons = [],
127 dots=dots,
128 cursor=(wm.ir.x, wm.ir.y, wm.ir.z),
129 time=time.time(),
130 wiimoteid=wm.unid)
131 pygame.event.post(evt)
132
133
134 # if wm.exp.type == wiiuse.EXP_NUNCHUK:
135 # nc = wm.exp.u.nunchuk
136 #
137 # for name,b in wiiuse.nunchuk_button.items():
138 # if wiiuse.is_just_pressed(nc, b):
139 # pygame.event.post(pygame.event.Event(NUNCHUK_BUTTON_PRESS, button=name,
140 # time=time.time(),
141 # id=wm.unid))
142 # elif wiiuse.is_released(nc, b):
143 # pygame.event.post(pygame.event.Event(NUNCHUK_BUTTON_RELEASE, button=name,
144 # time=time.time(),
145 # id=wm.unid))
146 #
147 # pygame.event.post(pygame.event.Event(NUNCHUK_ACCEL,
148 # orient=(nc.orient.roll, nc.orient.pitch,
149 # nc.orient.yaw),
150 # accel=(nc.gforce.x, nc.gforce.y, nc.gforce.z),
151 # time=time.time(),
152 # id=wm.unid))
153 # pygame.event.post(pygame.event.Event(NUNCHUK_JOY,
154 # angle=nc.js.ang,
155 # mag=nc.js.mag,
156 # time=time.time(),
157 # id=wm.unid))
158
159 def control_cb(self, wmp, attachment, speaker, ir, led, battery):
160 '''Could check the battery level and such here'''
161 pygame.event.post(pygame.event.Event(WIIMOTE_STATUS,
162 attachment=attachment,
163 speaker=speaker,
164 ir=ir,
165 led=[led[i] for i in range(4)],
166 battery=battery,
167 id=wmp[0].unid))
168
169 def disconnect_cb(self, wmp):
170 '''What should we do here?'''
171 pygame.event.post(pygame.event.Event(WIIMOTE_DISCONNECT,
172 id=wmp[0].unid))
173
174 def quit(self):
175 '''Go away.'''
176 for i in range(self.nmotes):
177 wiiuse.set_leds(self.wiimotes[i], 0)
178 wiiuse.disconnect(self.wiimotes[i])
179 self.go = False
180
181 WT = None
182
183 def init(nmotes, timeout):
184 '''Initialize the module.'''
185 global WT
186 if WT:
187 return
188 WT = wiimote_thread(nmotes, timeout)
189
190 def get_count():
191 '''How many Wiimotes were found?'''
192 return WT.actual_nmotes
193
194 def quit():
195 '''Gracefully shutdown the connection and turn off the wiimote leds'''
196 WT.quit()
197 WT.join()
198
199 class wiimote(object):
200 '''Object representing a Wiimote'''
201 def __init__(self, n):
202 self.wm = WT.wiimotes[n]
203
204 def enable_leds(self, m):
205 '''Control leds. The lower 4 bits map to the 4 leds'''
206 WT.do(wiiuse.set_leds, self.wm, sum([wiiuse.LED[i] for i in range(4) if m & (1<<i)]))
207
208 def enable_rumble(self, on):
209 '''Control rumble'''
210 WT.do(wiiuse.rumble, self.wm, on)
211
212 def enable_accels(self, on):
213 '''Control reporting of accelerometer data.'''
214 WT.do(wiiuse.motion_sensing, self.wm, on)
215
216 def enable_ir(self, on, vres=None, position=None, aspect=None):
217 '''Control reporting IR data.'''
218 WT.do(wiiuse.set_ir, self.wm, on)
219 if vres is not None:
220 WT.do(wiiuse.set_ir_vres, self.wm, *vres)
221 if position is not None:
222 WT.do(wiiuse.set_ir_position, self.wm, position)
223 if aspect is not None:
224 WT.do(wiiuse.set_aspect_ratio, self.wm, aspect)
225
226 def set_flags(self, smoothing=None, continuous=None, threshold=None):
227 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
228 enable = disable = 0
229 if smoothing is not None:
230 if smoothing:
231 enable |= wiiuse.SMOOTHING
232 else:
233 disable |= wiiuse.SMOOTHING
234 if continuous is not None:
235 if continuous:
236 enable |= wiiuse.CONTINUOUS
237 else:
238 disable |= wiiuse.CONTINUOUS
239 if threshold is not None:
240 if threshold:
241 enable |= wiiuse.ORIENT_THRESH
242 else:
243 disable |= wiiuse.ORIENT_THRESH
244 print enable, disable
245 WT.do(wiiuse.set_flags, self.wm, enable, disable)
246
247 def set_orient_thresh(self, thresh):
248 '''Set orientation threshold'''
249 WT.do(wiiuse.set_orient_threshold, self.wm, thresh)
250
251 def status(self):
252 '''Trigger a status callback.'''
253 WT.do(wiiuse.status, self.wm)
254
255 def disconnect(self):
256 '''Disconnect this Wiimote'''
257 WT.do(wiiuse.disconnect(self.wm))
258
259 def Wiimote(n):
260 '''Get the object for the nth Wiimote'''
261 return wiimote(n)
262