2c5911cc818a04b45a6c88c889b0490c156cf296
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
.timeout
= timeout
29 self
.startup
.get(True) # wait for the thread to get started and acquire the motes
32 '''This runs in a separate thread'''
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
)
40 for i
in range(self
.nmotes
):
41 wiiuse
.set_leds(self
.wiimotes
[i
], wiiuse
.LED
[i
])
43 self
.go
= self
.actual_nmotes
!= 0
45 self
.startup
.put(self
.go
)
48 if self
._paused
: continue
50 if wiiuse
.poll(self
.wiimotes
, self
.nmotes
) :
51 for i
in range(self
.nmotes
):
53 if m
[0].event
== wiiuse
.EVENT
:
59 # wiiuse.poll(self.wiimotes, self.nmotes)
63 # allow executing functions in this thread
66 func
, args
= self
.queue
.get_nowait()
69 print 'do:', func
.__name
__, args
78 def do(self
, func
, *args
):
79 '''Run the function in the thread handling the wiimote'''
80 self
.queue
.put((func
, args
))
82 def event_cb(self
, wmp
):
83 '''Called when the library has some data for the user.'''
85 pygame
.mouse
.set_pos((wm
.ir
.x
, wm
.ir
.y
))
87 def control_cb(self
, wmp
, attachment
, speaker
, ir
, led
, battery
):
88 '''Could check the battery level and such here'''
89 pygame
.event
.post(pygame
.event
.Event(WIIMOTE_STATUS
,
90 attachment
=attachment
,
93 led
=[led
[i
] for i
in range(4)],
97 def disconnect_cb(self
, wmp
):
98 '''What should we do here?'''
99 pygame
.event
.post(pygame
.event
.Event(WIIMOTE_DISCONNECT
,
104 for i
in range(self
.nmotes
):
105 wiiuse
.set_leds(self
.wiimotes
[i
], 0)
106 wiiuse
.disconnect(self
.wiimotes
[i
])
111 def init(nmotes
, timeout
):
112 '''Initialize the module.'''
116 WT
= wiimote_thread(nmotes
, timeout
)
119 '''How many Wiimotes were found?'''
120 return WT
.actual_nmotes
123 '''Gracefully shutdown the connection and turn off the wiimote leds'''
127 class wiimote(object):
128 '''Object representing a Wiimote'''
129 def __init__(self
, n
):
130 self
.wm
= WT
.wiimotes
[n
]
132 def enable_leds(self
, m
):
133 '''Control leds. The lower 4 bits map to the 4 leds'''
134 WT
.do(wiiuse
.set_leds
, self
.wm
, sum([wiiuse
.LED
[i
] for i
in range(4) if m
& (1<<i
)]))
136 def enable_rumble(self
, on
):
138 WT
.do(wiiuse
.rumble
, self
.wm
, on
)
140 def enable_accels(self
, on
):
141 '''Control reporting of accelerometer data.'''
142 WT
.do(wiiuse
.motion_sensing
, self
.wm
, on
)
144 def enable_ir(self
, on
, vres
=None, position
=None, aspect
=None):
145 '''Control reporting IR data.'''
146 WT
.do(wiiuse
.set_ir
, self
.wm
, on
)
148 WT
.do(wiiuse
.set_ir_vres
, self
.wm
, *vres
)
149 if position
is not None:
150 WT
.do(wiiuse
.set_ir_position
, self
.wm
, position
)
151 if aspect
is not None:
152 WT
.do(wiiuse
.set_aspect_ratio
, self
.wm
, aspect
)
154 def set_flags(self
, smoothing
=None, continuous
=None, threshold
=None):
155 '''Set flags SMOOTHING, CONTINUOUS, ORIENT_THRESH'''
157 if smoothing
is not None:
159 enable |
= wiiuse
.SMOOTHING
161 disable |
= wiiuse
.SMOOTHING
162 if continuous
is not None:
164 enable |
= wiiuse
.CONTINUOUS
166 disable |
= wiiuse
.CONTINUOUS
167 if threshold
is not None:
169 enable |
= wiiuse
.ORIENT_THRESH
171 disable |
= wiiuse
.ORIENT_THRESH
172 print enable
, disable
173 WT
.do(wiiuse
.set_flags
, self
.wm
, enable
, disable
)
175 def set_orient_thresh(self
, thresh
):
176 '''Set orientation threshold'''
177 WT
.do(wiiuse
.set_orient_threshold
, self
.wm
, thresh
)
180 '''Trigger a status callback.'''
181 WT
.do(wiiuse
.status
, self
.wm
)
183 def disconnect(self
):
184 '''Disconnect this Wiimote'''
185 WT
.do(wiiuse
.disconnect(self
.wm
))
188 '''Get the object for the nth Wiimote'''