![](/media/cache/profiles/Chris_Profile_Pic_kyGJjzP.jpg.50x50_q85.jpg)
Simons Wii controlled m3pi program
Dependencies: mbed m3pi ID12RFIDIRQ
Wiimote.cpp@0:0ab65a1aef12, 2011-05-18 (annotated)
- Committer:
- chris
- Date:
- Wed May 18 14:50:46 2011 +0000
- Revision:
- 0:0ab65a1aef12
Added some printing to the m3pi screen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:0ab65a1aef12 | 1 | /* Copyright (c) 2011, mbed |
chris | 0:0ab65a1aef12 | 2 | * |
chris | 0:0ab65a1aef12 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
chris | 0:0ab65a1aef12 | 4 | * of this software and associated documentation files (the "Software"), to deal |
chris | 0:0ab65a1aef12 | 5 | * in the Software without restriction, including without limitation the rights |
chris | 0:0ab65a1aef12 | 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
chris | 0:0ab65a1aef12 | 7 | * copies of the Software, and to permit persons to whom the Software is |
chris | 0:0ab65a1aef12 | 8 | * furnished to do so, subject to the following conditions: |
chris | 0:0ab65a1aef12 | 9 | * |
chris | 0:0ab65a1aef12 | 10 | * The above copyright notice and this permission notice shall be included in |
chris | 0:0ab65a1aef12 | 11 | * all copies or substantial portions of the Software. |
chris | 0:0ab65a1aef12 | 12 | * |
chris | 0:0ab65a1aef12 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
chris | 0:0ab65a1aef12 | 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
chris | 0:0ab65a1aef12 | 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
chris | 0:0ab65a1aef12 | 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
chris | 0:0ab65a1aef12 | 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
chris | 0:0ab65a1aef12 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
chris | 0:0ab65a1aef12 | 19 | * THE SOFTWARE. |
chris | 0:0ab65a1aef12 | 20 | */ |
chris | 0:0ab65a1aef12 | 21 | |
chris | 0:0ab65a1aef12 | 22 | // Simple decode class for Wii Remote data |
chris | 0:0ab65a1aef12 | 23 | |
chris | 0:0ab65a1aef12 | 24 | #include "Wiimote.h" |
chris | 0:0ab65a1aef12 | 25 | |
chris | 0:0ab65a1aef12 | 26 | // Wii Report Formats: |
chris | 0:0ab65a1aef12 | 27 | // * http://wiibrew.org/wiki/Wiimote#0x37:_Core_Buttons_and_Accelerometer_with_10_IR_bytes_and_6_Extension_Bytes |
chris | 0:0ab65a1aef12 | 28 | // * http://wiki.wiimoteproject.com/Reports#0x37_BTN_.2B_XLR_.2B_IR_.2B_6_EXT |
chris | 0:0ab65a1aef12 | 29 | |
chris | 0:0ab65a1aef12 | 30 | // Input Report 0x37: |
chris | 0:0ab65a1aef12 | 31 | // |
chris | 0:0ab65a1aef12 | 32 | // 0 [ ? | X(1:0) | PLUS | UP | DOWN | RIGHT | LEFT ] |
chris | 0:0ab65a1aef12 | 33 | // 1 [ HOME | Z(1) | Y(1) | MINUS | A | B | ONE | TWO ] |
chris | 0:0ab65a1aef12 | 34 | // 2 [ X(9:2) ] |
chris | 0:0ab65a1aef12 | 35 | // 3 [ Y(9:2) ] |
chris | 0:0ab65a1aef12 | 36 | // 4 [ Z(9:2) ] |
chris | 0:0ab65a1aef12 | 37 | // |
chris | 0:0ab65a1aef12 | 38 | |
chris | 0:0ab65a1aef12 | 39 | #include <stdio.h> |
chris | 0:0ab65a1aef12 | 40 | #include <math.h> |
chris | 0:0ab65a1aef12 | 41 | |
chris | 0:0ab65a1aef12 | 42 | #define WII_ZERO 0x206 |
chris | 0:0ab65a1aef12 | 43 | #define WII_1G 0x6A |
chris | 0:0ab65a1aef12 | 44 | |
chris | 0:0ab65a1aef12 | 45 | void Wiimote::decode(char* data) { |
chris | 0:0ab65a1aef12 | 46 | |
chris | 0:0ab65a1aef12 | 47 | // read buttons |
chris | 0:0ab65a1aef12 | 48 | left = (data[0] >> 0) & 0x1; |
chris | 0:0ab65a1aef12 | 49 | right = (data[0] >> 1) & 0x1; |
chris | 0:0ab65a1aef12 | 50 | down = (data[0] >> 2) & 0x1; |
chris | 0:0ab65a1aef12 | 51 | up = (data[0] >> 3) & 0x1; |
chris | 0:0ab65a1aef12 | 52 | plus = (data[0] >> 4) & 0x1; |
chris | 0:0ab65a1aef12 | 53 | two = (data[1] >> 0) & 0x1; |
chris | 0:0ab65a1aef12 | 54 | one = (data[1] >> 1) & 0x1; |
chris | 0:0ab65a1aef12 | 55 | b = (data[1] >> 2) & 0x1; |
chris | 0:0ab65a1aef12 | 56 | a = (data[1] >> 3) & 0x1; |
chris | 0:0ab65a1aef12 | 57 | minus = (data[1] >> 4) & 0x1; |
chris | 0:0ab65a1aef12 | 58 | home = (data[1] >> 7) & 0x1; |
chris | 0:0ab65a1aef12 | 59 | |
chris | 0:0ab65a1aef12 | 60 | // read accelerometers |
chris | 0:0ab65a1aef12 | 61 | rawx = (data[2] << 2) | (data[0] & 0x60) >> 5; |
chris | 0:0ab65a1aef12 | 62 | rawy = (data[3] << 2) | (data[1] & 0x20) >> 4; |
chris | 0:0ab65a1aef12 | 63 | rawz = (data[4] << 2) | (data[1] & 0x40) >> 5; |
chris | 0:0ab65a1aef12 | 64 | |
chris | 0:0ab65a1aef12 | 65 | // calculate accelerometer gravity |
chris | 0:0ab65a1aef12 | 66 | x = (float)((int)rawx - WII_ZERO) / (float)WII_1G; |
chris | 0:0ab65a1aef12 | 67 | y = (float)((int)rawy - WII_ZERO) / (float)WII_1G; |
chris | 0:0ab65a1aef12 | 68 | z = (float)((int)rawz - WII_ZERO) / (float)WII_1G; |
chris | 0:0ab65a1aef12 | 69 | |
chris | 0:0ab65a1aef12 | 70 | // calculate wheel angle |
chris | 0:0ab65a1aef12 | 71 | wheel = atan2(-y, -x) * (180.0 / 3.141592); |
chris | 0:0ab65a1aef12 | 72 | } |
chris | 0:0ab65a1aef12 | 73 | |
chris | 0:0ab65a1aef12 | 74 | void Wiimote::dump() { |
chris | 0:0ab65a1aef12 | 75 | printf("%d%d%d%d%d%d%d%d%d%d%d %.3f %.3f %.3f %.2f\n", left, right, down, up, plus, two, one, b, a, minus, home, x, y, z, wheel); |
chris | 0:0ab65a1aef12 | 76 | } |
chris | 0:0ab65a1aef12 | 77 | |
chris | 0:0ab65a1aef12 | 78 | |
chris | 0:0ab65a1aef12 | 79 | // Accelerometer data |
chris | 0:0ab65a1aef12 | 80 | // y- |
chris | 0:0ab65a1aef12 | 81 | // +---+ +--+ |
chris | 0:0ab65a1aef12 | 82 | // | + | + B |
chris | 0:0ab65a1aef12 | 83 | // | A | A | |
chris | 0:0ab65a1aef12 | 84 | // | | | | |
chris | 0:0ab65a1aef12 | 85 | // x+ | | x- z+ | | z- |
chris | 0:0ab65a1aef12 | 86 | // | 1 | 1 | |
chris | 0:0ab65a1aef12 | 87 | // | 2 | 2 | |
chris | 0:0ab65a1aef12 | 88 | // +---+ +--+ |
chris | 0:0ab65a1aef12 | 89 | // y+ |
chris | 0:0ab65a1aef12 | 90 | // |
chris | 0:0ab65a1aef12 | 91 | // x+ 0x19B |
chris | 0:0ab65a1aef12 | 92 | // x0 0x205 |
chris | 0:0ab65a1aef12 | 93 | // x- 0x26E |
chris | 0:0ab65a1aef12 | 94 | // |
chris | 0:0ab65a1aef12 | 95 | // y+ 0x19C 0x6A |
chris | 0:0ab65a1aef12 | 96 | // y0 0x206 |
chris | 0:0ab65a1aef12 | 97 | // y- 0x26E 0x68 |
chris | 0:0ab65a1aef12 | 98 | // |
chris | 0:0ab65a1aef12 | 99 | // z+ 0x19C |
chris | 0:0ab65a1aef12 | 100 | // z0 0x208 |
chris | 0:0ab65a1aef12 | 101 | // z- 0x26E |