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/

WiiClassicControllerWithCalibration.cpp

Committer:
RichardE
Date:
2013-06-30
Revision:
2:52c4a0b3a509
Child:
3:ecae3d286a99

File content as of revision 2:52c4a0b3a509:

/*
 * SOURCE FILE : WiiClassicControllerWithCalibration.cpp
 *
 * Definition of class WiiClassicControllerWithCalibration.
 *
 */

#include "WiiClassicControllerWithCalibration.h"

/** Constructor
 * @param sda pin to use for SDA.
 * @param scl pin to use for SCL.
 */
WiiClassicControllerWithCalibration::WiiClassicControllerWithCalibration( PinName sda, PinName scl ) :
    WiiClassicController( sda, scl )
{
    // Set default scaling factors.
    // Left joystick is 6 bit reading. Raw reading of 0 gives -1. Raw reading of 63 gives +1.
    SetScaling( LeftJoyX, (float)( 2.0 / 63.0 ), -1 );
    SetScaling( LeftJoyY, (float)( 2.0 / 63.0 ), -1 );
    // Right joystick is 5 bit reading. Raw reading of 0 gives -1. Raw reading of 31 gives +1.
    SetScaling( RightJoyX, (float)( 2.0 / 31.0 ), -1 );
    SetScaling( RightJoyY, (float)( 2.0 / 31.0 ), -1 );
    // Left trigger is 5 bit reading. Raw reading of 0 gives 0. Raw reading of 31 gives +1.
    SetScaling( LeftTrigger, (float)( 1.0 / 31.0 ), 0 );
    // Right trigger is 5 bit reading. Raw reading of 0 gives 0. Raw reading of 31 gives +1.
    SetScaling( RightTrigger, (float)( 1.0 / 31.0 ), 0 );
}

/** Destructor
 */
WiiClassicControllerWithCalibration::~WiiClassicControllerWithCalibration() {
}

/** Set scaling for a particular analogue input.
 * @param m scale (multiplier) for this analogue input.
 * @param c offset for this analogue input.
 */
 void WiiClassicControllerWithCalibration::SetScaling( AnaIn input, float m, float c ) {
    if( ( (int)input >= 0 ) && ( (int)input < (int)AnaInCount ) ) {
        AnaInRec *ptr = records + (int)input;
        ptr->Scale = m;
        ptr->Offset = c;
    }
 }

/** Get calibrated left joystick X reading.
 * @returns a reading between -1 and +1.
 */
float WiiClassicControllerWithCalibration::GetCalLJoyX( void ) const {
    return GetScaled( LeftJoyX, GetLJoyX() );
}

/** Get calibrated left joystick Y reading.
 * @returns a reading between -1 and +1.
 */
float WiiClassicControllerWithCalibration::GetCalLJoyY( void ) const {
    return GetScaled( LeftJoyY, GetLJoyY() );
}

/** Get calibrated right joystick X reading.
 * @returns a reading between -1 and +1.
 */
float WiiClassicControllerWithCalibration::GetCalRJoyX( void ) const {
    return GetScaled( RightJoyX, GetRJoyX() );
}

/** Get calibrated right joystick Y reading.
 * @returns a reading between -1 and +1.
 */
float WiiClassicControllerWithCalibration::GetCalRJoyY( void ) const {
    return GetScaled( RightJoyY, GetRJoyY() );
}

/** Get calibrated left trigger reading.
 * @returns a reading between 0 and +1.
 */
float WiiClassicControllerWithCalibration::GetCalLeftTrigger( void ) const {
    return GetScaled( LeftTrigger, GetLeftTrigger() );
}

/** Get calibrated right trigger reading.
 * @returns a reading between 0 and +1.
 */
float WiiClassicControllerWithCalibration::GetCalRightTrigger( void ) const {
    return GetScaled( RightTrigger, GetRightTrigger() );
}

/** Get scaled reading.
 * @param input analogue input to scale.
 * @param raw raw readings in counts.
 * @returns scaled reading.
 */
float WiiClassicControllerWithCalibration::GetScaled( AnaIn input, UInt8 raw ) const {
    if( ( (int)input >= 0 ) && ( (int)input < (int)AnaInCount ) ) {
        const AnaInRec *ptr = records + (int)input;
        return (float)raw * ptr->Scale + ptr->Offset;
    }
    else {
        return (float)0;
    }
}