Library for communicating with a Wii classic controller using the I2C bus.
Dependents: WiiClassicControllerTest
Note that you will also need the CommonTypes library to use this.
Get it here:http://mbed.org/users/RichardE/code/CommonTypes/
Diff: WiiClassicController.cpp
- Revision:
- 3:ecae3d286a99
- Parent:
- 0:242100dadee8
--- a/WiiClassicController.cpp Sun Jun 30 12:05:27 2013 +0000 +++ b/WiiClassicController.cpp Sun Jun 30 16:37:49 2013 +0000 @@ -8,8 +8,8 @@ #include "WiiClassicController.h" -// REMOVE THIS! -extern Serial pc; +// Delay to slow things down. Doesn;t work without this. +#define WAIT_TIME ((float)0.01 ) // in seconds /** Constructor. * @param sda pin to use for SDA. @@ -45,7 +45,7 @@ bool WiiClassicController::ControllerInit( void ) { const UInt8 cmd[] = { CONTROLLER_REGADDR, 0x00 }; bool ok = ( controllerPort.write( CONTROLLER_ADDR, (const char*)cmd, sizeof(cmd) ) == 0 ); - pc.printf( "ControllerInit returned %d\r\n", (int)ok ); + wait( WAIT_TIME ); return ok; } @@ -53,24 +53,21 @@ * @returns true on success, false on failure. */ bool WiiClassicController::ControllerRead() { + bool ok = false; // write the address we want to read from const UInt8 cmd[] = { 0x00 }; if( controllerPort.write( CONTROLLER_ADDR, (const char*)cmd, sizeof(cmd) ) == 0 ) { // The Wii Classic Controller is non-standard I2C // and can't manage setting the read address and immediately supplying the data // so wait a bit. - wait( 0.01 ); + wait( WAIT_TIME ); if( controllerPort.read( CONTROLLER_ADDR, (char*)readBuf, sizeof(readBuf) ) == 0 ) { for( int i = 0; i < CONTROLLER_READLEN; ++i ) { readBuf[ i ] = Decode( readBuf[ i ] ); } - return true; - } - else { - return false; + ok = true; } } - else { - return false; - } + // wait( WAIT_TIME ); + return ok; }