| 1 | #!/usr/bin/python |
|---|
| 2 | import sys |
|---|
| 3 | sys.path.insert(0, '/home/tbble/code/cwiid/svn/cwiid/python/build/lib.linux-x86_64-2.5/') |
|---|
| 4 | import cwiid |
|---|
| 5 | import sys |
|---|
| 6 | |
|---|
| 7 | def main(): |
|---|
| 8 | #Connect to address given on command-line, if present |
|---|
| 9 | print 'Put Wiimote in discoverable mode now (press 1+2)...' |
|---|
| 10 | global wiimote |
|---|
| 11 | if len(sys.argv) > 1: |
|---|
| 12 | wiimote = cwiid.Wiimote(sys.argv[1]) |
|---|
| 13 | else: |
|---|
| 14 | wiimote = cwiid.Wiimote() |
|---|
| 15 | |
|---|
| 16 | wiimote.rpt_mode = cwiid.RPT_BALANCE | cwiid.RPT_BTN |
|---|
| 17 | wiimote.request_status() |
|---|
| 18 | |
|---|
| 19 | if wiimote.state['ext_type'] != cwiid.EXT_BALANCE: |
|---|
| 20 | print 'This program only supports the Wii Balance Board' |
|---|
| 21 | wiimote.close() |
|---|
| 22 | return -1 |
|---|
| 23 | |
|---|
| 24 | balance_calibration = wiimote.get_balance_cal() |
|---|
| 25 | named_calibration = { 'right_top': balance_calibration[0], |
|---|
| 26 | 'right_bottom': balance_calibration[1], |
|---|
| 27 | 'left_top': balance_calibration[2], |
|---|
| 28 | 'left_bottom': balance_calibration[3], |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | exit = False |
|---|
| 32 | while not exit: |
|---|
| 33 | print "Type q to quit, or anything else to report your weight" |
|---|
| 34 | c = sys.stdin.read(1) |
|---|
| 35 | if c == 'q': |
|---|
| 36 | exit = True |
|---|
| 37 | wiimote.request_status() |
|---|
| 38 | print "%.2fkg" % (calcweight(wiimote.state['balance'], named_calibration) / 100.0, ) |
|---|
| 39 | |
|---|
| 40 | return 0 |
|---|
| 41 | |
|---|
| 42 | def calcweight( readings, calibrations ): |
|---|
| 43 | """ |
|---|
| 44 | Determine the weight of the user on the board in hundredths of a kilogram |
|---|
| 45 | """ |
|---|
| 46 | weight = 0 |
|---|
| 47 | for sensor in ('right_top', 'right_bottom', 'left_top', 'left_bottom'): |
|---|
| 48 | reading = readings[sensor] |
|---|
| 49 | calibration = calibrations[sensor] |
|---|
| 50 | # if reading < calibration[0]: |
|---|
| 51 | # print "Warning, %s reading below lower calibration value" % sensor |
|---|
| 52 | if reading > calibration[2]: |
|---|
| 53 | print "Warning, %s reading above upper calibration value" % sensor |
|---|
| 54 | # 1700 appears to be the step the calibrations are against. |
|---|
| 55 | # 17kg per sensor is 68kg, 1/2 of the advertised Japanese weight limit. |
|---|
| 56 | if reading < calibration[1]: |
|---|
| 57 | weight += 1700 * (reading - calibration[0]) / (calibration[1] - calibration[0]) |
|---|
| 58 | else: |
|---|
| 59 | weight += 1700 * (reading - calibration[1]) / (calibration[2] - calibration[1]) + 1700 |
|---|
| 60 | |
|---|
| 61 | return weight |
|---|
| 62 | |
|---|
| 63 | if __name__ == "__main__": |
|---|
| 64 | sys.exit(main()) |
|---|