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