eCompass (6-axes electronic compass) / Electronic Compass with Three-axis Magnetic Field Sensor and Three-axis Accelerometer by Bosch Sensortech

Dependents:   BLE_EddystoneBeacon_w_ACC_TY51822

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Wed Aug 23 09:22:29 2017 +0000
Parent:
2:93141eb80862
Commit message:
countermeasure for NonCopyable

Changed in this revision

BMC050.cpp Show annotated file Show diff for this revision Revisions of this file
BMC050.h Show annotated file Show diff for this revision Revisions of this file
diff -r 93141eb80862 -r 24aa4d5fa7de BMC050.cpp
--- a/BMC050.cpp	Wed Jun 01 12:30:38 2016 +0000
+++ b/BMC050.cpp	Wed Aug 23 09:22:29 2017 +0000
@@ -3,17 +3,11 @@
  *  BMC050 COMPASS 6 AXIS, made by Bosch Sensortec
  *      http://jp.bosch-sensortec.com/content/language1/html/5033.htm
  *
- * Copyright (c) 2014, 2016 Kenji Arai / JH1PJL
+ * Copyright (c) 2014,'16,'17 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
- *      Created: July      19th, 2014 
- *      Revised: June       1st, 2016
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
- * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *      Created: July       19th, 2014 
+ *      Revised: August     23rd, 2017
  */
 
 #include "BMC050.h"
@@ -21,8 +15,8 @@
 BMC050::BMC050 (
     PinName p_sda, PinName p_scl,
     const BMC050ACC_TypeDef *acc_parameter,
-    const BMC050MAG_TypeDef *mag_parameter) :
-    i2c(p_sda, p_scl)
+    const BMC050MAG_TypeDef *mag_parameter)
+ : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
 {
     initialize (acc_parameter, mag_parameter);
 }
@@ -30,8 +24,8 @@
 BMC050::BMC050 (
     I2C& p_i2c,
     const BMC050ACC_TypeDef *acc_parameter,
-    const BMC050MAG_TypeDef *mag_parameter) :
-    i2c(p_i2c)
+    const BMC050MAG_TypeDef *mag_parameter)
+ :  _i2c(p_i2c)
 {
     initialize (acc_parameter, mag_parameter);
 }
@@ -48,18 +42,18 @@
     if (mag_addr != BMC050_MAG_NOT_USED_ADDR) {
         dbf[0] = BMC050_M_POWER_MODE;
         dbf[1] = 0x01;  // Power control bit on
-        i2c.write(mag_addr, dbf, 2);
+        _i2c.write(mag_addr, dbf, 2);
         dbf[0] = BMC050_M_OPERATION;
         dbf[1] = 0;
-        i2c.write(mag_addr, dbf, 2);
+        _i2c.write(mag_addr, dbf, 2);
     }
     /////////////// Accelerometer configuration ////////////////
     // Check acc chip is available of not
     // step 1/start
     acc_addr = acc_parameter->addr;
     dbf[0] = BMC050_A_WHO_AM_I; 
-    i2c.write(acc_addr, dbf, 1); 
-    i2c.read(acc_addr, dbf, 1);
+    _i2c.write(acc_addr, dbf, 1); 
+    _i2c.read(acc_addr, dbf, 1);
     if (dbf[0] == I_AM_BMC050_ACC){ acc_ready = 1;
     } else {                        acc_ready = 0; }
     /////////////// Magnetometer Configuration /////////////////
@@ -67,10 +61,10 @@
     //      -> Need to go "Normal mode" via "Sleep mode"
     // step 2 
     if (mag_addr != BMC050_MAG_NOT_USED_ADDR) {
-        i2c.write(mag_addr, dbf, 2);
+        _i2c.write(mag_addr, dbf, 2);
         dbf[0] = BMC050_M_OPERATION;
         dbf[1] = 0;
-        i2c.write(mag_addr, dbf, 2);
+        _i2c.write(mag_addr, dbf, 2);
     }
     /////////////// Accelerometer configuration ////////////////
     // step 2/last
@@ -95,7 +89,7 @@
             fs_factor_acc = 256;
             dbf[1] = BMC050_FS_2G;
         }          
-        i2c.write(acc_addr, dbf, 2);
+        _i2c.write(acc_addr, dbf, 2);
         // set bandwidth
         dbf[1] = acc_parameter->bandwith;
         if (dbf[1] == BMC050_NOT_FILTERED){
@@ -104,22 +98,22 @@
         } else {
             dbf[0] = BMC050_A_BANDWIDTH;
         }
-        i2c.write(acc_addr, dbf, 2);
+        _i2c.write(acc_addr, dbf, 2);
     }
     /////////////// Magnetometer Configuration /////////////////
     // Check mag chip is available of not
     // step 3/last
     if (mag_addr != BMC050_MAG_NOT_USED_ADDR) {
         dbf[0] = BMC050_M_WHO_AM_I; 
-        i2c.write(mag_addr, dbf, 1); 
-        i2c.read(mag_addr, dbf, 1);
+        _i2c.write(mag_addr, dbf, 1); 
+        _i2c.read(mag_addr, dbf, 1);
         if (dbf[0] == I_AM_BMC050_MAG){ mag_ready = 1;
         } else {                        mag_ready = 0; }
         if ( mag_ready == 1){
             // set output data rate
             dbf[0] = BMC050_M_OPERATION;
             dbf[1] = mag_parameter->data_rate;
-            i2c.write(mag_addr, dbf, 2);
+            _i2c.write(mag_addr, dbf, 2);
         }
     }      
 }
@@ -134,8 +128,8 @@
     }
     // X,Y &Z
     dbf[0] = BMC050_A_OUT_X_L; 
-    i2c.write(acc_addr, dbf, 1, true); 
-    i2c.read(acc_addr, data, 6, false);
+    _i2c.write(acc_addr, dbf, 1, true); 
+    _i2c.read(acc_addr, data, 6, false);
     // change data type
     dt[0] = float((short(data[1] << 8 | data[0] & 0xc0))>> 6) * GRAVITY / fs_factor_acc;
     dt[1] = float((short(data[3] << 8 | data[2] & 0xc0))>> 6) * GRAVITY / fs_factor_acc;
@@ -154,8 +148,8 @@
     }
     // X,Y &Z
     dbf[0] = BMC050_A_OUT_X_L; 
-    i2c.write(acc_addr, dbf, 1, true); 
-    i2c.read(acc_addr, data, 6, false);
+    _i2c.write(acc_addr, dbf, 1, true); 
+    _i2c.read(acc_addr, data, 6, false);
     // change data type
     dt[0] = float((short(data[1] << 8 | data[0] & 0xc0))>> 6) / fs_factor_acc;
     dt[1] = float((short(data[3] << 8 | data[2] & 0xc0))>> 6) / fs_factor_acc;
@@ -172,8 +166,8 @@
     }
     // X,Y &Z
     dbf[0] = BMC050_M_OUT_X_L; 
-    i2c.write(mag_addr, dbf, 1, true); 
-    i2c.read(mag_addr, data, 6, false);
+    _i2c.write(mag_addr, dbf, 1, true); 
+    _i2c.read(mag_addr, data, 6, false);
     // change data type
     dt[0] = float((short(data[1] << 8 | data[0] & 0xf8))>> 3);
     dt[1] = float((short(data[3] << 8 | data[2] & 0xf8))>> 3);
@@ -183,24 +177,24 @@
 /////////////// Accelerometer ///////////////////
 float BMC050::read_temp() {
     dbf[0] = BMC050_A_OUT_TEMP; 
-    i2c.write(acc_addr, dbf, 1); 
-    i2c.read(acc_addr, dbf, 1);
-    return ((float)dbf[0] * 0.5 + 24.0);
+    _i2c.write(acc_addr, dbf, 1); 
+    _i2c.read(acc_addr, dbf, 1);
+    return ((float)dbf[0] * 0.5f + 24.0f);
 }
 
 /////////////// Accelerometer ///////////////////
 uint8_t BMC050::read_id_acc() {
     dbf[0] = BMC050_A_WHO_AM_I; 
-    i2c.write(acc_addr, dbf, 1); 
-    i2c.read(acc_addr, dbf, 1);
+    _i2c.write(acc_addr, dbf, 1); 
+    _i2c.read(acc_addr, dbf, 1);
     return (uint8_t)dbf[0];
 }
 
 /////////////// Magnetometer ////////////////////
 uint8_t BMC050::read_id_mag() {
     dbf[0] = BMC050_M_WHO_AM_I; 
-    i2c.write(mag_addr, dbf, 1); 
-    i2c.read(mag_addr, dbf, 1);
+    _i2c.write(mag_addr, dbf, 1); 
+    _i2c.read(mag_addr, dbf, 1);
     return (uint8_t)dbf[0];
 }
 
@@ -208,8 +202,8 @@
 uint8_t BMC050::data_ready_acc() {
     if (acc_ready == 1){
         dbf[0] = BMC050_A_OUT_X_L; 
-        i2c.write(acc_addr, dbf, 1); 
-        i2c.read(acc_addr, dbf, 1);
+        _i2c.write(acc_addr, dbf, 1); 
+        _i2c.read(acc_addr, dbf, 1);
         if (!(dbf[0] & 0x01)){
             return 0;
         }
@@ -221,8 +215,8 @@
 uint8_t BMC050::data_ready_mag() {
     if (mag_ready == 1){
         dbf[0] = BMC050_M_HALL_L; 
-        i2c.write(mag_addr, dbf, 1); 
-        i2c.read(mag_addr, dbf, 1);
+        _i2c.write(mag_addr, dbf, 1); 
+        _i2c.read(mag_addr, dbf, 1);
         if (!(dbf[0] & 0x01)){
             return 0;
         }
@@ -234,8 +228,8 @@
 uint8_t BMC050::read_reg_acc(uint8_t addr) {
     if (acc_ready == 1){
         dbf[0] = addr; 
-        i2c.write(acc_addr, dbf, 1); 
-        i2c.read(acc_addr, dbf, 1);
+        _i2c.write(acc_addr, dbf, 1); 
+        _i2c.read(acc_addr, dbf, 1);
     } else {
         dbf[0] = 0xff;
     }
@@ -246,8 +240,8 @@
 uint8_t BMC050::read_reg_mag(uint8_t addr) {
     if (mag_ready == 1){
         dbf[0] = addr; 
-        i2c.write(mag_addr, dbf, 1); 
-        i2c.read(mag_addr, dbf, 1);
+        _i2c.write(mag_addr, dbf, 1); 
+        _i2c.read(mag_addr, dbf, 1);
     } else {
         dbf[0] = 0xff;
     }
@@ -259,7 +253,7 @@
     if (acc_ready == 1){
         dbf[0] = addr;
         dbf[1] = data; 
-        i2c.write(acc_addr, dbf, 2); 
+        _i2c.write(acc_addr, dbf, 2); 
     }
 }
 
@@ -268,11 +262,12 @@
     if (mag_ready == 1){
         dbf[0] = addr;
         dbf[1] = data; 
-        i2c.write(mag_addr, dbf, 2); 
+        _i2c.write(mag_addr, dbf, 2); 
     }
 }
 
 /////////////// Common //////////////////////////
 void BMC050::frequency(int hz) {
-    i2c.frequency(hz);
+    _i2c.frequency(hz);
 }
+
diff -r 93141eb80862 -r 24aa4d5fa7de BMC050.h
--- a/BMC050.h	Wed Jun 01 12:30:38 2016 +0000
+++ b/BMC050.h	Wed Aug 23 09:22:29 2017 +0000
@@ -3,17 +3,11 @@
  *  BMC050 COMPASS 6 AXIS, made by Bosch Sensortec
  *      http://jp.bosch-sensortec.com/content/language1/html/5033.htm
  *
- * Copyright (c) 2014, 2016 Kenji Arai / JH1PJL
+ * Copyright (c) 2014,'16,'17 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
- *      Created: July      19th, 2014 
- *      Revised: June       1st, 2016
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
- * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *      Created: July       19th, 2014 
+ *      Revised: August     23rd, 2017
  */
 
 #ifndef BMC050_H
@@ -284,7 +278,8 @@
     void initialize(const BMC050ACC_TypeDef *acc_parameter,
                     const BMC050MAG_TypeDef *mag_parameter);
 
-    I2C i2c;
+    I2C *_i2c_p;
+    I2C &_i2c;
   
 private:
     float   fs_factor_acc;  // full scale factor
@@ -298,3 +293,4 @@
 };
 
 #endif      // BMC050_H
+