upm  0.8.0
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bme280.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.hpp>
28 #include <mraa/spi.hpp>
29 #include <mraa/gpio.hpp>
30 
31 #include "upm/iHumiditySensor.hpp"
32 
33 #include "bmp280.hpp"
34 
35 #define BME280_DEFAULT_I2C_BUS 0
36 #define BME280_DEFAULT_SPI_BUS 0
37 #define BME280_DEFAULT_ADDR 0x77
38 #define BME280_DEFAULT_CHIPID 0x60
39 
40 namespace upm {
41 
66  class BME280 : public BMP280, public IHumiditySensor {
67  public:
68 
72  typedef enum : uint8_t {
73  // Do not write into reserved bits.
74 
75  // We only specify those registers specific to the BME280. The
76  // rest of them can be found in the BMP280 header file.
77 
78  // read-only factory calibration data for humidity
79 
80  REG_CALIB_DIG_H1 = 0xa1,
81 
82  REG_CALIB_DIG_H2_LSB = 0xe1,
83  REG_CALIB_DIG_H2_MSB = 0xe2,
84 
85  REG_CALIB_DIG_H3 = 0xe3,
86 
87  REG_CALIB_DIG_H4_0 = 0xe4, // bits 8 -> 11:4
88  REG_CALIB_DIG_H4_1 = 0xe5, // bits 3:0 -> 3:0
89 
90  REG_CALIB_DIG_H5_0 = 0xe5, // bits 7:4 -> 3:0
91  REG_CALIB_DIG_H5_1 = 0xe6, // bits 8 -> 11:4
92 
93  REG_CALIB_DIG_H6 = 0xe7,
94 
95  REG_CTRL_HUM = 0xf2,
96 
97  REG_HUMIDITY_MSB = 0xfd,
98  REG_HUMIDITY_LSB = 0xfe
99  } BME280_REGS_T;
100 
104  typedef enum {
105  CTRL_HUM_OSRS_H0 = 0x01,
106  CTRL_HUM_OSRS_H1 = 0x02,
107  CTRL_HUM_OSRS_H2 = 0x04,
108  _CTRL_HUM_OSRS_H_MASK = 3,
109  _CTRL_HUM_OSRS_H_SHIFT = 0
110 
111  // 0x08-0x80 reserved
112  } CTRL_HUM_T;
113 
117  typedef enum {
118  OSRS_H_SKIPPED = 0,
119  OSRS_H_OVERSAMPLING_1 = 1, // x1
120  OSRS_H_OVERSAMPLING_2 = 2, // x2
121  OSRS_H_OVERSAMPLING_4 = 3,
122  OSRS_H_OVERSAMPLING_8 = 4,
123  OSRS_H_OVERSAMPLING_16 = 5
124  } OSRS_H_T;
125 
126 
143  BME280(int bus=BME280_DEFAULT_I2C_BUS, int addr=BME280_DEFAULT_ADDR,
144  int cs=-1, uint8_t theChipID=BME280_DEFAULT_CHIPID);
145 
149  virtual ~BME280();
150 
154  virtual void update();
155 
162  float getHumidity();
163 
172  virtual void setUsageMode(USAGE_MODE_T mode);
173 
183 
184  // Interface support
185  const char *getModuleName()
186  {
187  return "BME280";
188  };
189 
190  int getHumidityRelative()
191  {
192  return int(getHumidity());
193  };
194 
195  protected:
196  // relative humidity
197  float m_humidity;
198 
199  // read the calibration data
200  virtual void readCalibrationData();
201 
202  private:
203  // calibration data humidity
204  uint8_t m_dig_H1;
205  int16_t m_dig_H2;
206  uint8_t m_dig_H3;
207  int16_t m_dig_H4;
208  int16_t m_dig_H5;
209  int8_t m_dig_H6;
210 
211  // Bosch supplied conversion/compensation functions from the
212  // datasheet.
213  uint32_t bme280_compensate_H_int32(int32_t adc_H);
214  };
215 }
const char * getModuleName()
Definition: bme280.hpp:185
CTRL_HUM_T
Definition: bme280.hpp:104
virtual void setUsageMode(USAGE_MODE_T mode)
Definition: bme280.cxx:144
float getHumidity()
Definition: bme280.cxx:128
API for the BME280 Digital Humidity, Pressure, and Temperature Sensor.
Definition: bme280.hpp:66
virtual void update()
Definition: bme280.cxx:64
BME280_REGS_T
Definition: bme280.hpp:72
BME280(int bus=BME280_DEFAULT_I2C_BUS, int addr=BME280_DEFAULT_ADDR, int cs=-1, uint8_t theChipID=BME280_DEFAULT_CHIPID)
Definition: bme280.cxx:37
OSRS_H_T
Definition: bme280.hpp:117
USAGE_MODE_T
Definition: bmp280.hpp:250
virtual ~BME280()
Definition: bme280.cxx:60
void setOversampleRateHumidity(OSRS_H_T rate)
Definition: bme280.cxx:133
API for the BMP280 Digital Pressure Sensor.
Definition: bmp280.hpp:75
Interface for Humidity Sensors.
Definition: iHumiditySensor.hpp:34