4d42763c6238e8b28c71adc0f2b72291a135b730
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'''
20 def __init__(self
, nmotes
=1, timeout
=5):
21 Thread
.__init
__(self
, name
='wiimote')
23 self
.startup
= Queue()
25 self
.selectedWiimoteIndex
= 0
26 self
.timeout
= timeout
30 self
.startup
.get(True) # wait for the thread to get started and acquire the motes
33 '''This runs in a separate thread'''
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
)
41 for i
in range(self
.nmotes
):
42 wiiuse
.set_leds(self
.wiimotes
[i
], wiiuse
.LED
[i
])
44 self
.go
= self
.actual_nmotes
!= 0
46 self
.startup
.put(self
.go
)
49 if self
._paused
: continue
51 if wiiuse
.poll(self
.wiimotes
, self
.nmotes
) :
52 m
= self
.wiimotes
[self
.selectedWiimoteIndex
]
53 if m
[0].event
== wiiuse
.EVENT
:
60 func
, args
= self
.queue
.get_nowait()
71 def selectWiimote(self
, wiimoteIndex
) :
72 self
.selectedWiimoteIndex
= wiimoteIndex
74 def do(self
, func
, *args
):
75 '''Run the function in the thread handling the wiimote'''
76 self
.queue
.put((func
, args
))
78 def event_cb(self
, wmp
):
79 '''Called when the library has some data for the user.'''
81 pos
= (wm
.ir
.x
, wm
.ir
.y
)
82 pygame
.mouse
.set_pos(pos
)
87 wiiuse
.is_just_pressed(wm
, wiiuse
.button
['B']) :
88 event
= pygame
.event
.Event(pygame
.MOUSEBUTTONDOWN
,
91 pygame
.event
.post(event
)
93 if wm
.btns_released
and \
94 wiiuse
.is_released(wm
, wiiuse
.button
['B']):
95 event
= pygame
.event
.Event(pygame
.MOUSEBUTTONUP
,
98 pygame
.event
.post(event
)
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
,
108 led
=[led
[i
] for i
in range(4)],
112 def disconnect_cb(self
, wmp
):
113 '''What should we do here?'''
114 pygame
.event
.post(pygame
.event
.Event(WIIMOTE_DISCONNECT
,
119 for i
in range(self
.nmotes
):
120 wiiuse
.set_leds(self
.wiimotes
[i
], 0)
121 wiiuse
.disconnect(self
.wiimotes
[i
])
125 return self
.actual_nmotes
130 def init(nmotes
, timeout
, screenResolution
=(660, 370)):
131 '''Initialize the module.'''
135 WT
= wiimote_thread(nmotes
, timeout
)
138 for i
in range(nmotes
) :
139 wm
= Wiimote(i
) # access the wiimote object
140 wm
.enable_accels(0) # turn off acceleration reporting
141 wm
.enable_ir(1, vres
= screenResolution
, position
=wiiuse
.IR_BELOW
)
145 '''How many Wiimotes were found?'''
146 return WT
.get_count()
149 '''Gracefully shutdown the connection and turn off the wiimote leds'''
153 class wiimote(object):
154 '''Object representing a Wiimote'''
155 def __init__(self
, n
):
156 self
.wm
= WT
.wiimotes
[n
]
158 def enable_leds(self
, m
):
159 '''Control leds. The lower 4 bits map to the 4 leds'''
160 WT
.do(wiiuse
.set_leds
, self
.wm
, sum([wiiuse
.LED
[i
] for i
in range(4) if m
& (1<<i
)]))
162 def enable_rumble(self
, on
):
164 WT
.do(wiiuse
.rumble
, self
.wm
, on
)
166 def enable_accels(self
, on
):
167 '''Control reporting of accelerometer data.'''
168 WT
.do(wiiuse
.motion_sensing
, self
.wm
, on
)
170 def enable_ir(self
, on
, vres
=None, position
=None, aspect
=None):
171 '''Control reporting IR data.'''
172 WT
.do(wiiuse
.set_ir
, self
.wm
, on
)
174 WT
.do(wiiuse
.set_ir_vres
, self
.wm
, *vres
)
175 if position
is not None:
176 WT
.do(wiiuse
.set_ir_position
, self
.wm
, position
)
177 if aspect
is not None:
178 WT
.do(wiiuse
.set_aspect_ratio
, self
.wm
, aspect
)
180 def set_flags(self
, smoothing
=None, continuous
=None, threshold
=None):
181 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
183 if smoothing
is not None:
185 enable |
= wiiuse
.SMOOTHING
187 disable |
= wiiuse
.SMOOTHING
188 if continuous
is not None:
190 enable |
= wiiuse
.CONTINUOUS
192 disable |
= wiiuse
.CONTINUOUS
193 if threshold
is not None:
195 enable |
= wiiuse
.ORIENT_THRESH
197 disable |
= wiiuse
.ORIENT_THRESH
198 WT
.do(wiiuse
.set_flags
, self
.wm
, enable
, disable
)
200 def set_orient_thresh(self
, thresh
):
201 '''Set orientation threshold'''
202 WT
.do(wiiuse
.set_orient_threshold
, self
.wm
, thresh
)
205 '''Trigger a status callback.'''
206 WT
.do(wiiuse
.status
, self
.wm
)
208 def disconnect(self
):
209 '''Disconnect this Wiimote'''
210 WT
.do(wiiuse
.disconnect(self
.wm
))
213 '''Get the object for the nth Wiimote'''