Petras Saduikis
/
WiiNunchuckReader
A program allowing the output of one or two Wii Nunchucks to be read via I2C and decoded for use.
main.cpp@0:d51f5e2478c1, 2009-12-13 (annotated)
- Committer:
- snatch59
- Date:
- Sun Dec 13 12:19:31 2009 +0000
- Revision:
- 0:d51f5e2478c1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
snatch59 | 0:d51f5e2478c1 | 1 | /* |
snatch59 | 0:d51f5e2478c1 | 2 | * WiiNunchuckReader. A program allowing the output of one or two |
snatch59 | 0:d51f5e2478c1 | 3 | * Wii Nunchucks to be read via I2C and decoded for use, using the mbed |
snatch59 | 0:d51f5e2478c1 | 4 | * microcontroller and its associated libraries. |
snatch59 | 0:d51f5e2478c1 | 5 | * |
snatch59 | 0:d51f5e2478c1 | 6 | * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> |
snatch59 | 0:d51f5e2478c1 | 7 | * |
snatch59 | 0:d51f5e2478c1 | 8 | * This file is part of WiiNunchuckReader. |
snatch59 | 0:d51f5e2478c1 | 9 | * |
snatch59 | 0:d51f5e2478c1 | 10 | * WiiNunchuckReader is free software: you can redistribute it and/or modify |
snatch59 | 0:d51f5e2478c1 | 11 | * it under the terms of the GNU General Public License as published by |
snatch59 | 0:d51f5e2478c1 | 12 | * the Free Software Foundation, either version 3 of the License, or |
snatch59 | 0:d51f5e2478c1 | 13 | * (at your option) any later version. |
snatch59 | 0:d51f5e2478c1 | 14 | * |
snatch59 | 0:d51f5e2478c1 | 15 | * WiiNunchuckReader is distributed in the hope that it will be useful, |
snatch59 | 0:d51f5e2478c1 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
snatch59 | 0:d51f5e2478c1 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
snatch59 | 0:d51f5e2478c1 | 18 | * GNU General Public License for more details. |
snatch59 | 0:d51f5e2478c1 | 19 | * |
snatch59 | 0:d51f5e2478c1 | 20 | * You should have received a copy of the GNU General Public License |
snatch59 | 0:d51f5e2478c1 | 21 | * along with WiiNunchuckReader. If not, see <http://www.gnu.org/licenses/>. |
snatch59 | 0:d51f5e2478c1 | 22 | */ |
snatch59 | 0:d51f5e2478c1 | 23 | |
snatch59 | 0:d51f5e2478c1 | 24 | #include "I2CConfig.h" |
snatch59 | 0:d51f5e2478c1 | 25 | #include "WiiNunchuckReader.h" |
snatch59 | 0:d51f5e2478c1 | 26 | |
snatch59 | 0:d51f5e2478c1 | 27 | #define LOOP_DELAY 1 // seconds |
snatch59 | 0:d51f5e2478c1 | 28 | |
snatch59 | 0:d51f5e2478c1 | 29 | // global declarations |
snatch59 | 0:d51f5e2478c1 | 30 | Serial serial(USBTX, USBRX); |
snatch59 | 0:d51f5e2478c1 | 31 | |
snatch59 | 0:d51f5e2478c1 | 32 | void ReadAndReport(WiiNunchuckReader* const nchk, const char* const portname) |
snatch59 | 0:d51f5e2478c1 | 33 | { |
snatch59 | 0:d51f5e2478c1 | 34 | int bufSize = 0; |
snatch59 | 0:d51f5e2478c1 | 35 | char* bufPtr = NULL; |
snatch59 | 0:d51f5e2478c1 | 36 | bool debug = true; |
snatch59 | 0:d51f5e2478c1 | 37 | |
snatch59 | 0:d51f5e2478c1 | 38 | nchk->RequestRead(); |
snatch59 | 0:d51f5e2478c1 | 39 | serial.printf("%s: ", portname); |
snatch59 | 0:d51f5e2478c1 | 40 | |
snatch59 | 0:d51f5e2478c1 | 41 | if (debug) |
snatch59 | 0:d51f5e2478c1 | 42 | { |
snatch59 | 0:d51f5e2478c1 | 43 | bufSize = nchk->getBufferSize(); |
snatch59 | 0:d51f5e2478c1 | 44 | bufPtr = nchk->getReadBuf(); |
snatch59 | 0:d51f5e2478c1 | 45 | if (bufPtr != NULL) |
snatch59 | 0:d51f5e2478c1 | 46 | { |
snatch59 | 0:d51f5e2478c1 | 47 | for (int i = 0; i < bufSize; i++) |
snatch59 | 0:d51f5e2478c1 | 48 | { |
snatch59 | 0:d51f5e2478c1 | 49 | serial.printf("%x ", bufPtr[i]); |
snatch59 | 0:d51f5e2478c1 | 50 | } |
snatch59 | 0:d51f5e2478c1 | 51 | serial.printf("\r\n"); |
snatch59 | 0:d51f5e2478c1 | 52 | } |
snatch59 | 0:d51f5e2478c1 | 53 | } |
snatch59 | 0:d51f5e2478c1 | 54 | |
snatch59 | 0:d51f5e2478c1 | 55 | serial.printf("%d\t", nchk->getButtonZ()); |
snatch59 | 0:d51f5e2478c1 | 56 | serial.printf("%d\t", nchk->getButtonC()); |
snatch59 | 0:d51f5e2478c1 | 57 | serial.printf("%d\t", nchk->getAccelX()); |
snatch59 | 0:d51f5e2478c1 | 58 | serial.printf("%d\t", nchk->getAccelY()); |
snatch59 | 0:d51f5e2478c1 | 59 | serial.printf("%d\t", nchk->getAccelZ()); |
snatch59 | 0:d51f5e2478c1 | 60 | serial.printf("%d\t", nchk->getJoyX()); |
snatch59 | 0:d51f5e2478c1 | 61 | serial.printf("%d\r\n", nchk->getJoyY()); |
snatch59 | 0:d51f5e2478c1 | 62 | serial.printf("\r\n"); |
snatch59 | 0:d51f5e2478c1 | 63 | } |
snatch59 | 0:d51f5e2478c1 | 64 | |
snatch59 | 0:d51f5e2478c1 | 65 | int main() |
snatch59 | 0:d51f5e2478c1 | 66 | { |
snatch59 | 0:d51f5e2478c1 | 67 | WiiNunchuckReader nchkA(I2CPort_A::SDA, I2CPort_A::SCL); |
snatch59 | 0:d51f5e2478c1 | 68 | WiiNunchuckReader nchkB(I2CPort_B::SDA, I2CPort_B::SCL); |
snatch59 | 0:d51f5e2478c1 | 69 | |
snatch59 | 0:d51f5e2478c1 | 70 | while (true) |
snatch59 | 0:d51f5e2478c1 | 71 | { |
snatch59 | 0:d51f5e2478c1 | 72 | ReadAndReport(&nchkA, "PORT A"); |
snatch59 | 0:d51f5e2478c1 | 73 | ReadAndReport(&nchkB, "PORT B"); |
snatch59 | 0:d51f5e2478c1 | 74 | |
snatch59 | 0:d51f5e2478c1 | 75 | wait(LOOP_DELAY); |
snatch59 | 0:d51f5e2478c1 | 76 | } |
snatch59 | 0:d51f5e2478c1 | 77 | |
snatch59 | 0:d51f5e2478c1 | 78 | return EXIT_SUCCESS; |
snatch59 | 0:d51f5e2478c1 | 79 | } |