BM1383AGLV sensor library

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

Files at this revision

API Documentation at this revision

Comitter:
MACRUM
Date:
Fri Nov 20 14:55:11 2020 +0000
Parent:
0:d505f3c67a62
Commit message:
Add ready-status check

Changed in this revision

BM1383AGLV.cpp Show annotated file Show diff for this revision Revisions of this file
BM1383AGLV.h Show annotated file Show diff for this revision Revisions of this file
--- 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);
 }
--- a/BM1383AGLV.h	Sat Feb 03 14:26:39 2018 +0000
+++ b/BM1383AGLV.h	Fri Nov 20 14:55:11 2020 +0000
@@ -47,6 +47,8 @@
 #define BM1383AGLV_TEMPERATURE_MSB     (0x1D)
 #define BM1383AGLV_TEMPERATURE_LSB     (0x1E)
 
+#define BM1383AGLV_WAIT_READY_STATUS    0
+
 #ifdef _DEBUG
 extern Serial pc;
 #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
@@ -119,7 +121,8 @@
     float getTemperature();
 
 private:
-    I2C m_i2c;
+    I2C *i2c_p;
+    I2C &i2c;
     int m_addr;
     uint8_t m_buf[3];
     void readRegs(int addr, uint8_t * data, int len);