Test Ver

Dependencies:   mbed FatFileSystem

Committer:
jksoft
Date:
Sat Nov 17 13:22:00 2012 +0000
Revision:
0:269589d8d2c2
Test Program

Who changed what in which revision?

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