Junichi Katsu
/
ChuckChoroQ
ヌンチャクでコントロールするチョロQ機能に限定した物です。
Fork of StarBoardOrangeExample3 by
extlib/WiiNunchuck/WiiNunchuckReader.cpp@2:1d9c1cd90695, 2012-08-18 (annotated)
- Committer:
- jksoft
- Date:
- Sat Aug 18 13:21:50 2012 +0000
- Revision:
- 2:1d9c1cd90695
- Parent:
- 0:127b9ca59547
Rev1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 0:127b9ca59547 | 1 | /* |
shintamainjp | 0:127b9ca59547 | 2 | * WiiNunchuckReader. A program allowing the output of one or two |
shintamainjp | 0:127b9ca59547 | 3 | * Wii Nunchucks to be read via I2C and decoded for use, using the mbed |
shintamainjp | 0:127b9ca59547 | 4 | * microcontroller and its associated libraries. |
shintamainjp | 0:127b9ca59547 | 5 | * |
shintamainjp | 0:127b9ca59547 | 6 | * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> |
shintamainjp | 0:127b9ca59547 | 7 | * |
shintamainjp | 0:127b9ca59547 | 8 | * This file is part of WiiNunchuckReader. |
shintamainjp | 0:127b9ca59547 | 9 | * |
shintamainjp | 0:127b9ca59547 | 10 | * WiiNunchuckReader is free software: you can redistribute it and/or modify |
shintamainjp | 0:127b9ca59547 | 11 | * it under the terms of the GNU General Public License as published by |
shintamainjp | 0:127b9ca59547 | 12 | * the Free Software Foundation, either version 3 of the License, or |
shintamainjp | 0:127b9ca59547 | 13 | * (at your option) any later version. |
shintamainjp | 0:127b9ca59547 | 14 | * |
shintamainjp | 0:127b9ca59547 | 15 | * WiiNunchuckReader is distributed in the hope that it will be useful, |
shintamainjp | 0:127b9ca59547 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
shintamainjp | 0:127b9ca59547 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
shintamainjp | 0:127b9ca59547 | 18 | * GNU General Public License for more details. |
shintamainjp | 0:127b9ca59547 | 19 | * |
shintamainjp | 0:127b9ca59547 | 20 | * You should have received a copy of the GNU General Public License |
shintamainjp | 0:127b9ca59547 | 21 | * along with WiiNunchuckReader. If not, see <http://www.gnu.org/licenses/>. |
shintamainjp | 0:127b9ca59547 | 22 | */ |
shintamainjp | 0:127b9ca59547 | 23 | |
shintamainjp | 0:127b9ca59547 | 24 | #include "WiiNunchuckReader.h" |
shintamainjp | 0:127b9ca59547 | 25 | |
shintamainjp | 0:127b9ca59547 | 26 | // constructor |
shintamainjp | 0:127b9ca59547 | 27 | WiiNunchuckReader::WiiNunchuckReader(PinName sda, PinName scl) : |
shintamainjp | 0:127b9ca59547 | 28 | joyX(0), joyY(0), accelX(0), accelY(0), accelZ(0), |
shintamainjp | 0:127b9ca59547 | 29 | buttonC(0), buttonZ(0), nunchuckInit(false), |
shintamainjp | 0:127b9ca59547 | 30 | nunchuckPort(sda, scl) { |
shintamainjp | 0:127b9ca59547 | 31 | } |
shintamainjp | 0:127b9ca59547 | 32 | |
shintamainjp | 0:127b9ca59547 | 33 | void WiiNunchuckReader::RequestRead() { |
shintamainjp | 0:127b9ca59547 | 34 | // don't expect client to remember to send an init to the nunchuck |
shintamainjp | 0:127b9ca59547 | 35 | // so do it for them here |
shintamainjp | 0:127b9ca59547 | 36 | if (!nunchuckInit) { |
shintamainjp | 0:127b9ca59547 | 37 | nunchuckInit = NunchuckInit(); |
shintamainjp | 0:127b9ca59547 | 38 | } |
shintamainjp | 0:127b9ca59547 | 39 | |
shintamainjp | 0:127b9ca59547 | 40 | if (nunchuckInit) { // don't start reading if init failed |
shintamainjp | 0:127b9ca59547 | 41 | if (NunchuckRead()) { |
shintamainjp | 0:127b9ca59547 | 42 | // only decode successful reads |
shintamainjp | 0:127b9ca59547 | 43 | NunchuckDecode(); |
shintamainjp | 0:127b9ca59547 | 44 | } |
shintamainjp | 0:127b9ca59547 | 45 | } |
shintamainjp | 0:127b9ca59547 | 46 | } |
shintamainjp | 0:127b9ca59547 | 47 | |
shintamainjp | 0:127b9ca59547 | 48 | bool WiiNunchuckReader::NunchuckInit() { |
shintamainjp | 0:127b9ca59547 | 49 | bool success = false; |
shintamainjp | 0:127b9ca59547 | 50 | |
shintamainjp | 0:127b9ca59547 | 51 | const BYTE cmd[] = {NUNCHUCK_REGADDR, 0x00}; |
shintamainjp | 0:127b9ca59547 | 52 | if (I2C_OK == nunchuckPort.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd))) success = true; |
shintamainjp | 0:127b9ca59547 | 53 | |
shintamainjp | 0:127b9ca59547 | 54 | return success; |
shintamainjp | 0:127b9ca59547 | 55 | } |
shintamainjp | 0:127b9ca59547 | 56 | |
shintamainjp | 0:127b9ca59547 | 57 | bool WiiNunchuckReader::NunchuckRead() { |
shintamainjp | 0:127b9ca59547 | 58 | bool success = false; |
shintamainjp | 0:127b9ca59547 | 59 | |
shintamainjp | 0:127b9ca59547 | 60 | // write the address we want to read from |
shintamainjp | 0:127b9ca59547 | 61 | const BYTE cmd[] = {0x00}; |
shintamainjp | 0:127b9ca59547 | 62 | if (I2C_OK == nunchuckPort.write(NUNCHUCK_ADDR, (const char*)cmd, sizeof(cmd))) { |
shintamainjp | 0:127b9ca59547 | 63 | // the Wii Nunchuck is non-standard I2C |
shintamainjp | 0:127b9ca59547 | 64 | // and can't manage setting the read address and immediately supplying the data |
shintamainjp | 0:127b9ca59547 | 65 | // so wait a bit |
shintamainjp | 0:127b9ca59547 | 66 | wait(I2C_READ_DELAY); |
shintamainjp | 0:127b9ca59547 | 67 | |
shintamainjp | 0:127b9ca59547 | 68 | if (I2C_OK == nunchuckPort.read(NUNCHUCK_ADDR, readBuf, sizeof(readBuf))) success = true; |
shintamainjp | 0:127b9ca59547 | 69 | } |
shintamainjp | 0:127b9ca59547 | 70 | |
shintamainjp | 0:127b9ca59547 | 71 | return success; |
shintamainjp | 0:127b9ca59547 | 72 | } |
shintamainjp | 0:127b9ca59547 | 73 | |
shintamainjp | 0:127b9ca59547 | 74 | void WiiNunchuckReader::NunchuckDecode() { |
shintamainjp | 0:127b9ca59547 | 75 | joyX = (int)readBuf[JOY_X]; |
shintamainjp | 0:127b9ca59547 | 76 | joyY = (int)readBuf[JOY_Y]; |
shintamainjp | 0:127b9ca59547 | 77 | |
shintamainjp | 0:127b9ca59547 | 78 | // the accelerometer axis values are really 10 bit values |
shintamainjp | 0:127b9ca59547 | 79 | // so shift the 8 bit values read from the nunchuck |
shintamainjp | 0:127b9ca59547 | 80 | // 2 bits to the right |
shintamainjp | 0:127b9ca59547 | 81 | accelX = (int)readBuf[ACCEL_X] << 2; |
shintamainjp | 0:127b9ca59547 | 82 | accelY = (int)readBuf[ACCEL_Y] << 2; |
shintamainjp | 0:127b9ca59547 | 83 | accelZ = (int)readBuf[ACCEL_Z] << 2; |
shintamainjp | 0:127b9ca59547 | 84 | |
shintamainjp | 0:127b9ca59547 | 85 | DecodeAdditional(); |
shintamainjp | 0:127b9ca59547 | 86 | } |
shintamainjp | 0:127b9ca59547 | 87 | |
shintamainjp | 0:127b9ca59547 | 88 | void WiiNunchuckReader::DecodeAdditional() { |
shintamainjp | 0:127b9ca59547 | 89 | // the nunchcuk buttons have their own idea of state |
shintamainjp | 0:127b9ca59547 | 90 | int buttonState = readBuf[ADDITIONAL] & MASK_CZ; |
shintamainjp | 0:127b9ca59547 | 91 | switch (buttonState) { |
shintamainjp | 0:127b9ca59547 | 92 | case 3: |
shintamainjp | 0:127b9ca59547 | 93 | buttonZ = 0; |
shintamainjp | 0:127b9ca59547 | 94 | buttonC = 0; |
shintamainjp | 0:127b9ca59547 | 95 | break; |
shintamainjp | 0:127b9ca59547 | 96 | case 2: |
shintamainjp | 0:127b9ca59547 | 97 | buttonZ = 1; |
shintamainjp | 0:127b9ca59547 | 98 | buttonC = 1; |
shintamainjp | 0:127b9ca59547 | 99 | break; |
shintamainjp | 0:127b9ca59547 | 100 | case 1: |
shintamainjp | 0:127b9ca59547 | 101 | buttonZ = 0; |
shintamainjp | 0:127b9ca59547 | 102 | buttonC = 1; |
shintamainjp | 0:127b9ca59547 | 103 | break; |
shintamainjp | 0:127b9ca59547 | 104 | case 0: |
shintamainjp | 0:127b9ca59547 | 105 | buttonZ = 1; |
shintamainjp | 0:127b9ca59547 | 106 | buttonC = 0; |
shintamainjp | 0:127b9ca59547 | 107 | break; |
shintamainjp | 0:127b9ca59547 | 108 | } |
shintamainjp | 0:127b9ca59547 | 109 | |
shintamainjp | 0:127b9ca59547 | 110 | // and the accelerometer axis - for each axis value add bit 1 and bit 0 |
shintamainjp | 0:127b9ca59547 | 111 | // as required |
shintamainjp | 0:127b9ca59547 | 112 | if (readBuf[ADDITIONAL] & MASK_ACCLX1) accelX += 2; |
shintamainjp | 0:127b9ca59547 | 113 | if (readBuf[ADDITIONAL] & MASK_ACCLX2) accelX += 1; |
shintamainjp | 0:127b9ca59547 | 114 | if (readBuf[ADDITIONAL] & MASK_ACCLY1) accelY += 2; |
shintamainjp | 0:127b9ca59547 | 115 | if (readBuf[ADDITIONAL] & MASK_ACCLY2) accelY += 1; |
shintamainjp | 0:127b9ca59547 | 116 | if (readBuf[ADDITIONAL] & MASK_ACCLZ1) accelZ += 2; |
shintamainjp | 0:127b9ca59547 | 117 | if (readBuf[ADDITIONAL] & MASK_ACCLZ2) accelZ += 1; |
shintamainjp | 0:127b9ca59547 | 118 | } |