Retouche de l'icône pour MINDs.
[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
21 def __init__(self, nmotes=1, timeout=5, screenResolution=(660, 370), position='ABOVE'):
22 Thread.__init__(self, name='wiimote')
23 self.queue = Queue()
24 self.startup = Queue()
25 self.nmotes = nmotes
26 self.timeout = timeout
27 self.screenResolution = screenResolution
28 self.position = position
29 self.selectedWiimoteIndex = 0
30 self.setDaemon(1)
31 self._paused = False
32 self.start()
33 self.startup.get(True) # wait for the thread to get started and acquire the motes
34 self.eventCallBack = _default_event_cb
35
36 def setEventCallBack(self, func) :
37 self.eventCallBack = func
38
39 def run(self):
40 '''This runs in a separate thread'''
41 global wiiuse
42 import PyWiiUse as wiiuse # import here to avoid thread problems on windows
43 self.wiimotes = wiiuse.init(self.nmotes)
44 found = wiiuse.find(self.wiimotes, self.nmotes, self.timeout)
45 self.actual_nmotes = wiiuse.connect(self.wiimotes, self.nmotes)
46
47
48 if self.nmotes <= 4 :
49 for i in range(self.nmotes):
50 wiiuse.set_leds(self.wiimotes[i], wiiuse.LED[i])
51 else :
52 for i in range(4):
53 wiiuse.set_leds(self.wiimotes[i], wiiuse.LED[i])
54
55 if self.nmotes == 5 :
56 wiiuse.set_leds(self.wiimotes[4], wiiuse.LED_1 | wiiuse.LED_4)
57 if self.nmotes == 6 :
58 wiiuse.set_leds(self.wiimotes[4], wiiuse.LED_1 | wiiuse.LED_4)
59 wiiuse.set_leds(self.wiimotes[5], wiiuse.LED_1 | wiiuse.LED_2 | wiiuse.LED_3 | wiiuse.LED_4)
60
61 self.go = self.actual_nmotes != 0
62
63 self.startup.put(self.go)
64
65 while self.go:
66 if self._paused : continue
67 try :
68 if wiiuse.poll(self.wiimotes, self.nmotes) :
69 for i in range(self.nmotes) :
70 m = self.wiimotes[i]
71 if m[0].event == wiiuse.EVENT:
72 self.eventCallBack(self, i, m)
73 except Exception, e:
74 print e
75
76 while True:
77 try:
78 func, args = self.queue.get_nowait()
79 except Empty:
80 break
81 func(*args)
82
83 def pause(self) :
84 self._paused = True
85
86 def resume(self) :
87 self._paused = False
88
89 def selectWiimote(self, wiimoteIndex) :
90 self.selectedWiimoteIndex = wiimoteIndex
91
92 def do(self, func, *args):
93 '''Run the function in the thread handling the wiimote'''
94 self.queue.put((func, args))
95
96 def control_cb(self, wmp, attachment, speaker, ir, led, battery):
97 '''Could check the battery level and such here'''
98 pygame.event.post(pygame.event.Event(WIIMOTE_STATUS,
99 attachment=attachment,
100 speaker=speaker,
101 ir=ir,
102 led=[led[i] for i in range(4)],
103 battery=battery,
104 id=wmp[0].unid))
105
106 def disconnect_cb(self, wmp):
107 '''What should we do here?'''
108 pygame.event.post(pygame.event.Event(WIIMOTE_DISCONNECT,
109 id=wmp[0].unid))
110
111 def quit(self):
112 '''Go away.'''
113 # TODO will crash if self.nmotes > 4
114 # for i in range(self.nmotes):
115 # wiiuse.set_leds(self.wiimotes[i], 0)
116 # wiiuse.disconnect(self.wiimotes[i])
117 self.go = False
118
119 def get_count(self):
120 return self.actual_nmotes
121
122
123 def _default_event_cb(self, id, wmp):
124 ''' default callback that emulate a one button mouse '''
125 if id != self.selectedWiimoteIndex : return
126 wm = wmp[0]
127 pos = (wm.ir.x, wm.ir.y)
128 pygame.mouse.set_pos(pos)
129
130 eventType = None
131
132 if wm.btns and \
133 wiiuse.is_just_pressed(wm, wiiuse.button['B']) :
134 event = pygame.event.Event(pygame.MOUSEBUTTONDOWN,
135 pos = pos,
136 button = 1)
137 pygame.event.post(event)
138
139 if wm.btns_released and \
140 wiiuse.is_released(wm, wiiuse.button['B']):
141 event = pygame.event.Event(pygame.MOUSEBUTTONUP,
142 pos = pos,
143 button = 1)
144 pygame.event.post(event)
145
146 def _full_mouse_event_cb(self, id, wmp):
147 ''' callback that emulate a 2 buttons mouse with wheel '''
148 if id != self.selectedWiimoteIndex : return
149 wm = wmp[0]
150 pos = (wm.ir.x, wm.ir.y)
151 pygame.mouse.set_pos(pos)
152
153 eventType = None
154
155 if wm.btns :
156 button = 0
157 if wiiuse.is_just_pressed(wm, wiiuse.button['B']) :
158 button = 1
159 elif wiiuse.is_just_pressed(wm, wiiuse.button['A']) :
160 button = 2
161 elif wiiuse.is_just_pressed(wm, wiiuse.button['Up']) :
162 button = 4
163 elif wiiuse.is_just_pressed(wm, wiiuse.button['Down']) :
164 button = 5
165
166 if button :
167 event = pygame.event.Event(pygame.MOUSEBUTTONDOWN,
168 pos = pos,
169 button = button)
170 pygame.event.post(event)
171
172 if wm.btns_released :
173 button = 0
174 if wiiuse.is_released(wm, wiiuse.button['B']) :
175 button = 1
176 elif wiiuse.is_released(wm, wiiuse.button['A']) :
177 button = 2
178 elif wiiuse.is_released(wm, wiiuse.button['Up']) :
179 button = 4
180 elif wiiuse.is_released(wm, wiiuse.button['Down']) :
181 button = 5
182
183 if button :
184 event = pygame.event.Event(pygame.MOUSEBUTTONUP,
185 pos = pos,
186 button = button)
187 pygame.event.post(event)
188
189
190 WT = None
191
192 def init(nmotes, timeout, screenResolution=(660, 370), position='ABOVE'):
193 '''Initialize the module.'''
194 global WT
195 if WT:
196 return
197
198 WT = wiimote_thread(nmotes, timeout, screenResolution, position)
199
200 nmotes = get_count()
201 for i in range(nmotes) :
202 wm = Wiimote(i) # access the wiimote object
203 wm.enable_accels(0) # turn off acceleration reporting
204 wm.enable_ir(1, vres = screenResolution, position=position)
205
206
207 def get_count():
208 '''How many Wiimotes were found?'''
209 return WT.get_count()
210
211 def quit():
212 '''Gracefully shutdown the connection and turn off the wiimote leds'''
213 WT.quit()
214 WT.join()
215
216 class wiimote(object):
217 '''Object representing a Wiimote'''
218 def __init__(self, n):
219 self.wm = WT.wiimotes[n]
220
221 def enable_leds(self, m):
222 '''Control leds. The lower 4 bits map to the 4 leds'''
223 WT.do(wiiuse.set_leds, self.wm, sum([wiiuse.LED[i] for i in range(4) if m & (1<<i)]))
224
225 def enable_rumble(self, on):
226 '''Control rumble'''
227 WT.do(wiiuse.rumble, self.wm, on)
228
229 def enable_accels(self, on):
230 '''Control reporting of accelerometer data.'''
231 WT.do(wiiuse.motion_sensing, self.wm, on)
232
233 def enable_ir(self, on, vres=None, position=None, aspect=None):
234 '''Control reporting IR data.'''
235 WT.do(wiiuse.set_ir, self.wm, on)
236 if vres is not None:
237 WT.do(wiiuse.set_ir_vres, self.wm, *vres)
238 if position is not None:
239 WT.do(wiiuse.set_ir_position, self.wm, position)
240 if aspect is not None:
241 WT.do(wiiuse.set_aspect_ratio, self.wm, aspect)
242
243 def set_flags(self, smoothing=None, continuous=None, threshold=None):
244 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
245 enable = disable = 0
246 if smoothing is not None:
247 if smoothing:
248 enable |= wiiuse.SMOOTHING
249 else:
250 disable |= wiiuse.SMOOTHING
251 if continuous is not None:
252 if continuous:
253 enable |= wiiuse.CONTINUOUS
254 else:
255 disable |= wiiuse.CONTINUOUS
256 if threshold is not None:
257 if threshold:
258 enable |= wiiuse.ORIENT_THRESH
259 else:
260 disable |= wiiuse.ORIENT_THRESH
261 WT.do(wiiuse.set_flags, self.wm, enable, disable)
262
263 def set_orient_thresh(self, thresh):
264 '''Set orientation threshold'''
265 WT.do(wiiuse.set_orient_threshold, self.wm, thresh)
266
267 def status(self):
268 '''Trigger a status callback.'''
269 WT.do(wiiuse.status, self.wm)
270
271 def disconnect(self):
272 '''Disconnect this Wiimote'''
273 WT.do(wiiuse.disconnect(self.wm))
274
275 def Wiimote(n):
276 '''Get the object for the nth Wiimote'''
277 return wiimote(n)
278