RUI3 (RAK Unified Interface 3) - RAK4631
rak1906.h
Go to the documentation of this file.
1 #ifndef __RAK1906_H__
2 #define __RAK1906_H_
3 
4 #include "Wire.h"
5 #include <math.h>
6 
7 
8 #ifndef _BV
9 #define _BV(bit) (1 << (bit)) // /< This macro isn't pre-defined on all
10  // platforms
11 #endif
12 
13 #define RAK1906_ADDRESS 0x76
14 #define CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb) // /<
15  // combine
16  // msb
17  // &
18  // lsb
19  // bytes
20 
21 typedef enum {
22  RAK1906_STATUS_REGISTER = 0x1D, // /< Device status register
23  RAK1906_GAS_HEATER_REGISTER0 = 0x5A, // /< Heater Register 0
24  // address
25  RAK1906_GAS_DURATION_REGISTER0 = 0x64, // /< Heater Register 0
26  // address
27  RAK1906_CONTROL_GAS_REGISTER1 = 0x70, // /< Gas control register
28  // on/off
29  RAK1906_CONTROL_GAS_REGISTER2 = 0x71, // /< Gas control register
30  // settings
31  RAK1906_CONTROL_HUMIDITY_REGISTER = 0x72, // /< Humidity control
32  // register
33  RAK1906_SPI_REGISTER = 0x73, // /< Status register for SPI
34  // memory
35  RAK1906_CONTROL_MEASURE_REGISTER = 0x74, // /< Temp, Pressure
36  // control register
37  RAK1906_CONFIG_REGISTER = 0x75, // /< Configuration register
38  RAK1906_CHIPID_REGISTER = 0xD0, // /< Chip-Id register
39  RAK1906_SOFTRESET_REGISTER = 0xE0, // /< Reset when 0xB6 is written
40  // here
41  RAK1906_CHIPID = 0x61, // /< Hard-coded value 0x61 for BME680
42  RAK1906_RESET_CODE = 0xB6, // /< Reset when this put in reset reg
43  RAK1906_MEASURING_BIT_POSITION = 5, // /< Bit position for measuring
44  // flag
45  RAK1906_HUMIDITY_MASK = 0xF8, // /< Mask is binary B11111000
46  RAK1906_PRESSURE_MASK = 0xE3, // /< Mask is binary B11100011
47  RAK1906_TEMPERATURE_MASK = 0x1F, // /< Mask is binary B00011111
49 
50 /***************************************************************************************************
51 ** Declare the constants used for calibration **
52 ***************************************************************************************************/
53 #define RAK1906_COEFF_SIZE1 25 // /< First array with coefficients
54 #define RAK1906_COEFF_SIZE2 16 // /< Second array with coefficients
55 #define RAK1906_COEFF_START_ADDRESS1 0x89 // /< start address for
56  // array 1
57 #define RAK1906_COEFF_START_ADDRESS2 0xE1 // /< start address for
58  // array 2
59 #define RAK1906_HUM_REG_SHIFT_VAL 4 // /< Ambient humidity shift value
60 #define RAK1906_BIT_H1_DATA_MSK 0x0F // /< Mask for humidity
61 #define RAK1906_T2_LSB_REG 1 // /< Register for temperature calibration
62 #define RAK1906_T2_MSB_REG 2 // /< Register for temperature calibration
63 #define RAK1906_T3_REG 3 // /< Register for temperature calibration
64 #define RAK1906_P1_LSB_REG 5 // /< Register for pressure calibration
65 #define RAK1906_P1_MSB_REG 6 // /< Register for pressure calibration
66 #define RAK1906_P2_LSB_REG 7 // /< Register for pressure calibration
67 #define RAK1906_P2_MSB_REG 8 // /< Register for pressure calibration
68 #define RAK1906_P3_REG 9 // /< Register for pressure calibration
69 #define RAK1906_P4_LSB_REG 11 // /< Register for pressure calibration
70 #define RAK1906_P4_MSB_REG 12 // /< Register for pressure calibration
71 #define RAK1906_P5_LSB_REG 13 // /< Register for pressure calibration
72 #define RAK1906_P5_MSB_REG 14 // /< Register for pressure calibration
73 #define RAK1906_P7_REG 15 // /< Register for pressure calibration
74 #define RAK1906_P6_REG 16 // /< Register for pressure calibration
75 #define RAK1906_P8_LSB_REG 19 // /< Register for pressure calibration
76 #define RAK1906_P8_MSB_REG 20 // /< Register for pressure calibration
77 #define RAK1906_P9_LSB_REG 21 // /< Register for pressure calibration
78 #define RAK1906_P9_MSB_REG 22 // /< Register for pressure calibration
79 #define RAK1906_P10_REG 23 // /< Register for pressure calibration
80 #define RAK1906_H2_MSB_REG 0 // /< Register for humidity calibration
81 #define RAK1906_H2_LSB_REG 1 // /< Register for humidity calibration
82 #define RAK1906_H1_LSB_REG 1 // /< Register for humidity calibration
83 #define RAK1906_H1_MSB_REG 2 // /< Register for humidity calibration
84 #define RAK1906_H3_REG 3 // /< Register for humidity calibration
85 #define RAK1906_H4_REG 4 // /< Register for humidity calibration
86 #define RAK1906_H5_REG 5 // /< Register for humidity calibration
87 #define RAK1906_H6_REG 6 // /< Register for humidity calibration
88 #define RAK1906_H7_REG 7 // /< Register for humidity calibration
89 #define RAK1906_T1_LSB_REG 8 // /< Register for gas calibration
90 #define RAK1906_T1_MSB_REG 9 // /< Register for gas calibration
91 #define RAK1906_GH2_LSB_REG 10 // /< Register for gas calibration
92 #define RAK1906_GH2_MSB_REG 11 // /< Register for gas calibration
93 #define RAK1906_GH1_REG 12 // /< Register for gas calibration
94 #define RAK1906_GH3_REG 13 // /< Register for gas calibration
95 #define RAK1906_ADDR_RES_HEAT_RANGE_ADDR 0x02 // /< Register for gas
96  // calibration
97 #define RAK1906_RHRANGE_MSK 0x30 // /< Register for gas calibration
98 #define RAK1906_ADDR_RES_HEAT_VAL_ADDR 0x00 // /< Register for gas
99  // calibration
100 #define RAK1906_ADDR_RANGE_SW_ERR_ADDR 0x04 // /< Register for gas
101  // calibration
102 #define RAK1906_RSERROR_MSK 0xF0 // /< Register for gas calibration
103 
104 typedef enum {
110 
123 };
124 
125 class rak1906 {
126  public:
127  rak1906();
128 
129 #ifdef SUPPORT_RAK1906
130 
133 #endif
134 
141  bool init(void);
142 
148  bool update(void);
149 
154  float temperature(void);
155 
160  float humidity(void);
161 
166  float pressure(void);
167 
172  float gas(void);
175  int32_t tmpTemperature,
176  tmpHumidity,
177  tmpPressure,
178  tmpGas;
179  private:
180  RAK1906_ErrorCode sendCommand(RAK1906_Commands cmd);
181  RAK1906_ErrorCode readData(uint16_t *);
182 
183 
184  uint8_t setOversampling(const uint8_t, const uint8_t);
185  void waitForReadings() const; // /< Wait for readings to
186  // finish
187  uint8_t setIIRFilter(const uint8_t iirFilterSetting = UINT8_MAX) const; // Set
188  // IIR
189  // Filter
190  uint8_t readByte(const uint8_t) const;
191  void getCalibration(); // /< Load calibration from
192  // registers
193  bool setGas(uint16_t GasTemp, uint16_t GasMillis) const; // Gas
194  // heating
195  // temperature
196  // and
197  // time
198  float altitude(const int32_t press, const float seaLevel =
199  1013.25);
200  bool measuring() const; // /< true if currently measuring
201  void triggerMeasurement() const; // /< trigger a
202  // measurement
203 
204 
205  uint8_t _H6,
206  _P10,
207  _res_heat_range;
208  int8_t _H3,
209  _H4,
210  _H5,
211  _H7,
212  _G1,
213  _G3,
214  _T3,
215  _P3,
216  _P6,
217  _P7,
218  _res_heat,
219  _rng_sw_err;
220  uint16_t _H1,
221  _H2,
222  _T1,
223  _P1;
224  int16_t _G2,
225  _T2,
226  _P2,
227  _P4,
228  _P5,
229  _P8,
230  _P9;
231  int32_t _tfine,
232  _Temperature,
233  _Pressure,
234  _Humidity,
235  _Gas;
236 
237  // getData
238  template <typename T>
239  uint8_t &getData(const uint8_t addr, T &value) const
240  {
241  uint8_t * bytePtr = (uint8_t *)&value; // Pointer to structure beginning
242  static uint8_t structSize = sizeof(T); // Number of bytes in structure
243  Wire.beginTransmission(RAK1906_ADDRESS); // Address the I2C device
244  Wire.write(addr); // Send register address to read
245  Wire.endTransmission(); // Close transmission
246  Wire.requestFrom(RAK1906_ADDRESS, sizeof(T)); // Request 1 byte of data
247  structSize = Wire.available(); // Use the actual number of bytes
248  for (uint8_t i = 0; i < structSize; i++)
249  *bytePtr++ = Wire.read(); // loop for each byte to be read
250  return (structSize);
251  } //end getData()
252 
253  template <typename T>
254  uint8_t &putData(const uint8_t addr, const T &value) const
255  {
256  const uint8_t *bytePtr = (const uint8_t *)&value; // Pointer to structure beginning
257  static uint8_t structSize = sizeof(T); // Number of bytes in structure
258  Wire.beginTransmission(RAK1906_ADDRESS); // Address the I2C device
259  Wire.write(addr); // Send register address to write
260  for (uint8_t i = 0; i < sizeof(T); i++)
261  Wire.write(*bytePtr++); // loop for each byte to be written
262  Wire.endTransmission(); // Close transmission
263  return (structSize);
264  }
265 
266 
267 };
268 
269 #endif // end RAK1906_
Definition: rak1906.h:116
Definition: rak1906.h:35
Definition: rak1906.h:112
Definition: rak1906.h:113
Definition: rak1906.h:106
int32_t tmpGas
Definition: rak1906.h:175
Definition: rak1906.h:107
uint32_t endTransmission(uint8_t sendStop=true)
Definition: rak1906.h:114
Definition: rak1906.h:120
bool update(void)
This function will trigger the RAK1906 to update the Environment values, after the values are updated...
Definition: rak1906.h:42
Definition: rak1906.h:108
Definition: rak1906.h:29
RAK1906_Commands
Definition: rak1906.h:21
Definition: rak1906.h:111
virtual int available(void)
virtual int read(void)
Definition: rak1906.h:46
Definition: rak1906.h:38
Definition: rak1906.h:27
Definition: rak1906.h:113
TwoWire Wire
Definition: rak1906.h:125
Definition: rak1906.h:111
Definition: rak1906.h:114
Definition: rak1906.h:122
Definition: rak1906.h:41
float temperature(void)
This function will trigger the RAK1906 to return the latest TEMPERATURE value.
float humidity(void)
This function will trigger the RAK1906 to return the latest HUMIDITY value.
float gas(void)
This function will trigger the RAK1906 to return the latest GAS value.
Definition: rak1906.h:37
Definition: rak1906.h:31
void beginTransmission(uint8_t address)
bool init(void)
This function enabled the RAK1906 sensor and puts the device into standby mode / sleep mode for lowes...
Definition: rak1906.h:33
int32_t tmpPressure
Definition: rak1906.h:175
Definition: rak1906.h:25
virtual size_t write(uint8_t value)
Definition: rak1906.h:117
int32_t tmpTemperature
Definition: rak1906.h:175
uint8_t requestFrom(uint8_t, uint8_t)
Definition: rak1906.h:113
Definition: rak1906.h:113
Definition: rak1906.h:113
sensorTypes
Definition: rak1906.h:111
Definition: rak1906.h:47
Definition: rak1906.h:43
#define RAK1906_ADDRESS
Definition: rak1906.h:13
Definition: rak1906.h:118
Definition: rak1906.h:45
Definition: rak1906.h:22
float pressure(void)
This function will trigger the RAK1906 to return the latest AIR PRESSURE value.
int32_t tmpHumidity
Definition: rak1906.h:175
Definition: rak1906.h:113
Definition: rak1906.h:112
oversamplingTypes
Definition: rak1906.h:115
Definition: rak1906.h:111
Definition: rak1906.h:113
Definition: rak1906.h:119
Definition: rak1906.h:23
Definition: rak1906.h:105
RAK1906_ErrorCode
Definition: rak1906.h:104
Definition: rak1906.h:39
Definition: rak1906.h:121
iirFilterTypes
Definition: rak1906.h:113