Jens Schneider / BME280
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BME280.h Source File

BME280.h

00001 /*****************************************************************************
00002  *                                                                           *
00003  * BOSCH BME208 Temperature Pressure Humidity Sensor Driver / I2C            *
00004  *                                                                           *
00005  * (c) 2015 Jens Schneider                                                   *
00006  * mailto:jens.schneider@kaust.edu.sa                                        *
00007  * Visual Computing Center (VCC)                                             *
00008  * King Abdullah University of Science and Technology (KAUST)                *
00009  * 4700 Thuwal, Kingdom of Saudi Arabia                                      *
00010  *                                                                           *
00011  * PERMISSION GRANTED TO USE IN NON-COMMERCIAL AND EDUCATIONAL PROJECTS      *
00012  * NO RESPONSIBILITY ASSUMED FOR DAMAGE OR LOSS OF ANY HARDWARE OR SOFTWARE  *
00013  *                                                                           *
00014  * version 0.1                                                               *
00015  * last change 07.Nov.2015                                                   *
00016  *                                                                           *
00017  *****************************************************************************/
00018 #ifndef __BME280_H__
00019 #define __BME280_H__
00020 
00021 #include"mbed.h"
00022 #include<string>
00023 
00024 class BME280 {
00025 public:
00026     /// Oversampling settings for Pressure, Temperature, Humidity
00027     enum sampling_t {
00028         SAMPLING_0x         = 0x00,                 ///< skipped, output set to 0x80000
00029         SAMPLING_1x         = 0x01,                 ///< oversampling x1
00030         SAMPLING_2x         = 0x02,                 ///< oversampling x2
00031         SAMPLING_4x         = 0x03,                 ///< oversampling x4
00032         SAMPLING_8x         = 0x04,                 ///< oversampling x8
00033         SAMPLING_16x        = 0x05                  ///< oversampling x16
00034     };
00035     /// Standby times for auto mode, ignored in forced mode
00036     enum standby_t {
00037         STANDBY_0_5         = 0x0,                  ///<    0.5ms standby between autonomous measurements
00038         STANDBY_62_5        = 0x1,                  ///<   62.5ms standby time
00039         STANDBY_125         = 0x2,                  ///<  125.0ms standby time
00040         STANDBY_250         = 0x3,                  ///<  250.0ms standby time
00041         STANDBY_500         = 0x4,                  ///<  500.0ms standby time
00042         STANDBY_1000        = 0x5,                  ///< 1000.0ms standby time
00043         STANDBY_10          = 0x6,                  ///<   10.0ms standby time
00044         STANDBY_20          = 0x7                   ///<   20.0ms standby time
00045     };
00046     /// Filter settings
00047     enum filter_t {
00048         FILTER_OFF          = 0x0,                  ///< no IIR filtering, immediately reaches 75% of step response
00049         FILTER_2            = 0x1,                  ///< 75% of step response reached after 2 samples
00050         FILTER_4            = 0x2,                  ///< 75% of step response reached after 5 samples
00051         FILTER_8            = 0x3,                  ///< 75% of step response reached after 11 samples
00052         FILTER_16           = 0x4                   ///< 75% of step response reached after 22 samples
00053     };
00054     /// Sensor mode
00055     enum mode_t {
00056         MODE_SLEEP          = 0x00,                 ///< sleep mode, can be achieved by calling reset()
00057         MODE_FORCED         = 0x01,                 ///< forced mode, conversion only upon request
00058         MODE_AUTO           = 0x03                  ///< continuous measurement mode
00059     };
00060     /// Registers
00061     static const uint8_t    REG_HUM_LSB;            ///< Humidity, lsb register
00062     static const uint8_t    REG_HUM_MSB;            ///< Humidity, msb register
00063     static const uint8_t    REG_TEMP_XLSB;          ///< Temperature, xlsb register (4bit)
00064     static const uint8_t    REG_TEMP_LSB;           ///< Temperature, lsb register
00065     static const uint8_t    REG_TEMP_MSB;           ///< Temperature, msb register
00066     static const uint8_t    REG_PRESS_XLSB;         ///< Pressure, xlsb register (4bit)
00067     static const uint8_t    REG_PRESS_LSB;          ///< Pressure, lsb register
00068     static const uint8_t    REG_PRESS_MSB;          ///< Pressure, msb register
00069     static const uint8_t    REG_CONFIG;             ///< Config register
00070     static const uint8_t    REG_CTRL_MEAS;          ///< Control Measurement register
00071     static const uint8_t    REG_STATUS;             ///< Status register
00072     static const uint8_t    REG_CTRL_HUM;           ///< Control Humidity register
00073     static const uint8_t    REG_CALIB26_41_BASE;    ///< Base of higher calibration data registers
00074     static const uint8_t    REG_RESET;              ///< Soft Reset register
00075     static const uint8_t    REG_ID;                 ///< Chip ID register
00076     static const uint8_t    REG_CALIB00_25_BASE;    ///< Base of lower calibration data registers
00077     /// Values
00078     static const uint8_t    VAL_CALIB26_41_SIZE;    ///< Amount of higher calibration data
00079     static const uint8_t    VAL_CALIB00_25_SIZE;    ///< Amount of lower calibration data
00080     static const uint8_t    VAL_CHIP_ID;            ///< Chip ID
00081     static const uint8_t    VAL_RESET;              ///< Reset command
00082     static const uint8_t    STATUS_IDLE;            ///< Status value for Idle
00083     static const uint8_t    STATUS_MEASURING;       ///< Status value for Measurment in progress
00084     static const uint8_t    STATUS_UPDATING;        ///< Status value for Updating calibration registers from NVM
00085     static const uint8_t    STATUS_ERROR;           ///< Status value for Error
00086     
00087 protected:
00088 
00089     /// Calibration data structure
00090     struct calib_t {
00091         uint16_t    dig_T1;     
00092         int16_t     dig_T2;
00093         int16_t     dig_T3;
00094         uint16_t    dig_P1;
00095         int16_t     dig_P2;
00096         int16_t     dig_P3;
00097         int16_t     dig_P4;
00098         int16_t     dig_P5;
00099         int16_t     dig_P6;
00100         int16_t     dig_P7;
00101         int16_t     dig_P8;
00102         int16_t     dig_P9;
00103         uint8_t     dig_H1;
00104         int16_t     dig_H2;
00105         uint8_t     dig_H3;
00106         int16_t     dig_H4;
00107         int16_t     dig_H5;
00108         int8_t      dig_H6;
00109     } m_calib;
00110 
00111 public:
00112 
00113                         BME280(void);                               ///< Default constructor
00114                         ~BME280(void);                              ///< Destructor
00115     bool                init(I2C& i2c, uint8_t address=0x76);       ///< Initialize sensor and read out calibration
00116     bool                start(  sampling_t hum = SAMPLING_8x,       ///< Start measurement with provided configuration
00117                                 sampling_t temp = SAMPLING_8x, 
00118                                 sampling_t press = SAMPLING_8x,
00119                                 standby_t standby = STANDBY_500,
00120                                 filter_t filter = FILTER_8,
00121                                 mode_t mode = MODE_AUTO);
00122     bool                stop(void);                                 ///< Stop measurement and put sensor in sleep mode
00123     bool                done(void);                                 ///< "Detach" sensor from I2C bus and put in sleep mode
00124     bool                get(float& T, float& P, float& H);          ///< read float data: [T]=1C    [P]=1 hPa    [H]=%
00125     bool                get(int32_t& T,uint32_t& P,uint32_t& H);    ///< read int data:   [T]=0.01C [P]=1/256 Pa [H]=1/1024 %
00126     const bool&         is_ok(void) const;                          ///< true iff sensor initialized properly
00127     const std::string&  name(void) const;                           ///< returns sensor name
00128     
00129     /// LOW LEVEL API
00130     uint8_t             read_chip_id(void);                         ///< returns the chip id on success, 0x00 on failure
00131     bool                reset(void);                                ///< reset the sensor and put to sleep mode
00132     uint8_t             read_status(void);                          ///< read status, returns 0xFF on failure
00133     bool                write8(uint8_t reg,uint8_t val);            ///< write 8bit register, returns true on success
00134     bool                read8(uint8_t reg,uint8_t& val);            ///< read 8bit register, returns true on success
00135     const calib_t&      calib(void) const;                          ///< access to calibration data
00136     bool                burst_read(uint8_t* data, uint8_t nBytes,
00137                                    uint8_t reg);                    ///< burst read of data
00138     
00139     I2C*                m_pI2C;                                     ///< pointer to I2C bus
00140     uint8_t             m_address;                                  ///< slave address (with write flag)
00141     int32_t             m_fine_temp;                                ///< last temperature for thermo-compensation
00142     mode_t              m_mode;                                     ///< sensor mode
00143     bool                m_bOk;                                      ///< sensor initialized ok?
00144     
00145     bool                read_calibration(void);                     ///< read lower and higher part of calibration data
00146     bool                read_calibration_lo(void);                  ///< read lower part of calibration data
00147     bool                read_calibration_hi(void);                  ///< read higher part of calibration data
00148     int16_t             conv_s16(const uint8_t* data,               ///< convert 2 bytes of calibration data to int16
00149                                  int lo, int hi) const;
00150     uint16_t            conv_u16(const uint8_t* data,               ///< convert 2 bytes of calibration data to uint16
00151                                  int lo, int hi) const;
00152     bool                raw_data(int32_t& T,int32_t& P,int32_t& H); ///< read the raw sensor data
00153     
00154     /// converts a raw temperature measurement into degree C, resolution 0.01C
00155     /// updates m_fine_temp for later pressure and humidity compensation
00156     int32_t             compensate_T(int32_t raw_T);
00157     
00158     /// converts a raw pressure measurement into PA, resolution 1/256 Pa / 1/256000 hPa
00159     /// uses m_fine_temp for correction
00160     uint32_t            compensate_P(int32_t raw_P);
00161     
00162     // converts a raw humidity measurement into %rel.H., resolution 1/1024 % rh
00163     // uses m_fine_temp for correction
00164     uint32_t            compensate_H(int32_t raw_H);
00165     static const std::string m_name;
00166 private:
00167     // No copy constructor for you!
00168     BME280(const BME280& other);
00169 };
00170 
00171 #endif