Created
May 29, 2012 16:10
-
-
Save maebert/2829262 to your computer and use it in GitHub Desktop.
Python WiiMote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Wiimote wrapper class.""" | |
import cwiid | |
class Wiimote(): | |
_buttons = { | |
'B': 1<<2, 'home': 1<<7 | |
} | |
def __init__(self, calib = (100, 150, 100, 150)): | |
self.wm = None | |
self.pos = (0, 0) | |
failures = 0 | |
while not self.wm: | |
try: | |
self.wm = cwiid.Wiimote('00:18:00:08:DB:44') | |
except RuntimeError: | |
failures +=1 | |
print "Still looking... (%d failed attempts)" % failures | |
self.wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC | |
self.min_x, self.max_x, self.min_y, self.max_y = calib | |
def pressed(self, alt=False): | |
if not alt: | |
return bool(self.wm.state['buttons'] & self._buttons['B']) | |
else: | |
return bool(self.wm.state['buttons'] & self._buttons['home']) | |
def get_state_on_click(self): | |
while not self.pressed(): | |
pass | |
acc = self.wm.state['acc'] | |
self.wm.rumble = 1 | |
while self.pressed(): | |
pass | |
self.wm.rumble = 0 | |
return acc | |
def calibrate(self): | |
print("Point Down") | |
_, self.max_x, _ = self.get_state_on_click() | |
print("Point Up") | |
_, self.min_x, _ = self.get_state_on_click() | |
print("Roll Right") | |
self.min_y, _, _ = self.get_state_on_click() | |
print("Roll Left") | |
self.max_y, _, _ = self.get_state_on_click() | |
def get(self): | |
x, y, _ = self.wm.state['acc'] | |
px = 2.0 * (x - self.min_x) / (self.max_x - self.min_x) - 1 | |
py = 2.0 * (y - self.min_y) / (self.max_y - self.min_y) - 1 | |
self.pos = (py, px) | |
return self.pos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment