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