commentaire++
[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 :
61 pass
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 '''Called when the library has some data for the user.'''
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
133
134 WT = None
135
136 def init(nmotes, timeout, screenResolution=(660, 370), position='ABOVE'):
137 '''Initialize the module.'''
138 global WT
139 if WT:
140 return
141 WT = wiimote_thread(nmotes, timeout)
142
143 if position == 'ABOVE' :
144 position = wiiuse.IR_ABOVE
145 elif position == 'BELOW' :
146 position = wiiuse.IR_BELOW
147 else :
148 position = wiiuse.IR_ABOVE
149
150
151 nmotes = get_count()
152 for i in range(nmotes) :
153 wm = Wiimote(i) # access the wiimote object
154 wm.enable_accels(0) # turn off acceleration reporting
155 wm.enable_ir(1, vres = screenResolution, position=position)
156
157
158 def get_count():
159 '''How many Wiimotes were found?'''
160 return WT.get_count()
161
162 def quit():
163 '''Gracefully shutdown the connection and turn off the wiimote leds'''
164 WT.quit()
165 WT.join()
166
167 class wiimote(object):
168 '''Object representing a Wiimote'''
169 def __init__(self, n):
170 self.wm = WT.wiimotes[n]
171
172 def enable_leds(self, m):
173 '''Control leds. The lower 4 bits map to the 4 leds'''
174 WT.do(wiiuse.set_leds, self.wm, sum([wiiuse.LED[i] for i in range(4) if m & (1<<i)]))
175
176 def enable_rumble(self, on):
177 '''Control rumble'''
178 WT.do(wiiuse.rumble, self.wm, on)
179
180 def enable_accels(self, on):
181 '''Control reporting of accelerometer data.'''
182 WT.do(wiiuse.motion_sensing, self.wm, on)
183
184 def enable_ir(self, on, vres=None, position=None, aspect=None):
185 '''Control reporting IR data.'''
186 WT.do(wiiuse.set_ir, self.wm, on)
187 if vres is not None:
188 WT.do(wiiuse.set_ir_vres, self.wm, *vres)
189 if position is not None:
190 WT.do(wiiuse.set_ir_position, self.wm, position)
191 if aspect is not None:
192 WT.do(wiiuse.set_aspect_ratio, self.wm, aspect)
193
194 def set_flags(self, smoothing=None, continuous=None, threshold=None):
195 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
196 enable = disable = 0
197 if smoothing is not None:
198 if smoothing:
199 enable |= wiiuse.SMOOTHING
200 else:
201 disable |= wiiuse.SMOOTHING
202 if continuous is not None:
203 if continuous:
204 enable |= wiiuse.CONTINUOUS
205 else:
206 disable |= wiiuse.CONTINUOUS
207 if threshold is not None:
208 if threshold:
209 enable |= wiiuse.ORIENT_THRESH
210 else:
211 disable |= wiiuse.ORIENT_THRESH
212 WT.do(wiiuse.set_flags, self.wm, enable, disable)
213
214 def set_orient_thresh(self, thresh):
215 '''Set orientation threshold'''
216 WT.do(wiiuse.set_orient_threshold, self.wm, thresh)
217
218 def status(self):
219 '''Trigger a status callback.'''
220 WT.do(wiiuse.status, self.wm)
221
222 def disconnect(self):
223 '''Disconnect this Wiimote'''
224 WT.do(wiiuse.disconnect(self.wm))
225
226 def Wiimote(n):
227 '''Get the object for the nth Wiimote'''
228 return wiimote(n)
229