The original snake game for the mbedgc
Dependencies: mbed EthernetNetIf HTTPClient
Fork of SimpleLib_03272011 by
mbedGC/WiiClassicControllerReader.cpp@0:011be8250218, 2011-04-02 (annotated)
- Committer:
- jp
- Date:
- Sat Apr 02 23:23:07 2011 +0000
- Revision:
- 0:011be8250218
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jp | 0:011be8250218 | 1 | /* |
jp | 0:011be8250218 | 2 | * WiiClassicControllerReader. A program allowing the output of one or two |
jp | 0:011be8250218 | 3 | * Wii Classic Controllers to be read via I2C and decoded for use, using the mbed |
jp | 0:011be8250218 | 4 | * microcontroller and its associated libraries. |
jp | 0:011be8250218 | 5 | * |
jp | 0:011be8250218 | 6 | * Written by Alfredo Guerrero <alfredog83@gmail.com> for the mbedGC open-source |
jp | 0:011be8250218 | 7 | * game console <http://www.mbedgc.com>. Based on the original code for |
jp | 0:011be8250218 | 8 | * the WiiNunchuckReader written by Petras Saduikis <petras@petras.co.uk>. |
jp | 0:011be8250218 | 9 | * |
jp | 0:011be8250218 | 10 | * This file is part of WiiClassicControllerReader. |
jp | 0:011be8250218 | 11 | * |
jp | 0:011be8250218 | 12 | * WiiClassicControllerReader is free software: you can redistribute it and/or modify |
jp | 0:011be8250218 | 13 | * it under the terms of the GNU General Public License as published by |
jp | 0:011be8250218 | 14 | * the Free Software Foundation, either version 3 of the License, or |
jp | 0:011be8250218 | 15 | * (at your option) any later version. |
jp | 0:011be8250218 | 16 | * |
jp | 0:011be8250218 | 17 | * WiiClassicControllerReader is distributed in the hope that it will be useful, |
jp | 0:011be8250218 | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
jp | 0:011be8250218 | 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jp | 0:011be8250218 | 20 | * GNU General Public License for more details. |
jp | 0:011be8250218 | 21 | * |
jp | 0:011be8250218 | 22 | * You can find a copy of the GNU General Public License at <http://www.gnu.org/licenses/>. |
jp | 0:011be8250218 | 23 | */ |
jp | 0:011be8250218 | 24 | |
jp | 0:011be8250218 | 25 | #include "WiiClassicControllerReader.h" |
jp | 0:011be8250218 | 26 | |
jp | 0:011be8250218 | 27 | // constructor |
jp | 0:011be8250218 | 28 | WiiClassicControllerReader::WiiClassicControllerReader(PinName sda, PinName scl) : |
jp | 0:011be8250218 | 29 | controllerPort(sda, scl), |
jp | 0:011be8250218 | 30 | ljoyX(0), ljoyY(0), rjoyX(0), rjoyY(0), buttonX(0), buttonY(0), buttonA(0), |
jp | 0:011be8250218 | 31 | buttonB(0), buttonZL(0), buttonZR(0), buttonLT(0), buttonRT(0), buttonLC(0), |
jp | 0:011be8250218 | 32 | buttonRC(0), buttonHOME(0), buttonSELECT(0), buttonSTART(0), buttonDU(0), |
jp | 0:011be8250218 | 33 | buttonDD(0), buttonDL(0), buttonDR(0), controllerInit(false) |
jp | 0:011be8250218 | 34 | { |
jp | 0:011be8250218 | 35 | } |
jp | 0:011be8250218 | 36 | |
jp | 0:011be8250218 | 37 | void WiiClassicControllerReader::RequestRead() |
jp | 0:011be8250218 | 38 | { |
jp | 0:011be8250218 | 39 | // don't expect client to remember to send an init to the nunchuck |
jp | 0:011be8250218 | 40 | // so do it for them here |
jp | 0:011be8250218 | 41 | if (!controllerInit) |
jp | 0:011be8250218 | 42 | { |
jp | 0:011be8250218 | 43 | controllerInit = ControllerInit(); |
jp | 0:011be8250218 | 44 | } |
jp | 0:011be8250218 | 45 | |
jp | 0:011be8250218 | 46 | if (controllerInit) // don't start reading if init failed |
jp | 0:011be8250218 | 47 | { |
jp | 0:011be8250218 | 48 | if (ControllerRead()) |
jp | 0:011be8250218 | 49 | { |
jp | 0:011be8250218 | 50 | // only decode successful reads |
jp | 0:011be8250218 | 51 | ControllerDecode(); |
jp | 0:011be8250218 | 52 | } |
jp | 0:011be8250218 | 53 | } |
jp | 0:011be8250218 | 54 | } |
jp | 0:011be8250218 | 55 | |
jp | 0:011be8250218 | 56 | bool WiiClassicControllerReader::ControllerInit() |
jp | 0:011be8250218 | 57 | { |
jp | 0:011be8250218 | 58 | bool success = false; |
jp | 0:011be8250218 | 59 | |
jp | 0:011be8250218 | 60 | const BYTE cmd[] = {CONTROLLER_REGADDR, 0x00}; |
jp | 0:011be8250218 | 61 | if (I2C_OK == controllerPort.write(CONTROLLER_ADDR, (const char*)cmd, sizeof(cmd))) success = true; |
jp | 0:011be8250218 | 62 | |
jp | 0:011be8250218 | 63 | return success; |
jp | 0:011be8250218 | 64 | } |
jp | 0:011be8250218 | 65 | |
jp | 0:011be8250218 | 66 | bool WiiClassicControllerReader::ControllerRead() |
jp | 0:011be8250218 | 67 | { |
jp | 0:011be8250218 | 68 | bool success = false; |
jp | 0:011be8250218 | 69 | |
jp | 0:011be8250218 | 70 | // write the address we want to read from |
jp | 0:011be8250218 | 71 | const BYTE cmd[] = {0x00}; |
jp | 0:011be8250218 | 72 | if (I2C_OK == controllerPort.write(CONTROLLER_ADDR, (const char*)cmd, sizeof(cmd))) |
jp | 0:011be8250218 | 73 | { |
jp | 0:011be8250218 | 74 | // the Wii Classic Controller is non-standard I2C |
jp | 0:011be8250218 | 75 | // and can't manage setting the read address and immediately supplying the data |
jp | 0:011be8250218 | 76 | // so wait a bit |
jp | 0:011be8250218 | 77 | wait(I2C_READ_DELAY); |
jp | 0:011be8250218 | 78 | |
jp | 0:011be8250218 | 79 | if (I2C_OK == controllerPort.read(CONTROLLER_ADDR, readBuf, sizeof(readBuf))) success = true; |
jp | 0:011be8250218 | 80 | } |
jp | 0:011be8250218 | 81 | |
jp | 0:011be8250218 | 82 | return success; |
jp | 0:011be8250218 | 83 | } |
jp | 0:011be8250218 | 84 | |
jp | 0:011be8250218 | 85 | void WiiClassicControllerReader::ControllerDecode() |
jp | 0:011be8250218 | 86 | { |
jp | 0:011be8250218 | 87 | ljoyX = (int)readBuf[0] & MASK_LX; |
jp | 0:011be8250218 | 88 | ljoyY = (int)readBuf[1] & MASK_LY; |
jp | 0:011be8250218 | 89 | rjoyY = (int)readBuf[2] & MASK_RY; |
jp | 0:011be8250218 | 90 | buttonRT = (int)readBuf[3] & MASK_RT; |
jp | 0:011be8250218 | 91 | buttonRC = (int)readBuf[4] & MASK_RC_DL; |
jp | 0:011be8250218 | 92 | buttonSTART = (int)readBuf[4] & MASK_BSTART_ZR; |
jp | 0:011be8250218 | 93 | buttonHOME = (int)readBuf[4] & MASK_BHOME_X; |
jp | 0:011be8250218 | 94 | buttonSELECT = (int)readBuf[4] & MASK_BSELECT_A; |
jp | 0:011be8250218 | 95 | buttonLC = (int)readBuf[4] & MASK_LC_Y; |
jp | 0:011be8250218 | 96 | buttonDD = (int)readBuf[4] & MASK_BDD_B; |
jp | 0:011be8250218 | 97 | buttonDR = (int)readBuf[4] & MASK_BDR_ZL; |
jp | 0:011be8250218 | 98 | buttonDU = (int)readBuf[5] & MASK_BDU; |
jp | 0:011be8250218 | 99 | buttonDL = (int)readBuf[5] & MASK_RC_DL; |
jp | 0:011be8250218 | 100 | buttonZR = (int)readBuf[5] & MASK_BSTART_ZR; |
jp | 0:011be8250218 | 101 | buttonX = (int)readBuf[5] & MASK_BHOME_X; |
jp | 0:011be8250218 | 102 | buttonA = (int)readBuf[5] & MASK_BSELECT_A; |
jp | 0:011be8250218 | 103 | buttonY = (int)readBuf[5] & MASK_LC_Y; |
jp | 0:011be8250218 | 104 | buttonB = (int)readBuf[5] & MASK_BDD_B; |
jp | 0:011be8250218 | 105 | buttonZL = (int)readBuf[5] & MASK_BDR_ZL; |
jp | 0:011be8250218 | 106 | |
jp | 0:011be8250218 | 107 | // the RjoyX axis and LT values are really 5 bit values |
jp | 0:011be8250218 | 108 | // so shift the 2 bit values read from the controller |
jp | 0:011be8250218 | 109 | // 3 bits to the right |
jp | 0:011be8250218 | 110 | rjoyX = ( (int)readBuf[0] & MASK_RX34 ) << 3; |
jp | 0:011be8250218 | 111 | buttonLT = ( (int)readBuf[2] & MASK_LT34 ) << 3; |
jp | 0:011be8250218 | 112 | |
jp | 0:011be8250218 | 113 | // and for each value add bit 2, bit 1, and bit 0 |
jp | 0:011be8250218 | 114 | // as required |
jp | 0:011be8250218 | 115 | if (readBuf[2] & MASK_BDR_ZL) rjoyX += 1; |
jp | 0:011be8250218 | 116 | if (readBuf[1] & MASK_BDD_B) rjoyX += 2; |
jp | 0:011be8250218 | 117 | if (readBuf[1] & MASK_BDR_ZL) rjoyX += 4; |
jp | 0:011be8250218 | 118 | if (readBuf[3] & MASK_LC_Y) buttonLT += 1; |
jp | 0:011be8250218 | 119 | if (readBuf[3] & MASK_BDD_B) buttonLT += 2; |
jp | 0:011be8250218 | 120 | if (readBuf[3] & MASK_BDR_ZL) buttonLT += 4; |
jp | 0:011be8250218 | 121 | |
jp | 0:011be8250218 | 122 | } |