1 # -*- coding: utf-8 -*-
3 wiimote -> mouse interface
9 from threading
import Thread
10 from Queue
import Queue
, Empty
13 # events to use. Is there a way to get ones known to be unused?
16 wiiuse
= None # import within the thread, why do I have to do this?
18 class wiimote_thread(Thread
):
19 '''Manage the wiiuse interface'''
21 def __init__(self
, nmotes
=1, timeout
=5, screenResolution
=(660, 370), position
='ABOVE'):
22 Thread
.__init
__(self
, name
='wiimote')
24 self
.startup
= Queue()
26 self
.timeout
= timeout
27 self
.screenResolution
= screenResolution
28 self
.position
= position
29 self
.selectedWiimoteIndex
= 0
33 self
.startup
.get(True) # wait for the thread to get started and acquire the motes
34 self
.eventCallBack
= _default_event_cb
36 def setEventCallBack(self
, func
) :
37 self
.eventCallBack
= func
40 '''This runs in a separate thread'''
42 import PyWiiUse
as wiiuse
# import here to avoid thread problems on windows
43 self
.wiimotes
= wiiuse
.init(self
.nmotes
)
44 found
= wiiuse
.find(self
.wiimotes
, self
.nmotes
, self
.timeout
)
45 self
.actual_nmotes
= wiiuse
.connect(self
.wiimotes
, self
.nmotes
)
49 for i
in range(self
.nmotes
):
50 wiiuse
.set_leds(self
.wiimotes
[i
], wiiuse
.LED
[i
])
53 wiiuse
.set_leds(self
.wiimotes
[i
], wiiuse
.LED
[i
])
56 wiiuse
.set_leds(self
.wiimotes
[4], wiiuse
.LED_1 | wiiuse
.LED_4
)
58 wiiuse
.set_leds(self
.wiimotes
[4], wiiuse
.LED_1 | wiiuse
.LED_4
)
59 wiiuse
.set_leds(self
.wiimotes
[5], wiiuse
.LED_1 | wiiuse
.LED_2 | wiiuse
.LED_3 | wiiuse
.LED_4
)
61 self
.go
= self
.actual_nmotes
!= 0
63 self
.startup
.put(self
.go
)
66 if self
._paused
: continue
68 if wiiuse
.poll(self
.wiimotes
, self
.nmotes
) :
69 for i
in range(self
.nmotes
) :
71 if m
[0].event
== wiiuse
.EVENT
:
72 self
.eventCallBack(self
, i
, m
)
78 func
, args
= self
.queue
.get_nowait()
89 def selectWiimote(self
, wiimoteIndex
) :
90 self
.selectedWiimoteIndex
= wiimoteIndex
92 def do(self
, func
, *args
):
93 '''Run the function in the thread handling the wiimote'''
94 self
.queue
.put((func
, args
))
96 def control_cb(self
, wmp
, attachment
, speaker
, ir
, led
, battery
):
97 '''Could check the battery level and such here'''
98 pygame
.event
.post(pygame
.event
.Event(WIIMOTE_STATUS
,
99 attachment
=attachment
,
102 led
=[led
[i
] for i
in range(4)],
106 def disconnect_cb(self
, wmp
):
107 '''What should we do here?'''
108 pygame
.event
.post(pygame
.event
.Event(WIIMOTE_DISCONNECT
,
113 # TODO will crash if self.nmotes > 4
114 # for i in range(self.nmotes):
115 # wiiuse.set_leds(self.wiimotes[i], 0)
116 # wiiuse.disconnect(self.wiimotes[i])
120 return self
.actual_nmotes
123 def _default_event_cb(self
, id, wmp
):
124 ''' default callback that emulate a one button mouse '''
125 if id != self
.selectedWiimoteIndex
: return
127 pos
= (wm
.ir
.x
, wm
.ir
.y
)
128 pygame
.mouse
.set_pos(pos
)
133 wiiuse
.is_just_pressed(wm
, wiiuse
.button
['B']) :
134 event
= pygame
.event
.Event(pygame
.MOUSEBUTTONDOWN
,
137 pygame
.event
.post(event
)
139 if wm
.btns_released
and \
140 wiiuse
.is_released(wm
, wiiuse
.button
['B']):
141 event
= pygame
.event
.Event(pygame
.MOUSEBUTTONUP
,
144 pygame
.event
.post(event
)
146 def _full_mouse_event_cb(self
, id, wmp
):
147 ''' callback that emulate a 2 buttons mouse with wheel '''
148 if id != self
.selectedWiimoteIndex
: return
150 pos
= (wm
.ir
.x
, wm
.ir
.y
)
151 pygame
.mouse
.set_pos(pos
)
157 if wiiuse
.is_just_pressed(wm
, wiiuse
.button
['B']) :
159 elif wiiuse
.is_just_pressed(wm
, wiiuse
.button
['A']) :
161 elif wiiuse
.is_just_pressed(wm
, wiiuse
.button
['Up']) :
163 elif wiiuse
.is_just_pressed(wm
, wiiuse
.button
['Down']) :
167 event
= pygame
.event
.Event(pygame
.MOUSEBUTTONDOWN
,
170 pygame
.event
.post(event
)
172 if wm
.btns_released
:
174 if wiiuse
.is_released(wm
, wiiuse
.button
['B']) :
176 elif wiiuse
.is_released(wm
, wiiuse
.button
['A']) :
178 elif wiiuse
.is_released(wm
, wiiuse
.button
['Up']) :
180 elif wiiuse
.is_released(wm
, wiiuse
.button
['Down']) :
184 event
= pygame
.event
.Event(pygame
.MOUSEBUTTONUP
,
187 pygame
.event
.post(event
)
192 def init(nmotes
, timeout
, screenResolution
=(660, 370), position
='ABOVE'):
193 '''Initialize the module.'''
198 WT
= wiimote_thread(nmotes
, timeout
, screenResolution
, position
)
201 for i
in range(nmotes
) :
202 wm
= Wiimote(i
) # access the wiimote object
203 wm
.enable_accels(0) # turn off acceleration reporting
204 wm
.enable_ir(1, vres
= screenResolution
, position
=position
)
208 '''How many Wiimotes were found?'''
209 return WT
.get_count()
212 '''Gracefully shutdown the connection and turn off the wiimote leds'''
216 class wiimote(object):
217 '''Object representing a Wiimote'''
218 def __init__(self
, n
):
219 self
.wm
= WT
.wiimotes
[n
]
221 def enable_leds(self
, m
):
222 '''Control leds. The lower 4 bits map to the 4 leds'''
223 WT
.do(wiiuse
.set_leds
, self
.wm
, sum([wiiuse
.LED
[i
] for i
in range(4) if m
& (1<<i
)]))
225 def enable_rumble(self
, on
):
227 WT
.do(wiiuse
.rumble
, self
.wm
, on
)
229 def enable_accels(self
, on
):
230 '''Control reporting of accelerometer data.'''
231 WT
.do(wiiuse
.motion_sensing
, self
.wm
, on
)
233 def enable_ir(self
, on
, vres
=None, position
=None, aspect
=None):
234 '''Control reporting IR data.'''
235 WT
.do(wiiuse
.set_ir
, self
.wm
, on
)
237 WT
.do(wiiuse
.set_ir_vres
, self
.wm
, *vres
)
238 if position
is not None:
239 WT
.do(wiiuse
.set_ir_position
, self
.wm
, position
)
240 if aspect
is not None:
241 WT
.do(wiiuse
.set_aspect_ratio
, self
.wm
, aspect
)
243 def set_flags(self
, smoothing
=None, continuous
=None, threshold
=None):
244 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
246 if smoothing
is not None:
248 enable |
= wiiuse
.SMOOTHING
250 disable |
= wiiuse
.SMOOTHING
251 if continuous
is not None:
253 enable |
= wiiuse
.CONTINUOUS
255 disable |
= wiiuse
.CONTINUOUS
256 if threshold
is not None:
258 enable |
= wiiuse
.ORIENT_THRESH
260 disable |
= wiiuse
.ORIENT_THRESH
261 WT
.do(wiiuse
.set_flags
, self
.wm
, enable
, disable
)
263 def set_orient_thresh(self
, thresh
):
264 '''Set orientation threshold'''
265 WT
.do(wiiuse
.set_orient_threshold
, self
.wm
, thresh
)
268 '''Trigger a status callback.'''
269 WT
.do(wiiuse
.status
, self
.wm
)
271 def disconnect(self
):
272 '''Disconnect this Wiimote'''
273 WT
.do(wiiuse
.disconnect(self
.wm
))
276 '''Get the object for the nth Wiimote'''