Skip to content

Instantly share code, notes, and snippets.

@ganadist
Last active August 29, 2015 14:17
Show Gist options
  • Save ganadist/53d182846cb5e890e79a to your computer and use it in GitHub Desktop.
Save ganadist/53d182846cb5e890e79a to your computer and use it in GitHub Desktop.
wiifit
from evdev import InputDevice, categorize, list_devices, ecodes
class BalanceBoard:
def __init__(self, device):
self.dev = device
self.data = {}
def loop(self):
for ev in self.dev.read_loop():
if ev.type == ecodes.EV_ABS:
self.collect(ev)
elif ev.type == ecodes.EV_SYN:
self.emit(ev)
elif ev.type == ecodes.EV_KEY:
pass
else:
print(categorize(ev))
def collect(self, ev):
self.data[ev.code] = ev.value
def emit(self, ev):
if len(self.data) != 4:
return
print '%02.02f Kg'%(sum(self.data.values()) / 100.0)
self.data = {}
def probe():
NAME = 'Nintendo Wii Remote Balance Board'
devs = [InputDevice(fn) for fn in list_devices()]
for dev in devs:
if dev.name == NAME:
return dev
print 'there is no wii balance board'
if __name__ == '__main__':
dev = probe()
if not dev:
sys.exit(0)
w = BalanceBoard(dev)
w.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment