Ok pour le fonctionnement avec plusieurs wiimotes.
[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
32 def run(self):
33 '''This runs in a separate thread'''
34 global wiiuse
35 import PyWiiUse as wiiuse # import here to avoid thread problems on windows
36 self.wiimotes = wiiuse.init(self.nmotes)
37 found = wiiuse.find(self.wiimotes, self.nmotes, self.timeout)
38 self.actual_nmotes = wiiuse.connect(self.wiimotes, self.nmotes)
39
40
41 for i in range(self.nmotes):
42 wiiuse.set_leds(self.wiimotes[i], wiiuse.LED[i])
43
44 self.go = self.actual_nmotes != 0
45
46 self.startup.put(self.go)
47
48 while self.go:
49 if self._paused : continue
50 try :
51 if wiiuse.poll(self.wiimotes, self.nmotes) :
52 m = self.wiimotes[self.selectedWiimoteIndex]
53 if m[0].event == wiiuse.EVENT:
54 self.event_cb(m)
55 except :
56 pass
57
58 while True:
59 try:
60 func, args = self.queue.get_nowait()
61 except Empty:
62 break
63 print 'do:', func.__name__, args
64 func(*args)
65
66 def pause(self) :
67 self._paused = True
68
69 def resume(self) :
70 self._paused = False
71
72 def selectWiimote(self, wiimoteIndex) :
73 self.selectedWiimoteIndex = wiimoteIndex
74
75 def do(self, func, *args):
76 '''Run the function in the thread handling the wiimote'''
77 self.queue.put((func, args))
78
79 def event_cb(self, wmp):
80 '''Called when the library has some data for the user.'''
81 wm = wmp[0]
82 pos = (wm.ir.x, wm.ir.y)
83 pygame.mouse.set_pos(pos)
84
85 eventType = None
86
87 if wm.btns and \
88 wiiuse.is_just_pressed(wm, wiiuse.button['B']) :
89 event = pygame.event.Event(pygame.MOUSEBUTTONDOWN,
90 pos = pos,
91 button = 1)
92 pygame.event.post(event)
93
94 if wm.btns_released and \
95 wiiuse.is_released(wm, wiiuse.button['B']):
96 event = pygame.event.Event(pygame.MOUSEBUTTONUP,
97 pos = pos,
98 button = 1)
99 pygame.event.post(event)
100
101
102
103 def control_cb(self, wmp, attachment, speaker, ir, led, battery):
104 '''Could check the battery level and such here'''
105 pygame.event.post(pygame.event.Event(WIIMOTE_STATUS,
106 attachment=attachment,
107 speaker=speaker,
108 ir=ir,
109 led=[led[i] for i in range(4)],
110 battery=battery,
111 id=wmp[0].unid))
112
113 def disconnect_cb(self, wmp):
114 '''What should we do here?'''
115 pygame.event.post(pygame.event.Event(WIIMOTE_DISCONNECT,
116 id=wmp[0].unid))
117
118 def quit(self):
119 '''Go away.'''
120 for i in range(self.nmotes):
121 wiiuse.set_leds(self.wiimotes[i], 0)
122 wiiuse.disconnect(self.wiimotes[i])
123 self.go = False
124
125 WT = None
126
127 def init(nmotes, timeout, screenResolution=(660, 370)):
128 '''Initialize the module.'''
129 global WT
130 if WT:
131 return
132 WT = wiimote_thread(nmotes, timeout)
133
134 nmotes = get_count()
135 for i in range(nmotes) :
136 wm = Wiimote(i) # access the wiimote object
137 wm.enable_accels(0) # turn off acceleration reporting
138 wm.enable_ir(1, vres = screenResolution, position=wiiuse.IR_BELOW)
139
140
141 def get_count():
142 '''How many Wiimotes were found?'''
143 return WT.actual_nmotes
144
145 def quit():
146 '''Gracefully shutdown the connection and turn off the wiimote leds'''
147 WT.quit()
148 WT.join()
149
150 class wiimote(object):
151 '''Object representing a Wiimote'''
152 def __init__(self, n):
153 self.wm = WT.wiimotes[n]
154
155 def enable_leds(self, m):
156 '''Control leds. The lower 4 bits map to the 4 leds'''
157 WT.do(wiiuse.set_leds, self.wm, sum([wiiuse.LED[i] for i in range(4) if m & (1<<i)]))
158
159 def enable_rumble(self, on):
160 '''Control rumble'''
161 WT.do(wiiuse.rumble, self.wm, on)
162
163 def enable_accels(self, on):
164 '''Control reporting of accelerometer data.'''
165 WT.do(wiiuse.motion_sensing, self.wm, on)
166
167 def enable_ir(self, on, vres=None, position=None, aspect=None):
168 '''Control reporting IR data.'''
169 WT.do(wiiuse.set_ir, self.wm, on)
170 if vres is not None:
171 WT.do(wiiuse.set_ir_vres, self.wm, *vres)
172 if position is not None:
173 WT.do(wiiuse.set_ir_position, self.wm, position)
174 if aspect is not None:
175 WT.do(wiiuse.set_aspect_ratio, self.wm, aspect)
176
177 def set_flags(self, smoothing=None, continuous=None, threshold=None):
178 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
179 enable = disable = 0
180 if smoothing is not None:
181 if smoothing:
182 enable |= wiiuse.SMOOTHING
183 else:
184 disable |= wiiuse.SMOOTHING
185 if continuous is not None:
186 if continuous:
187 enable |= wiiuse.CONTINUOUS
188 else:
189 disable |= wiiuse.CONTINUOUS
190 if threshold is not None:
191 if threshold:
192 enable |= wiiuse.ORIENT_THRESH
193 else:
194 disable |= wiiuse.ORIENT_THRESH
195 print enable, disable
196 WT.do(wiiuse.set_flags, self.wm, enable, disable)
197
198 def set_orient_thresh(self, thresh):
199 '''Set orientation threshold'''
200 WT.do(wiiuse.set_orient_threshold, self.wm, thresh)
201
202 def status(self):
203 '''Trigger a status callback.'''
204 WT.do(wiiuse.status, self.wm)
205
206 def disconnect(self):
207 '''Disconnect this Wiimote'''
208 WT.do(wiiuse.disconnect(self.wm))
209
210 def Wiimote(n):
211 '''Get the object for the nth Wiimote'''
212 return wiimote(n)
213