Changement de l'image d'accueil en version 2.0.
[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 for i in range(self.actual_nmotes) :
92 wm = self.wiimotes[i]
93 if i == wiimoteIndex :
94 self.do(wiiuse.set_ir, wm, 1)
95 self.do(wiiuse.set_ir_vres, wm, *self.screenResolution)
96 if self.position == 'ABOVE' :
97 position = wiiuse.IR_ABOVE
98 elif self.position == 'BELOW' :
99 position = wiiuse.IR_BELOW
100 else :
101 position = wiiuse.IR_ABOVE
102 self.do(wiiuse.set_ir_position, wm, position)
103 # TODO aspect ratio
104 #self.do(wiiuse.set_aspect_ratio, wm, aspect)
105
106 else :
107 self.do(wiiuse.set_ir, wm, 0)
108
109 def do(self, func, *args):
110 '''Run the function in the thread handling the wiimote'''
111 self.queue.put((func, args))
112
113 def control_cb(self, wmp, attachment, speaker, ir, led, battery):
114 '''Could check the battery level and such here'''
115 pygame.event.post(pygame.event.Event(WIIMOTE_STATUS,
116 attachment=attachment,
117 speaker=speaker,
118 ir=ir,
119 led=[led[i] for i in range(4)],
120 battery=battery,
121 id=wmp[0].unid))
122
123 def disconnect_cb(self, wmp):
124 '''What should we do here?'''
125 pygame.event.post(pygame.event.Event(WIIMOTE_DISCONNECT,
126 id=wmp[0].unid))
127
128 def quit(self):
129 '''Go away.'''
130 for i in range(self.nmotes):
131 wiiuse.set_leds(self.wiimotes[i], 0)
132 wiiuse.disconnect(self.wiimotes[i])
133 self.go = False
134
135 def get_count(self):
136 return self.actual_nmotes
137
138
139 def _default_event_cb(self, id, wmp):
140 ''' default callback that emulate a one button mouse '''
141 if id != self.selectedWiimoteIndex : return
142 wm = wmp[0]
143 pos = (wm.ir.x, wm.ir.y)
144 pygame.mouse.set_pos(pos)
145
146 eventType = None
147
148 if wm.btns and \
149 wiiuse.is_just_pressed(wm, wiiuse.button['B']) :
150 event = pygame.event.Event(pygame.MOUSEBUTTONDOWN,
151 pos = pos,
152 button = 1)
153 pygame.event.post(event)
154
155 if wm.btns_released and \
156 wiiuse.is_released(wm, wiiuse.button['B']):
157 event = pygame.event.Event(pygame.MOUSEBUTTONUP,
158 pos = pos,
159 button = 1)
160 pygame.event.post(event)
161
162 def _full_mouse_event_cb(self, id, wmp):
163 ''' callback that emulate a 2 buttons mouse with wheel '''
164 if id != self.selectedWiimoteIndex : return
165 wm = wmp[0]
166 pos = (wm.ir.x, wm.ir.y)
167 pygame.mouse.set_pos(pos)
168
169 eventType = None
170
171 if wm.btns :
172 button = 0
173 if wiiuse.is_just_pressed(wm, wiiuse.button['B']) :
174 button = 1
175 elif wiiuse.is_just_pressed(wm, wiiuse.button['A']) :
176 button = 2
177 elif wiiuse.is_just_pressed(wm, wiiuse.button['Up']) :
178 button = 4
179 elif wiiuse.is_just_pressed(wm, wiiuse.button['Down']) :
180 button = 5
181
182 if button :
183 event = pygame.event.Event(pygame.MOUSEBUTTONDOWN,
184 pos = pos,
185 button = button)
186 pygame.event.post(event)
187
188 if wm.btns_released :
189 button = 0
190 if wiiuse.is_released(wm, wiiuse.button['B']) :
191 button = 1
192 elif wiiuse.is_released(wm, wiiuse.button['A']) :
193 button = 2
194 elif wiiuse.is_released(wm, wiiuse.button['Up']) :
195 button = 4
196 elif wiiuse.is_released(wm, wiiuse.button['Down']) :
197 button = 5
198
199 if button :
200 event = pygame.event.Event(pygame.MOUSEBUTTONUP,
201 pos = pos,
202 button = button)
203 pygame.event.post(event)
204
205
206 WT = None
207
208 def init(nmotes, timeout, screenResolution=(660, 370), position='ABOVE'):
209 '''Initialize the module.'''
210 global WT
211 if WT:
212 return
213
214 WT = wiimote_thread(nmotes, timeout, screenResolution, position)
215
216 nmotes = get_count()
217 for i in range(nmotes) :
218 wm = Wiimote(i) # access the wiimote object
219 wm.enable_accels(0) # turn off acceleration reporting
220 #wm.enable_ir(1, vres = screenResolution, position=position)
221
222
223 def get_count():
224 '''How many Wiimotes were found?'''
225 return WT.get_count()
226
227 def quit():
228 '''Gracefully shutdown the connection and turn off the wiimote leds'''
229 WT.quit()
230 WT.join()
231
232 class wiimote(object):
233 '''Object representing a Wiimote'''
234 def __init__(self, n):
235 self.wm = WT.wiimotes[n]
236
237 def enable_leds(self, m):
238 '''Control leds. The lower 4 bits map to the 4 leds'''
239 WT.do(wiiuse.set_leds, self.wm, sum([wiiuse.LED[i] for i in range(4) if m & (1<<i)]))
240
241 def enable_rumble(self, on):
242 '''Control rumble'''
243 WT.do(wiiuse.rumble, self.wm, on)
244
245 def enable_accels(self, on):
246 '''Control reporting of accelerometer data.'''
247 WT.do(wiiuse.motion_sensing, self.wm, on)
248
249 def enable_ir(self, on, vres=None, position=None, aspect=None):
250 '''Control reporting IR data.'''
251 WT.do(wiiuse.set_ir, self.wm, on)
252 if vres is not None:
253 WT.do(wiiuse.set_ir_vres, self.wm, *vres)
254 if position is not None:
255 WT.do(wiiuse.set_ir_position, self.wm, position)
256 if aspect is not None:
257 WT.do(wiiuse.set_aspect_ratio, self.wm, aspect)
258
259 def set_flags(self, smoothing=None, continuous=None, threshold=None):
260 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
261 enable = disable = 0
262 if smoothing is not None:
263 if smoothing:
264 enable |= wiiuse.SMOOTHING
265 else:
266 disable |= wiiuse.SMOOTHING
267 if continuous is not None:
268 if continuous:
269 enable |= wiiuse.CONTINUOUS
270 else:
271 disable |= wiiuse.CONTINUOUS
272 if threshold is not None:
273 if threshold:
274 enable |= wiiuse.ORIENT_THRESH
275 else:
276 disable |= wiiuse.ORIENT_THRESH
277 WT.do(wiiuse.set_flags, self.wm, enable, disable)
278
279 def set_orient_thresh(self, thresh):
280 '''Set orientation threshold'''
281 WT.do(wiiuse.set_orient_threshold, self.wm, thresh)
282
283 def status(self):
284 '''Trigger a status callback.'''
285 WT.do(wiiuse.status, self.wm)
286
287 def disconnect(self):
288 '''Disconnect this Wiimote'''
289 WT.do(wiiuse.disconnect(self.wm))
290
291 def Wiimote(n):
292 '''Get the object for the nth Wiimote'''
293 return wiimote(n)
294