Last active
September 20, 2016 01:26
-
-
Save RandomInsano/bc158fd9bdb1adf927984ca26db918c9 to your computer and use it in GitHub Desktop.
Playing around with xwiimote to make a weigh scale
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
#!/usr/bin/env python | |
import errno | |
from sys import stdout | |
from time import sleep | |
from select import poll, POLLIN | |
from inspect import getmembers | |
import xwiimote | |
def get_device(entry): | |
if entry is None: | |
return None | |
try: | |
dev = xwiimote.iface(entry) | |
print "Found", dev.get_devtype() | |
dev.opened() | |
dev.open(dev.available() | xwiimote.IFACE_WRITABLE) | |
return dev | |
except IOError as e: | |
print "Error:", e | |
return None | |
def get_balance_board(): | |
bb = None | |
try: | |
mon = xwiimote.monitor(True, True) | |
count = 0 | |
while bb is None: | |
stdout.write("Waiting {0}...\r".format(count)) | |
stdout.flush() | |
bb = get_device(mon.poll()) | |
count += 1 | |
sleep(1) | |
return bb | |
except SystemError as e: | |
print "Monitor error:",e | |
while True: | |
dev = get_balance_board() | |
# display some information and open the iface | |
try: | |
print "Battery:", dev.get_battery(), "%" | |
except SystemError as e: | |
print "ooops", e | |
exit(1) | |
p = poll() | |
p.register(dev.get_fd(), POLLIN) | |
evt = xwiimote.event() | |
n = 0 | |
while n < 2: | |
p.poll() | |
try: | |
dev.dispatch(evt) | |
if evt.type == xwiimote.EVENT_KEY: | |
code, state = evt.get_key() | |
print "Key:", code, ", State:", state | |
n+=1 | |
elif evt.type == xwiimote.EVENT_GONE: | |
print "Gone" | |
n = 2 | |
elif evt.type == xwiimote.EVENT_WATCH: | |
print "Watch" | |
elif evt.type == xwiimote.EVENT_BALANCE_BOARD: | |
br, _, _ = evt.get_abs(0) | |
fr, _, _ = evt.get_abs(1) | |
bl, _, _ = evt.get_abs(2) | |
fl, _, _ = evt.get_abs(3) | |
weight = (br + fr + bl + fl) / 100.0 | |
stdout.write("{:3.1f} kg \r".format(weight)) | |
stdout.flush() | |
else: | |
if evt.type != xwiimote.EVENT_ACCEL: | |
print "type:", evt.type | |
except IOError as e: | |
if e.errno != errno.EAGAIN: | |
print "Bad" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment