James Devine / LSM303D

Dependents:   Cosmic_Pi_STM32_i2c

Files at this revision

API Documentation at this revision

Comitter:
pingu_98
Date:
Sun Jan 06 19:13:18 2019 +0000
Parent:
2:8cd0c3c11b48
Commit message:
modified to avoid i2c noncopy violation in mbed5.x

Changed in this revision

LSM303D.cpp Show annotated file Show diff for this revision Revisions of this file
LSM303D.h Show annotated file Show diff for this revision Revisions of this file
--- a/LSM303D.cpp	Mon Feb 08 17:57:22 2016 +0000
+++ b/LSM303D.cpp	Sun Jan 06 19:13:18 2019 +0000
@@ -60,31 +60,12 @@
     CTRL7       = 0x26
 };
 
-bool LSM303D::write_reg(int addr_i2c,int addr_reg, char v)
-{
-    char data[2] = {addr_reg, v}; 
-    return LSM303D::_LSM303.write(addr_i2c, data, 2) == 0;
-}
-
-bool LSM303D::read_reg(int addr_i2c,int addr_reg, char *v)
-{
-    char data = addr_reg; 
-    bool result = false;
-    
-    __disable_irq();
-    if ((_LSM303.write(addr_i2c, &data, 1) == 0) && (_LSM303.read(addr_i2c, &data, 1) == 0)){
-        *v = data;
-        result = true;
-    }
-    __enable_irq();
-    return result;
-}
-
-LSM303D::LSM303D(PinName sda, PinName scl):
-    _LSM303(sda, scl)
+LSM303D::LSM303D(I2C *i2c):
+    i2c_(i2c)
+    //_LSM303(sda, scl)
 {
     char reg_v;
-    _LSM303.frequency(200000);
+    //_LSM303.frequency(200000);
         
     reg_v = 0;
     
@@ -104,20 +85,41 @@
     write_reg(addr_acc_mag,CTRL7,reg_v);
 }
 
+bool LSM303D::write_reg(int addr_i2c,int addr_reg, char v)
+{
+    char data[2] = {addr_reg, v}; 
+    i2c_->write(addr_i2c, data, 2);
+    return 0;
+}
+
+bool LSM303D::read_reg(int addr_i2c,int addr_reg, char *v)
+{
+    char data = addr_reg; 
+    bool result = false;
+    
+    __disable_irq();
+    if ((i2c_->write(addr_i2c, &data, 1) == 0) && (i2c_->write(addr_i2c, &data, 1) == 0)){
+        *v = data;
+        result = true;
+    }
+    __enable_irq();
+    return result;
+}
+
 
 bool LSM303D::read(float *ax, float *ay, float *az, float *mx, float *my, float *mz) {
     char acc[6], mag[6];
  
     if (recv(addr_acc_mag, OUT_X_A, acc, 6) && recv(addr_acc_mag, OUT_X_M, mag, 6)) {
-        *ax = float(short(acc[1] << 8 | acc[0]))*0.061;  //32768/4=8192
-        *ay =  float(short(acc[3] << 8 | acc[2]))*0.061;
-        *az =  float(short(acc[5] << 8 | acc[4]))*0.061;
+        *ax = float(short(acc[1] << 8 | acc[0]))*0.00061f;  //32768/4=8192
+        *ay =  float(short(acc[3] << 8 | acc[2]))*0.00061f;
+        *az =  float(short(acc[5] << 8 | acc[4]))*0.00061f;
         //+-4gauss
-        *mx = float(short(mag[0] << 8 | mag[1]))*0.16;
-        *mz = float(short(mag[2] << 8 | mag[3]))*0.16;
-        *my = float(short(mag[4] << 8 | mag[5]))*0.16;
+        *mx = float(short(mag[0] << 8 | mag[1]))*0.16f;
+        *mz = float(short(mag[2] << 8 | mag[3]))*0.16f;
+        *my = float(short(mag[4] << 8 | mag[5]))*0.16f;
  
-        return true;
+        return 0;
     }
  
     return false;
@@ -127,5 +129,5 @@
 bool LSM303D::recv(char sad, char sub, char *buf, int length) {
     if (length > 1) sub |= 0x80;
  
-    return _LSM303.write(sad, &sub, 1, true) == 0 && _LSM303.read(sad, buf, length) == 0;
+    return i2c_->write(sad, &sub, 1, true) == 0 && i2c_->read(sad, buf, length) == 0;
 }
--- a/LSM303D.h	Mon Feb 08 17:57:22 2016 +0000
+++ b/LSM303D.h	Sun Jan 06 19:13:18 2019 +0000
@@ -12,7 +12,8 @@
          * @param sda is the pin for the I2C SDA line
          * @param scl is the pin for the I2C SCL line
          */
-        LSM303D(PinName sda, PinName scl);
+        LSM303D(I2C *i2c);
+        //Si7020(I2C *i2c);
 
   
         /** read the raw accelerometer and compass values
@@ -24,8 +25,8 @@
 
 
     private:
-        I2C _LSM303;
-
+        //I2C _LSM303;
+        I2C *i2c_;
          
         float ax, ay, az;
         float mx, my, mz;