Built from datasheet for 9-DOF sensor stick from SparkFun.

Dependents:   9Dof_unit_testing sparkfun6dof Seeed_Grove_Digital_Compass_Example CompassTest ... more

Revision:
1:8a1357c351c6
Parent:
0:8b84d61cee94
Child:
2:8eb755577f83
--- a/HMC5883L.h	Wed Oct 31 04:35:08 2012 +0000
+++ b/HMC5883L.h	Wed Oct 31 05:06:34 2012 +0000
@@ -74,6 +74,15 @@
 #define STATUS_LOCK         0x02
 #define STATUS_READY        0x01
 
+// Utility
+#ifndef M_PI
+#define M_PI 3.1415926535897932384626433832795
+#endif
+
+#define 2PI         (2*M_PI)
+#define RAD_TO_DEG  (180.0/M_PI)
+#define DEG_TO_RAD  (M_PI/180.0)
+
 /**
  * The HMC5883L 3-Axis Digital Compass IC
  */
@@ -84,16 +93,13 @@
 
     /**
      * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left).
-     *
-     * You don't need to manually set or clear the LSB when calling I2C::read() or I2C::write(),
-     * the library takes care of it.  We just always clear the LSB.
      */
     static const int I2C_ADDRESS = 0x3D;
 
     /**
      * Constructor.
      *
-     * Sets FS_SEL to 0x03 for proper opertaion.
+     * Calls init function
      *
      * @param sda - mbed pin to use for the SDA I2C line.
      * @param scl - mbed pin to use for the SCL I2C line.
@@ -102,6 +108,8 @@
 
     /**
     * Constructor that accepts external i2c interface object.
+    * 
+    * Calls init function
     *
     * @param i2c The I2C interface object to use.
     */
@@ -159,14 +167,7 @@
     /**
     * Funciton for setting the mode register
     * 
-    * |= 7 |= 6 |= 5 |= 4 |= 3 |= 2 |= 1 |= 0 |
-    * | (0)| (0)| (0)| (0)| (0)| (0)|MD1 | MD0|
-    *
-    * |=MD1 |=MD0 |=Operating Mode |=Constant |
-    * | 0 | 0 | Continuous-Measurement Mode | CONTINUOUS_MODE |
-    * | 0 | 1 | Single-Measument Mode | SINGLE_MODE |
-    * | 1 | 0 | Idle Mode | IDLE_MODE |
-    * | 1 | 1 | Idle Mode | IDLE_MODE |
+    * Constants: CONTINUOUS_MODE, SINGLE_MODE, IDLE_MODE
     * 
     * When you send a the Single-Measurement Mode instruction to the mode register
     * a single measurement is made, the RDY bit is set in the status register,
@@ -200,12 +201,37 @@
     /**
     * Function for retrieving the contents of status register
     * 
-    * |= 7 |= 6 |= 5 |= 4 |= 3 |= 2 |= 1 |= 0 |
-    * | (0)| (0)| (0)| (0)| (0)| (0)|LOCK | RDY|
+    * Bit1: LOCK, Bit0: RDY
     *
     * @returns status register
     */
     char getStatus();
+    
+    /**
+    * Function for getting radian heading using 2-dimensional calculation.
+    * 
+    * Compass must be held flat and away from an magnetic field generating
+    * devices such as cell phones and speakers.
+    *
+    * TODO: declenation angle compensation
+    * 
+    * @returns heading in radians
+    */
+    double getHeadingXY();
+    
+    /**
+    * Function for getting degree heading using 2-dimensional calculation.
+    * 
+    * Compass must be held flat and away from an magnetic field generating
+    * devices such as cell phones and speakers.
+    *
+    * TODO: declenation angle compensation
+    * 
+    * @returns heading in degrees
+    */
+    double getHeadingXYDeg() {
+        return (getHeadingXY() * RAD_TO_DEG);
+    }
 
 private: