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 WiiClassicControllerWithCalibration.h Source File

WiiClassicControllerWithCalibration.h

00001 /*
00002  * SOURCE FILE : WiiClassicControllerWithCalibration.h
00003  *
00004  * Definition of class WiiClassicControllerWithCalibration.
00005  *
00006  */
00007 
00008 #ifndef WiiClassicControllerWithCalibrationDefined
00009 
00010     #define WiiClassicControllerWithCalibrationDefined
00011 
00012     #include "WiiClassicController.h"
00013     
00014     /// Derived from WiiClassicController but with calibrated analogue inputs.
00015     class WiiClassicControllerWithCalibration : public WiiClassicController {
00016 
00017     public :
00018 
00019         /// Enumeration of all the analogue inputs on the Wii classic controller.
00020         enum AnaIn {
00021             LeftJoyX,
00022             LeftJoyY,
00023             RightJoyX,
00024             RightJoyY,
00025             LeftTrigger,
00026             RightTrigger,
00027             AnaInCount          // MUST COME LAST!
00028         };
00029                 
00030         /** Constructor
00031          * @param sda pin to use for SDA.
00032          * @param scl pin to use for SCL.
00033          */
00034         WiiClassicControllerWithCalibration( PinName sda, PinName scl );
00035 
00036         /** Destructor
00037          */
00038         virtual ~WiiClassicControllerWithCalibration();
00039 
00040         /** Read from the controller.
00041          * This override will also update calibration information
00042          * when calibration is in progress.
00043          * @returns true on success, false on failure.
00044          */
00045         virtual bool Read( void );
00046 
00047         /** Set scaling for a particular analogue input.
00048          * @param input channel to change.
00049          * @param m scale (multiplier) for this analogue input.
00050          * @param c offset for this analogue input.
00051          */
00052          void SetScaling( AnaIn input, float m, float c );
00053          
00054         /** Get scaling for a particular analogue input.
00055          * @param input channel to read.
00056          * @param m scale (multiplier) for this analogue input return here.
00057          * @param c offset for this analogue input returned here.
00058          */
00059          void GetScaling( AnaIn input, float *m, float *c ) const;
00060 
00061         /** Get calibrated left joystick X reading.
00062          * @returns a reading between -1 and +1.
00063          */
00064         float GetCalLJoyX( void ) const;
00065         
00066         /** Get calibrated left joystick Y reading.
00067          * @returns a reading between -1 and +1.
00068          */
00069         float GetCalLJoyY( void ) const;
00070         
00071         /** Get calibrated right joystick X reading.
00072          * @returns a reading between -1 and +1.
00073          */
00074         float GetCalRJoyX( void ) const;
00075         
00076         /** Get calibrated right joystick Y reading.
00077          * @returns a reading between -1 and +1.
00078          */
00079         float GetCalRJoyY( void ) const;
00080 
00081         /** Get calibrated left trigger reading.
00082          * @returns a reading between 0 and +1.
00083          */
00084         float GetCalLeftTrigger( void ) const;
00085         
00086         /** Get calibrated right trigger reading.
00087          * @returns a reading between 0 and +1.
00088          */
00089         float GetCalRightTrigger( void ) const;
00090 
00091         /** Get indication that calibration is in progress.
00092          * @returns true if calibration is in progress.
00093          */
00094         bool IsCalibrating( void ) const {
00095             return calibrating;
00096         }
00097         
00098         /** Start calibrating.
00099          * Every call to Read method updates calibration until StopCalibrating is called.
00100          */
00101         void StartCalibrating( void );
00102         
00103         /** Stop calibrating.
00104          */
00105         void StopCalibrating( void );
00106         
00107     private :
00108 
00109         // Indicates calibration is in progress.
00110         bool calibrating;
00111         
00112         // Record for each analogue input.
00113         class AnaInRec {
00114         
00115         public :
00116         
00117             float Scale;
00118             float Offset;
00119             UInt8 MaxCount;
00120             UInt8 MinCount;
00121             UInt8 ZeroCount;
00122             
00123             /** Calculate scale and offset for a bipolar reading.
00124              * @param minValue minimum value to read.
00125              * @param maxValue maximum value to read.
00126              */
00127              void CalculateScaleAndOffsetBiPolar( float minValue, float maxValue );
00128              
00129             /** Calculate scale and offset for a unipolar reading.
00130              * Minimum value is assumed to be zero.
00131              * @param maxValue maximum value to read.
00132              */
00133              void CalculateScaleAndOffsetUniPolar( float maxValue );
00134              
00135         };
00136             
00137         // Records for all analogue inputs.
00138         AnaInRec records[ (int)AnaInCount ];
00139         
00140         /** Get scaled reading.
00141          * @param input analogue input to scale.
00142          * @param raw raw readings in counts.
00143          * @param min minimum permitted value.
00144          * @param max maximum permited value.
00145          * @returns scaled reading.
00146          */
00147         float GetScaled( AnaIn input, UInt8 raw, float min, float max ) const;
00148         
00149         /** Update calibration information.
00150          */
00151         void UpdateCalibration( void );
00152         
00153         /** Get the raw counts for one of the analogue inputs.
00154          * @param input analogue input to read.
00155          * @returns raw counts for input.
00156          */
00157         UInt8 GetAnaIn( AnaIn input ) const;
00158 
00159     };
00160 
00161 #endif
00162 
00163 /* END of WiiClassicControllerWithCalibration.h */
00164