Library for communicating with a Wii classic controller using the I2C bus.

Dependents:   WiiClassicControllerTest

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WiiClassicController.h Source File

WiiClassicController.h

00001 /*
00002  * SOURCE FILE : WiiClassicController.h
00003  *
00004  * Definition of class WiiClassicController.
00005  * Allows use of a Wii classic controller using an I2C bus.
00006  *
00007  */
00008 
00009 #ifndef WiiClassicControllerDefined
00010 
00011   #define WiiClassicControllerDefined
00012 
00013   #include <mbed.h>
00014   #include "Types.h"
00015   
00016   #define CONTROLLER_ADDR     0xA4  // I2C library doesn't right shift the address, so provided shifted
00017   #define CONTROLLER_REGADDR  0x40  // relevant register address
00018   #define CONTROLLER_READLEN  0x06  // always read this many bytes back
00019 
00020   /// Allows use of a Wii classic controller using an I2C bus.
00021   class WiiClassicController {
00022 
00023   public :
00024 
00025     /** Constructor.
00026      * @param sda pin to use for SDA.
00027      * @param scl pin to use for SCL.
00028      */
00029     WiiClassicController( PinName sda, PinName scl );
00030 
00031     /** Destructor.
00032      */
00033     virtual ~WiiClassicController();
00034 
00035     /** Read from the controller.
00036      * @returns true on success, false on failure.
00037      */
00038     virtual bool Read( void );
00039     
00040     /** Read left joystick X axis.
00041      * @returns joystick reading as number between 0 and 63.
00042      */
00043     UInt8 GetLJoyX( void ) const {
00044         return (UInt8)( readBuf[ 0 ] & 0x3F );
00045     }
00046     
00047     /** Read left joystick Y axis.
00048      * @returns joystick reading as number between 0 and 63.
00049      */
00050     UInt8 GetLJoyY( void ) const {
00051         return (UInt8)( readBuf[ 1 ] & 0x3F );
00052     }
00053     
00054     /** Read right joystick X axis.
00055      * @returns joystick reading as number between 0 and 31.
00056      */
00057     UInt8 GetRJoyX( void ) const {
00058         return (UInt8)(
00059             ( ( readBuf[ 2 ] & 0x80 ) >> 7 ) |
00060             ( ( readBuf[ 1 ] & 0xC0 ) >> 5 ) |
00061             ( ( readBuf[ 0 ] & 0xC0 ) >> 3 )
00062         );
00063     }
00064     
00065     /** Read right joystick Y axis.
00066      * @returns joystick reading as number between 0 and 31.
00067      */
00068     UInt8 GetRJoyY( void ) const {
00069         return (UInt8)( readBuf[ 2 ] & 0x1F );
00070     }
00071 
00072     /** Read X button.
00073      * @returns true if button is pressed.
00074      */    
00075     bool GetButtonX( void ) const {
00076         return ( readBuf[ 5 ] & 0x08 ) ? false : true;
00077     }
00078     
00079     /** Read Y button.
00080      * @returns true if button is pressed.
00081      */    
00082     bool GetButtonY( void ) const {
00083         return ( readBuf[ 5 ] & 0x20 ) ? false : true;
00084     }
00085     
00086     /** Read A button.
00087      * @returns true if button is pressed.
00088      */    
00089     bool GetButtonA( void ) const {
00090         return ( readBuf[ 5 ] & 0x10 ) ? false : true;
00091     }
00092     
00093     /** Read B button.
00094      * @returns true if button is pressed.
00095      */    
00096     bool GetButtonB( void ) const {
00097         return ( readBuf[ 5 ] & 0x40 ) ? false : true;
00098     }
00099     
00100     /** Read left trigger button.
00101      * @returns true if button is pressed.
00102      */    
00103     bool GetButtonLT( void ) const {
00104         return ( readBuf[ 4 ] & 0x20 ) ? false : true;
00105     }
00106     
00107     /** Read right trigger button.
00108      * @returns true if button is pressed.
00109      */    
00110     bool GetButtonRT( void ) const {
00111         return ( readBuf[ 4 ] & 0x02 ) ? false : true;
00112     }
00113     
00114     /** Read left trigger reading.
00115      * @returns reading as a number between 0 and 31.
00116      */    
00117     UInt8 GetLeftTrigger( void ) const {
00118         return (UInt8)(
00119             ( ( readBuf[ 3 ] & 0xE0 ) >> 5 ) |
00120             ( ( readBuf[ 2 ] & 0x60 ) >> 2 )
00121         );
00122     }
00123     
00124     /** Read right trigger reading.
00125      * @returns reading as a number between 0 and 31.
00126      */    
00127     UInt8 GetRightTrigger( void ) const {
00128         return (UInt8)( readBuf[ 3 ] & 0x1F );
00129     }
00130     
00131     /** Read ZL button.
00132      * @returns true if button is pressed.
00133      */    
00134     bool GetButtonZL( void ) const {
00135         return ( readBuf[ 5 ] & 0x80 ) ? false : true;
00136     }
00137     
00138     /** Read ZR button.
00139      * @returns true if button is pressed.
00140      */    
00141     bool GetButtonZR( void ) const {
00142         return ( readBuf[ 5 ] & 0x04 ) ? false : true;
00143     }
00144     
00145     /** Read select (or minus) button.
00146      * @returns true if button is pressed.
00147      */    
00148     bool GetButtonSelect( void ) const {
00149         return ( readBuf[ 4 ] & 0x10 ) ? false : true;
00150     }
00151 
00152     /** Read home button.
00153      * @returns true if button is pressed.
00154      */    
00155     bool GetButtonHome( void ) const {
00156         return ( readBuf[ 4 ] & 0x08 ) ? false : true;
00157     }
00158     
00159     /** Read start (or plus) button.
00160      * @returns true if button is pressed.
00161      */    
00162     bool GetButtonStart( void ) const {
00163         return ( readBuf[ 4 ] & 0x04 ) ? false : true;
00164     }
00165     
00166     /** Read up button.
00167      * @returns true if button is pressed.
00168      */    
00169     bool GetButtonUp( void ) const {
00170         return ( readBuf[ 5 ] & 0x01 ) ? false : true;
00171     }
00172     
00173     /** Read down button.
00174      * @returns true if button is pressed.
00175      */    
00176     bool GetButtonDown( void ) const {
00177         return ( readBuf[ 4 ] & 0x40 ) ? false : true;
00178     }
00179     
00180     /** Read left button.
00181      * @returns true if button is pressed.
00182      */    
00183     bool GetButtonLeft( void ) const {
00184         return ( readBuf[ 5 ] & 0x02 ) ? false : true;
00185     }
00186     
00187     /** Read right button.
00188      * @returns true if button is pressed.
00189      */    
00190     bool GetButtonRight( void ) const {
00191         return ( readBuf[ 4 ] & 0x80 ) ? false : true;
00192     }
00193     
00194     /** Get size of read buffer.
00195      * @returns size of read buffer.
00196      */
00197     UInt8 GetReadBufSize( void ) const {
00198         return sizeof(readBuf);
00199     }
00200     
00201     /** Get address of read buffer.
00202      * @returns pointer to read buffer.
00203      */
00204     UInt8* GetReadBuf( void ) {
00205         return readBuf;
00206     }
00207 
00208   private :
00209 
00210     // I2C port used for comms with controller.  
00211     I2C controllerPort;
00212     
00213     // Set to true once controller has been initialised.
00214     bool initialised;
00215     
00216     // Buffer for incoming data.
00217     UInt8 readBuf[ CONTROLLER_READLEN ];
00218 
00219     /** Initialise the controller.
00220      * @returns true on success, false on failure.
00221      */
00222     bool ControllerInit( void );
00223     
00224     /** Read from the controller, assuming it has been initialised.
00225      * @returns true on success, false on failure.
00226      */
00227     bool ControllerRead( void );
00228 
00229     /** Decoder a byte in received packet.
00230      * @param x byte to decode
00231      * @returns decoded byte
00232      */
00233     UInt8 Decode( UInt8 x ) {
00234         return (x ^ 0x17) + 0x17;
00235     }
00236     
00237   };
00238 
00239 #endif
00240 
00241 /* END of WiiClassicController.h */
00242 
00243