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