A program allowing the output of one or two Wii Nunchucks to be read via I2C and decoded for use.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * WiiNunchuckReader. A program allowing the output of one or two
00003 * Wii Nunchucks to be read via I2C and decoded for use, using the mbed 
00004 * microcontroller and its associated libraries.
00005 *
00006 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
00007 *
00008 * This file is part of WiiNunchuckReader.
00009 *
00010 * WiiNunchuckReader is free software: you can redistribute it and/or modify
00011 * it under the terms of the GNU General Public License as published by
00012 * the Free Software Foundation, either version 3 of the License, or
00013 * (at your option) any later version.
00014 * 
00015 * WiiNunchuckReader is distributed in the hope that it will be useful,
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 * GNU General Public License for more details.
00019 *
00020 * You should have received a copy of the GNU General Public License
00021 * along with WiiNunchuckReader.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 
00024 #include "I2CConfig.h"
00025 #include "WiiNunchuckReader.h"
00026 
00027 #define LOOP_DELAY    1    // seconds
00028 
00029 // global declarations
00030 Serial serial(USBTX, USBRX);
00031 
00032 void ReadAndReport(WiiNunchuckReader* const nchk, const char* const portname)
00033 {
00034     int bufSize = 0;
00035     char* bufPtr = NULL;
00036     bool debug = true;
00037 
00038     nchk->RequestRead();
00039     serial.printf("%s: ", portname);
00040         
00041     if (debug)
00042     {
00043         bufSize = nchk->getBufferSize();
00044         bufPtr = nchk->getReadBuf();
00045         if (bufPtr != NULL)
00046         {
00047             for (int i = 0; i < bufSize; i++)
00048             {
00049                 serial.printf("%x ", bufPtr[i]);
00050             }
00051             serial.printf("\r\n");
00052         }
00053     }
00054         
00055     serial.printf("%d\t", nchk->getButtonZ());
00056     serial.printf("%d\t", nchk->getButtonC());
00057     serial.printf("%d\t", nchk->getAccelX());
00058     serial.printf("%d\t", nchk->getAccelY());
00059     serial.printf("%d\t", nchk->getAccelZ());
00060     serial.printf("%d\t", nchk->getJoyX());
00061     serial.printf("%d\r\n", nchk->getJoyY());
00062     serial.printf("\r\n");
00063 }
00064 
00065 int main() 
00066 {
00067     WiiNunchuckReader nchkA(I2CPort_A::SDA, I2CPort_A::SCL);
00068     WiiNunchuckReader nchkB(I2CPort_B::SDA, I2CPort_B::SCL);
00069     
00070     while (true)
00071     {
00072         ReadAndReport(&nchkA, "PORT A");
00073         ReadAndReport(&nchkB, "PORT B");
00074                 
00075         wait(LOOP_DELAY);
00076     }
00077     
00078     return EXIT_SUCCESS;
00079 }