BMP180 is a digital barometric pressure sensor made by Bosch Sensortec (I2C Interface)

Dependents:   LPC1114_data_logger ProjectIOT Wether_Meter LPC1114_barometer_with_data_logging

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Wed Aug 23 09:23:32 2017 +0000
Parent:
3:20e0c6b19c24
Commit message:
countermeasure for NonCopyable

Changed in this revision

BMP180.cpp Show annotated file Show diff for this revision Revisions of this file
BMP180.h Show annotated file Show diff for this revision Revisions of this file
diff -r 20e0c6b19c24 -r 37fed112956c BMP180.cpp
--- a/BMP180.cpp	Sun Jun 29 06:51:05 2014 +0000
+++ b/BMP180.cpp	Wed Aug 23 09:23:32 2017 +0000
@@ -2,18 +2,12 @@
  * mbed library program 
  *  Control BMP180(Bosch) Pressure Sensor
  *
- * Copyright (c) 2014 Kenji Arai / JH1PJL
+ * Copyright (c) 2013,'14,'17 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
- *      Created: August    14th, 2013   for STM32L152 
- *      Changed: May       21st, 2014   mbed LPC1114
- *      Revised: June      22nd, 2014
- *
- * 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: August     14th, 2013   for STM32L152 
+ *      Changed: May        21st, 2014   mbed LPC1114
+ *      Revised: August     23rd, 2017
  */
 /*
  *---------------- REFERENCE ----------------------------------------------------------------------
@@ -60,11 +54,15 @@
 #define B_PRES_MEASURE_OSS3     0xf4    // pressure /over sampling #3
 #define B_RESET_CMD             0xb6    // Reset chip command
 
-BMP180::BMP180 (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) {
+BMP180::BMP180 (PinName p_sda, PinName p_scl)
+ : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p)
+{
     init();
 }
 
-BMP180::BMP180 (I2C& p_i2c) : i2c(p_i2c) { 
+BMP180::BMP180 (I2C& p_i2c)
+ : _i2c(p_i2c)
+{ 
     init();
 }
 
@@ -95,7 +93,7 @@
     // start temprerature measurment
     baro_dt[0] = BARO_CTRL_MEAS_REG;
     baro_dt[1] = B_TEMP_MEASURE; 
-    i2c_write_n_bytes(BMP180_addr, baro_dt, 2);    
+    _i2c_write_n_bytes(BMP180_addr, baro_dt, 2);    
     wait(0.3);       // wait for convertion
 #if 0
     printf("type:0x%x\r\nac1:0x%x,ac2:0x%x,ac3:0x%x,ac4:0x%x,ac5:0x%x,ac6:0x%x\n\r",
@@ -104,8 +102,8 @@
 #endif
     // read temp.
     baro_dt[0] = BARO_ADC_OUT_MSB_REG;    
-    i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);        // not 3 but 2 (June 14th/Debug)
+    _i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);        // not 3 but 2 (June 14th/Debug)
     raw_temp = baro_dt[0] << 8 | baro_dt[1];
 #if 0
     printf("temp_raw:0x%x\n\r",raw_temp);
@@ -113,12 +111,12 @@
     // start pressure measurement
     baro_dt[0] = BARO_CTRL_MEAS_REG;
     baro_dt[1] = B_PRES_MEASURE_OSS3; 
-    i2c_write_n_bytes(BMP180_addr, baro_dt, 2);
+    _i2c_write_n_bytes(BMP180_addr, baro_dt, 2);
     wait(0.3);       // wait for convertion
     // read pressure
     baro_dt[0] = BARO_ADC_OUT_MSB_REG;    
-    i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
-    i2c_read_n_bytes(BMP180_addr,baro_dt,3);
+    _i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,3);
     raw_pres = ( baro_dt[0] << 16 | baro_dt[1] << 8 | baro_dt[2] ) >> (8 - 3 );
 #if 0
     printf("pres_raw:0x%x\n\r",raw_pres);
@@ -130,7 +128,7 @@
         dt_x2 = ( (int32_t)eep_mc << 11 ) / ( dt_x1 + (int32_t)eep_md );
         dt_b5 = dt_x1 + dt_x2;
     }
-    temperature = (float)( ( dt_b5 + 8 ) >> 4 )/10.0;    // temperature in 0.1 degC    
+    temperature = (float)( ( dt_b5 + 8 ) >> 4 )/10.0f;    // temperature in 0.1 degC    
     //      Pressure
     dt_b6 = dt_b5 - 4000;
     dt_x1 = ( dt_b6 * dt_b6 ) >> 12;
@@ -158,49 +156,50 @@
     dt_x2 *= -1;
     // pressure in Pa
     dx += ( dt_x1 + dt_x2 + CONST_MI ) >> 4;
-    pressure = (float)dx / 100.0;
+    pressure = (float)dx / 100.0f;
 }
 
 void BMP180::init () {
     BMP180_addr = BMP180ADDR;
     // parameters AC1-AC6
     baro_dt[0] = BARO_PROM_ADDR;
-    i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_ac1 =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_ac2 =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_ac3 =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_ac4 =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_ac5 =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_ac6 =  (baro_dt[0] << 8)  | baro_dt[1];
     // parameters B1,B2
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_b1 =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_b2 =  (baro_dt[0] << 8)  | baro_dt[1];
     // parameters MB,MC,MD
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_mb =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_mc =  (baro_dt[0] << 8)  | baro_dt[1];
-    i2c_read_n_bytes(BMP180_addr,baro_dt,2);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,2);
     eep_md =  (baro_dt[0] << 8)  | baro_dt[1];
     // Read ID
     baro_dt[0] = BARO_CHIP_ID_REG;
-    i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
-    i2c_read_n_bytes(BMP180_addr,baro_dt,1);
+    _i2c_write_n_bytes(BMP180_addr, baro_dt, 1);
+    _i2c_read_n_bytes(BMP180_addr,baro_dt,1);
     id_number = baro_dt[0];
 }
 
-void BMP180::i2c_read_n_bytes (int addr, char* dt, int n) {
-    i2c.read(addr, dt, n);
+void BMP180::_i2c_read_n_bytes (int addr, char* dt, int n) {
+    _i2c.read(addr, dt, n);
 }
 
-void BMP180::i2c_write_n_bytes (int addr, char* dt, int n) {
-    i2c.write(addr, dt, n);
+void BMP180::_i2c_write_n_bytes (int addr, char* dt, int n) {
+    _i2c.write(addr, dt, n);
 }
+
diff -r 20e0c6b19c24 -r 37fed112956c BMP180.h
--- a/BMP180.h	Sun Jun 29 06:51:05 2014 +0000
+++ b/BMP180.h	Wed Aug 23 09:23:32 2017 +0000
@@ -2,18 +2,12 @@
  * mbed library program 
  *  Control BMP180(Bosch) Pressure Sensor
  *
- * Copyright (c) 2014 Kenji Arai / JH1PJL
+ * Copyright (c) 2013,'14,'17 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
- *      Created: August    14th, 2013   for STM32L152 
- *      Changed: May       21st, 2014   mbed LPC1114
- *      Revised: June      22nd, 2014
- *
- * 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: August     14th, 2013   for STM32L152 
+ *      Changed: May        21st, 2014   mbed LPC1114
+ *      Revised: August     23rd, 2017
  */
 
 #ifndef BMP180_H
@@ -92,10 +86,12 @@
 
 protected:
     void init(void);
-    void i2c_read_n_bytes(int, char*, int);
-    void i2c_write_n_bytes(int, char*, int);
+    void _i2c_read_n_bytes(int, char*, int);
+    void _i2c_write_n_bytes(int, char*, int);
 
-    I2C i2c;
+    I2C *_i2c_p;
+    I2C &_i2c;
+
     float temperature;
     float pressure;
     uint8_t id_number;
@@ -113,3 +109,4 @@
 };
 
 #endif // BMP180_H
+