うおーるぼっと用プログラム Wiiリモコンからのダイレクト操作モードのみ BlueUSBをベースに使用しています。

Dependencies:   BD6211F mbed SimpleFilter

Committer:
jksoft
Date:
Fri Apr 29 15:50:23 2011 +0000
Revision:
0:4f749f62c6d7

        

Who changed what in which revision?

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