MAG3110 library with calibrate operation included

Dependents:   FRDM-KL46-Template AxedaGo-Freescal_FRDM-KL46Z revert AxedaGo-Freescal_FRDM-KL46Z HC-05_S2B_HelloWorld_WIZwiki-W7500_sensores

Fork of MAG3110 by Andrew Lindsay

Files at this revision

API Documentation at this revision

Comitter:
mmaas
Date:
Tue Dec 31 17:16:37 2013 +0000
Parent:
4:cf40601402b7
Commit message:
Moved calibrate operation into the library

Changed in this revision

MAG3110.cpp Show annotated file Show diff for this revision Revisions of this file
MAG3110.h Show annotated file Show diff for this revision Revisions of this file
diff -r cf40601402b7 -r f510561f6107 MAG3110.cpp
--- a/MAG3110.cpp	Fri May 24 20:16:24 2013 +0000
+++ b/MAG3110.cpp	Tue Dec 31 17:16:37 2013 +0000
@@ -5,14 +5,14 @@
 /******************************************************************************
  * Constructors
  ******************************************************************************/
-MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), 
+MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
     _i2c_address(0x1D), _pc(NULL), _debug(false)
 {
     begin();
 }
 
-MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), 
-   _i2c_address(0x1D), _pc(pc), _debug(true)
+MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
+    _i2c_address(0x1D), _pc(pc), _debug(true)
 {
     begin();
 }
@@ -28,7 +28,7 @@
     cmd[0] = MAG_CTRL_REG1;
     cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
     _i2c.write(_i2c_address, cmd, 2);
-    
+
     // No adjustment initially
     _avgX = 0;
     _avgY = 0;
@@ -87,5 +87,32 @@
 
 
 
+void MAG3110::calXY(PinName pin, int activeValue )
+{
+    DigitalIn calPin(pin);
+    int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
+
+
+    // Wait for Button Press and Release before beginning calibration
+    while(calPin != activeValue) {}
+    while(calPin == activeValue) {}
 
 
+    // Read initial values of magnetomoter - read it here to create a slight delay for calPin to settle
+    tempXmax = tempXmin = readVal(MAG_OUT_X_MSB);
+    tempYmax = tempYmin = readVal(MAG_OUT_Y_MSB);
+
+    // Update min and max values until calPin asserted again
+    while(calPin != activeValue) {
+        newX = readVal(MAG_OUT_X_MSB);
+        newY = readVal(MAG_OUT_Y_MSB);
+        if (newX > tempXmax) tempXmax = newX;
+        if (newX < tempXmin) tempXmin = newX;
+        if (newY > tempYmax) tempYmax = newY;
+        if (newY < tempYmin) tempYmin = newY;
+    }
+
+    setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
+
+}
+
diff -r cf40601402b7 -r f510561f6107 MAG3110.h
--- a/MAG3110.h	Fri May 24 20:16:24 2013 +0000
+++ b/MAG3110.h	Tue Dec 31 17:16:37 2013 +0000
@@ -132,6 +132,12 @@
      * @param maxY maximum value for Y range
      */
     void setCalibration(int minX, int maxX, int minY, int maxY);
+    /**
+     * Perfrom the calibration process.
+     * @param calPin GPIO pinName to use for coordinating the board rotation
+     * @param activeValue the GPIO pin value when input asserted
+     */    
+     void calXY(PinName pin, int activeValue);
 
 private:
     I2C _i2c;