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/

Files at this revision

API Documentation at this revision

Comitter:
RichardE
Date:
Sun Jun 30 12:05:27 2013 +0000
Parent:
1:9e766313a668
Child:
3:ecae3d286a99
Commit message:
Added WiiClassicControllerWithCalibration class. Scale and offset are present but there is currently no way to perform a calibration.

Changed in this revision

WiiClassicControllerWithCalibration.cpp Show annotated file Show diff for this revision Revisions of this file
WiiClassicControllerWithCalibration.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiiClassicControllerWithCalibration.cpp	Sun Jun 30 12:05:27 2013 +0000
@@ -0,0 +1,102 @@
+/*
+ * 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;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WiiClassicControllerWithCalibration.h	Sun Jun 30 12:05:27 2013 +0000
@@ -0,0 +1,100 @@
+/*
+ * SOURCE FILE : WiiClassicControllerWithCalibration.h
+ *
+ * Definition of class WiiClassicControllerWithCalibration.
+ *
+ */
+
+#ifndef WiiClassicControllerWithCalibrationDefined
+
+    #define WiiClassicControllerWithCalibrationDefined
+
+    #include "WiiClassicController.h"
+    
+    /// Derived from WiiClassicController but with calibrated analogue inputs.
+    class WiiClassicControllerWithCalibration : public WiiClassicController {
+
+    public :
+
+        /// Enumeration of all the analogue inputs on the Wii classic controller.
+        enum AnaIn {
+            LeftJoyX,
+            LeftJoyY,
+            RightJoyX,
+            RightJoyY,
+            LeftTrigger,
+            RightTrigger,
+            AnaInCount          // MUST COME LAST!
+        };
+                
+        /** Constructor
+         * @param sda pin to use for SDA.
+         * @param scl pin to use for SCL.
+         */
+        WiiClassicControllerWithCalibration( PinName sda, PinName scl );
+
+        /** Destructor
+         */
+        virtual ~WiiClassicControllerWithCalibration();
+
+        /** Set scaling for a particular analogue input.
+         * @param m scale (multiplier) for this analogue input.
+         * @param c offset for this analogue input.
+         */
+         void SetScaling( AnaIn input, float m, float c );
+         
+        /** Get calibrated left joystick X reading.
+         * @returns a reading between -1 and +1.
+         */
+        float GetCalLJoyX( void ) const;
+        
+        /** Get calibrated left joystick Y reading.
+         * @returns a reading between -1 and +1.
+         */
+        float GetCalLJoyY( void ) const;
+        
+        /** Get calibrated right joystick X reading.
+         * @returns a reading between -1 and +1.
+         */
+        float GetCalRJoyX( void ) const;
+        
+        /** Get calibrated right joystick Y reading.
+         * @returns a reading between -1 and +1.
+         */
+        float GetCalRJoyY( void ) const;
+
+        /** Get calibrated left trigger reading.
+         * @returns a reading between 0 and +1.
+         */
+        float GetCalLeftTrigger( void ) const;
+        
+        /** Get calibrated right trigger reading.
+         * @returns a reading between 0 and +1.
+         */
+        float GetCalRightTrigger( void ) const;
+
+    private :
+
+        // Record for each analogue input.
+        class AnaInRec {
+        public :
+            float Scale;
+            float Offset;
+        };
+            
+        // Records for all analogue inputs.
+        AnaInRec records[ (int)AnaInCount ];
+        
+        /** Get scaled reading.
+         * @param input analogue input to scale.
+         * @param raw raw readings in counts.
+         * @returns scaled reading.
+         */
+        float GetScaled( AnaIn input, UInt8 raw ) const;
+        
+    };
+
+#endif
+
+/* END of WiiClassicControllerWithCalibration.h */
+