Skip to content

Instantly share code, notes, and snippets.

@maebert
Created May 29, 2012 16:10
Show Gist options
  • Save maebert/2829262 to your computer and use it in GitHub Desktop.
Save maebert/2829262 to your computer and use it in GitHub Desktop.
Python WiiMote
"""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