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