Simple library for MAG3110 magenetometer as built into Avnet Wi-Go module

Dependencies:   MotionSensor

Dependents:   Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more

Revision:
6:f510561f6107
Parent:
1:5a0e7a58d980
Child:
7:0f45239e157a
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 );
+
+}
+