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