I2C driver for HMC5583L digital compass sensor

Dependents:   m3Dpi

Files at this revision

API Documentation at this revision

Comitter:
sillevl
Date:
Sat Dec 19 10:40:55 2015 +0000
Parent:
0:91f08ac76444
Commit message:
add constructor that accepts I2C object

Changed in this revision

hmc5583l.cpp Show annotated file Show diff for this revision Revisions of this file
hmc5583l.h Show annotated file Show diff for this revision Revisions of this file
--- a/hmc5583l.cpp	Thu Dec 03 07:52:58 2015 +0000
+++ b/hmc5583l.cpp	Sat Dec 19 10:40:55 2015 +0000
@@ -1,13 +1,23 @@
 #include "hmc5583l.h"
 #include "stdint.h"
 
+HMC5583L::HMC5583L(I2C &_i2c, int _address): i2c(_i2c), address(_address)
+{
+    initialize();
+}
+
 HMC5583L::HMC5583L(PinName sda, PinName scl, int _address): i2c(sda, scl), address(_address)
 {
+    initialize();
+}
+
+void HMC5583L::initialize()
+{
     char data[2];
     data[0] = 0x02; // select mode register
     data[1] = 0x00; // continous measurement mode
 
-    i2c.write(address, data, 2);
+    i2c.write(address, data, 2);   
 }
 
 coord HMC5583L::getCompass()
--- a/hmc5583l.h	Thu Dec 03 07:52:58 2015 +0000
+++ b/hmc5583l.h	Sat Dec 19 10:40:55 2015 +0000
@@ -11,7 +11,8 @@
 
 class HMC5583L{
     public:
-    HMC5583L(PinName sda, PinName scl, int address);
+    HMC5583L(PinName sda, PinName scl, int address = 0x3D);
+    HMC5583L(I2C &i2c, int address = 0x3D);
     
     coord getCompass();
     
@@ -19,7 +20,9 @@
     int address;
     I2C i2c;
     
-    protected:
     int regToInt(char msb, char lsb);
     
+    private:
+    void initialize();
+    
 };
\ No newline at end of file