Generalized adaptation of the WiiChuk_compat library.

Fork of WiiChuk_compat by Greg Brush

Files at this revision

API Documentation at this revision

Comitter:
d34d
Date:
Mon Dec 22 03:32:51 2014 +0000
Parent:
4:e39bbb89c4e9
Commit message:
Add method for calibrating joystick center position.

Changed in this revision

WiiChuck.cpp Show annotated file Show diff for this revision Revisions of this file
WiiChuck.h Show annotated file Show diff for this revision Revisions of this file
diff -r e39bbb89c4e9 -r be9ce129de7c WiiChuck.cpp
--- a/WiiChuck.cpp	Sun Dec 21 18:24:50 2014 +0000
+++ b/WiiChuck.cpp	Mon Dec 22 03:32:51 2014 +0000
@@ -53,6 +53,25 @@
      _callback = NULL;
 }
 
+bool WiiChuck::calibrateJoystickNeutralPosition(uint8_t* centerX, uint8_t* centerY) {
+    if (!_initialized) return false;
+    
+    nunchuck_data_t data;
+    uint16_t tempX = 0, tempY = 0;
+    
+    for (size_t i = 0; i < 8; i++) {
+        if (!this->read(&data)) return false;
+        
+        tempX += data.joyX;
+        tempY += data.joyY;
+    }
+    
+    // take the average of the readings ( >> 3 == / 8)
+    *centerX = tempX >> 3;
+    *centerY = tempY >> 3;
+    return true;
+}
+
 void WiiChuck::getValues() {
     bool hasData = read(&_data);
   
diff -r e39bbb89c4e9 -r be9ce129de7c WiiChuck.h
--- a/WiiChuck.h	Sun Dec 21 18:24:50 2014 +0000
+++ b/WiiChuck.h	Mon Dec 22 03:32:51 2014 +0000
@@ -123,6 +123,14 @@
      * Detach the callback and stop polling the nunchuck
      */
     void detach();
+    
+    /**
+     * Gets the value for the x and y position of the joystick when it is in the neutral position.
+     *
+     * @param centerX Pointer to a uint8_t that will hold the center x value
+     * @param centerY Pointer to a uint8_t that will hold the center y value
+     */
+    bool calibrateJoystickNeutralPosition(uint8_t* centerX, uint8_t* centerY);
 
 private: 
     void getValues();