a

Dependents:   Skipper_operation gy-91_tset Autoflight2022_913

Fork of BMP280 by Edwin Cho

Revision:
8:2dc7ede4ac55
Parent:
7:c72b726c7dc9
Child:
9:315c0737f4bc
--- a/BMP280.h	Tue Apr 19 02:03:35 2016 +0000
+++ b/BMP280.h	Fri Sep 07 08:28:00 2018 +0000
@@ -17,9 +17,10 @@
 
 #include "mbed.h"
 
+//#define DEFAULT_SLAVE_ADDRESS (0x77)
+#define DEFAULT_SLAVE_ADDRESS (0x76)
+
 //#define _DEBUG
-#define DEFAULT_SLAVE_ADDRESS (0x77 << 1)
-
 #ifdef _DEBUG
 extern Serial pc;
 #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
@@ -27,7 +28,7 @@
 #define DEBUG_PRINT(...)
 #endif
 
- 
+
 /** BME280 class
  *
  *  BME280: A library to correct environmental data using Boshe BME280 device
@@ -39,6 +40,44 @@
 class BMP280
 {
 public:
+    enum OptionPowerMode{
+        SLEEP = 0,
+        FORCED,
+        NOMAL = 3
+    };
+    enum OptionOverSampling{
+        OS_SKIPPED = 0,
+        OS_X1,
+        OS_X2,
+        OS_X4,
+        OS_X8,
+        OS_X16
+    };
+    enum OptionPressFilter{
+        PF_OFF = 0,
+        PF_X2,
+        PF_X4,
+        PF_X8,
+        PF_X16
+    };
+    enum OptionStandByTime{
+        SBT_0_5MS = 0,
+        SBT_62_5MS,
+        SBT_125MS,
+        SBT_250MS,
+        SBT_500MS,
+        SBT_1000MS,
+        SBT_2000MS,
+        SBT_4000MS
+    };
+    enum FilterSelection{
+        HANDHELD_DEVICE_LOW_POWER,
+        HANDHELD_DEVICE_DYNAMIC,
+        WETHER_MONITORING,
+        FLOOR_CHANGE_DETECTION,
+        DROP_DETECTION,
+        INDOOR_NAVIGATION        
+    };
 
     /** Create a BME280 instance
      *  which is connected to specified I2C pins with specified address
@@ -67,6 +106,13 @@
      *
      */
     void initialize(void);
+    void initialize(FilterSelection filter);
+    void initialize(OptionPowerMode opPM,
+                    OptionOverSampling opPOS,
+                    OptionOverSampling opTOS,
+                    OptionPressFilter opPF,
+                    OptionStandByTime opSBT,
+                    float putPutRate);
 
     /** Read the current temperature value (degree Celsius) from BME280 sensor
      *
@@ -76,13 +122,34 @@
     /** Read the current pressure value (hectopascal)from BME280 sensor
      *
      */
-    float getPressure(void);
+    float getPressure(bool skipMeasureTemp = false);
 
     /** Read the current humidity value (humidity %) from BME280 sensor
      *
      */
   //  float getHumidity(void);
 
+    char getID();
+
+    float getSampleRate();
+
+    float getCycle_s();
+    float getCycle_ms();
+    float getCycle_us();
+
+    void setPowerMode(OptionPowerMode op);
+    void setPressOverSampling(OptionOverSampling op);
+    void setTempOverSampling(OptionOverSampling op);
+    void setPressFilter(OptionPressFilter op);
+    void setStandByTime(OptionStandByTime op);
+
+    void selectFilter(FilterSelection filter);
+
+    void enableSPI3WriteMode(bool enable);
+    void resetSettings();
+    bool whoAmI();
+
+
 private:
 
     I2C         *i2c_p;
@@ -96,6 +163,67 @@
     int16_t     dig_H2, dig_H4, dig_H5, dig_H6;
     int32_t     t_fine;
 
+    char pressOverSampling; //0,1,2,4,8,16
+    char tempOverSampling; //0,1,2,4,8,16
+    char powerMode; //sleep,forced,nomal
+    char standByTime; //0.5,62.5,125,250,500,1000,2000,4000 [ms]
+    char pressFilter; //off,2,4,8,16
+    bool SPI3WriteMode; //off,on
+    float sampleTime;
+    float sampleRate;
+
+    void reset();
+    char ctrl_meas();
+    char config();
+
+    void setSetters(OptionPowerMode opPM,
+                    OptionOverSampling opPOS,
+                    OptionOverSampling opTOS,
+                    OptionPressFilter opPF,
+                    OptionStandByTime opSBT,
+                    float sampleRate);
+    void setOutputDataRate(float Hz);
 };
 
+inline float BMP280::getSampleRate(){return sampleRate;}
+
+inline float BMP280::getCycle_s(){return sampleTime*0.001;}
+inline float BMP280::getCycle_ms(){return sampleTime;}
+inline float BMP280::getCycle_us(){return sampleTime*1000;}
+
+inline void BMP280::setPressOverSampling(OptionOverSampling op){pressOverSampling = static_cast<char>(op);}
+inline void BMP280::setTempOverSampling(OptionOverSampling op){tempOverSampling = static_cast<char>(op);}
+inline void BMP280::setPowerMode(OptionPowerMode op){powerMode = static_cast<char>(op);}
+inline void BMP280::setStandByTime(OptionStandByTime op){standByTime = static_cast<char>(op);}
+inline void BMP280::setPressFilter(OptionPressFilter op){pressFilter = static_cast<char>(op);}
+inline void BMP280::enableSPI3WriteMode(bool enable){SPI3WriteMode = enable;}
+
+inline char BMP280::ctrl_meas()
+{    
+    return ((tempOverSampling << 5) | (pressOverSampling << 2) | powerMode);
+}
+
+inline char BMP280::config()
+{
+    return ((standByTime << 5) | (pressFilter << 2) | (static_cast<char>(SPI3WriteMode)));
+}
+
+
+inline void BMP280::setSetters(OptionPowerMode opPM, OptionOverSampling opPOS, OptionOverSampling opTOS, 
+                               OptionPressFilter opPF, OptionStandByTime opSBT, float outputRate)
+{
+    setPowerMode(opPM);
+    setPressOverSampling(opPOS);
+    setTempOverSampling(opTOS);
+    setPressFilter(opPF);
+    setStandByTime(opSBT);
+    setOutputDataRate(outputRate);
+}
+
+inline void BMP280::setOutputDataRate(float Hz){
+    sampleRate = Hz;
+    sampleTime = 1000.0 / Hz;
+}
+
+
 #endif // MBED_BME280_H