Last active
August 29, 2015 14:17
-
-
Save ganadist/53d182846cb5e890e79a to your computer and use it in GitHub Desktop.
wiifit
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
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