BM1383AGLV sensor library

Dependents:   simple-sensor-client rohm-SensorShield-example

Revision:
1:997a1eb76a4f
Parent:
0:d505f3c67a62
--- a/BM1383AGLV.cpp	Sat Feb 03 14:26:39 2018 +0000
+++ b/BM1383AGLV.cpp	Fri Nov 20 14:55:11 2020 +0000
@@ -30,18 +30,28 @@
 #include "mbed.h"
 #include "BM1383AGLV.h"
 
-BM1383AGLV::BM1383AGLV(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+BM1383AGLV::BM1383AGLV(PinName sda, PinName scl, int addr)
+    :
+    i2c_p(new I2C(sda, scl)), 
+    i2c(*i2c_p),
+    m_addr(addr)
 {
     initialize();
 }
 
-BM1383AGLV::BM1383AGLV(I2C &i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr)
+BM1383AGLV::BM1383AGLV(I2C &i2c_obj, int addr)
+    :
+    i2c_p(NULL), 
+    i2c(i2c_obj),
+    m_addr(addr)
 {
     initialize();
 }
 
 BM1383AGLV::~BM1383AGLV()
 {
+    if (NULL != i2c_p)
+        delete  i2c_p;
 }
 
 void BM1383AGLV::initialize()
@@ -75,6 +85,15 @@
 {
     uint32_t rawPressure;
 
+#if BM1383AGLV_WAIT_READY_STATUS
+    uint8_t ready_status = 0;
+    do {
+        wait_ms(6);
+        readRegs(BM1383AGLV_STATUS, &ready_status, 1);
+    } while(ready_status == 0);
+#else
+    wait_ms(6);
+#endif
     readRegs(BM1383AGLV_PRESSURE_MSB, m_buf, 3);
     rawPressure = (((uint32_t)m_buf[0] << 16) | ((uint32_t)m_buf[1] << 8) | m_buf[2]&0xFC) >> 2;
     return (float)rawPressure / (2048);
@@ -84,6 +103,15 @@
 {
     int32_t rawTemerature;
     
+#if BM1383AGLV_WAIT_READY_STATUS
+    uint8_t ready_status = 0;
+    do {
+        wait_ms(6);
+        readRegs(BM1383AGLV_STATUS, &ready_status, 1);
+    } while(ready_status == 0);
+#else
+    wait_ms(6);
+#endif
     readRegs(BM1383AGLV_TEMPERATURE_MSB, m_buf, 2);
     rawTemerature = ((int32_t)m_buf[0] << 8) | (m_buf[1]);
     return (float)rawTemerature / 32;
@@ -92,11 +120,11 @@
 void BM1383AGLV::readRegs(int addr, uint8_t * data, int len)
 {
     char t[1] = {addr};
-    m_i2c.write(m_addr, t, 1, true);
-    m_i2c.read(m_addr, (char *)data, len);
+    i2c.write(m_addr, t, 1, true);
+    i2c.read(m_addr, (char *)data, len);
 }
 
 void BM1383AGLV::writeRegs(uint8_t * data, int len)
 {
-    m_i2c.write(m_addr, (char *)data, len);
+    i2c.write(m_addr, (char *)data, len);
 }