upm  0.3.2
Sensor/Actuator repository for libmraa (v0.7.2)
hp20x.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 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 
29 #define HP20X_I2C_BUS 0
30 #define HP20X_DEFAULT_I2C_ADDR 0x76
31 
32 namespace upm {
33 
63  class HP20X {
64  public:
65 
69  typedef enum {
70  CMD_SOFT_RST = 0x06,
71 
72  CMD_ADC_CVT = 0x40, // mask - ANDed with DSR and CHNL bits
73 
74  CMD_READ_PT = 0x10, // read pressure/temp
75  CMD_READ_AT = 0x11, // read alt/temp
76 
77  CMD_READ_P = 0x30, // read pressure only
78  CMD_READ_A = 0x31, // read alt only
79  CMD_READ_T = 0x32, // read temp only
80 
81  CMD_ANA_CAL = 0x28, // recalibrate internal analog blocks
82 
83  CMD_READ_REG = 0x80, // mask - ANDed with reg addr
84  CMD_WRITE_REG = 0xc0 // mask - ANDed with reg addr
85  } HP20X_CMD_T;
86 
90  typedef enum {
91  CHNL_PT = 0x00, // pressure and temperature
92  CHNL_T = 0x02, // temperature
93 
94  CHNL_SHIFT = 0 // don't use, indicates position in REG
95  } CHNL_BITS_T;
96 
100  typedef enum {
101  DSR_4096 = 0x00, // decimation rate of digital filter
102  DSR_2048 = 0x01,
103  DSR_1024 = 0x02,
104  DSR_512 = 0x03,
105  DSR_256 = 0x04,
106  DSR_128 = 0x05,
107 
108  DSR_SHIFT = 2 // don't use, indicates position in REG
109  } DSR_BITS_T;
110 
111 
115  typedef enum {
116  REG_ALT_OFF_LSB = 0x00,
117  REG_ALT_OFF_MSB = 0x01,
118 
119  REG_PA_H_TH_LSB = 0x02, // Pres/Alt high threshold
120  REG_PA_H_TH_MSB = 0x03,
121 
122  REG_PA_M_TH_LSB = 0x04, // Pres/Alt medium threshold
123  REG_PA_M_TH_MSB = 0x05,
124 
125  REG_PA_L_TH_LSB = 0x06, // Pres/Alt low threshold
126  REG_PA_L_TH_MSB = 0x07,
127 
128  REG_T_H_TH = 0x08, // temperature high threshold
129  REG_T_M_TH = 0x09,
130  REG_T_L_TH = 0x0a,
131 
132  REG_INT_EN = 0x0b, // interrupt enables
133  REG_INT_CFG = 0x0c, // interrupt configuration
134  REG_INT_SRC = 0x0d, // interrupt sources
135 
136  REG_PARA = 0x0e // parameters config
137  } HP20X_REG_T;
138 
142  typedef enum {
143  INT_EN_T_WIN_EN = 0x01,
144  INT_EN_PA_WIN_EN = 0x02,
145 
146  INT_EN_T_TRAV_EN = 0x04,
147  INT_EN_PA_TRAV_EN = 0x08,
148 
149  INT_EN_T_RDY_EN = 0x10,
150  INT_EN_PA_RDY_EN = 0x20
151  // 0x40, 0x80 reserved
152  } INT_EN_BITS_T;
153 
157  typedef enum {
158  INT_CFG_T_WIN_CFG = 0x01,
159  INT_CFG_PA_WIN_CFG = 0x02,
160 
161  INT_CFG_T_TRAV_CFG = 0x04,
162  INT_CFG_PA_TRAV_CFG = 0x08,
163 
164  INT_CFG_T_RDY_CFG = 0x10,
165  INT_CFG_PA_RDY_CFG = 0x20,
166 
167  INT_CFG_PA_MODE = 0x40
168  // 0x80 reserved
169  } INT_CFG_BITS_T;
170 
174  typedef enum {
175  INT_SRC_T_WIN = 0x01,
176  INT_SRC_PA_WIN = 0x02,
177 
178  INT_SRC_T_TRAV = 0x04,
179  INT_SRC_PA_TRAV = 0x08,
180 
181  INT_SRC_T_RDY = 0x10,
182  INT_SRC_PA_RDY = 0x20,
183 
184  INT_SRC_DEV_RDY = 0x40, // device is ready
185 
186  INT_SRC_TH_ERR = 0x80 // threshold error
187  } INT_SRC_BITS_T;
188 
192  typedef enum {
193  // 0x01-0x40 reserved
194  PARA_CMPS_EN = 0x80 // compensation enable
195  } PARA_BITS_T;
196 
203  HP20X(int bus=HP20X_I2C_BUS, uint8_t address=HP20X_DEFAULT_I2C_ADDR);
204 
208  ~HP20X();
209 
216  bool init(DSR_BITS_T dsr=DSR_4096);
217 
224  bool writeCmd(uint8_t cmd);
225 
233  bool writeReg(HP20X_REG_T reg, uint8_t data);
234 
241  uint8_t readReg(HP20X_REG_T reg);
242 
249  int readData();
250 
257  bool isReady();
258 
265  bool waitforDeviceReady();
266 
272  float getTemperature();
273 
279  float getPressure();
280 
286  float getAltitude();
287 
294  void compensationEnable(bool enable);
295 
304  bool setInterruptEnable(uint8_t bits);
305 
313  bool setInterruptConfig(uint8_t bits);
314 
322  uint8_t getInterruptSource();
323 
330  void setDSR(DSR_BITS_T dsr);
331 
332 
337  void recalibrateInternal();
338 
344  void softReset();
345 
353  void setAltitudeOffset(int16_t off);
354 
362  void setPAThreshholds(int16_t low, int16_t med, int16_t high);
363 
371  void setTemperatureThreshholds(int8_t low, int8_t med, int8_t high);
372 
373 
374  protected:
375  mraa::I2c m_i2c;
376 
377  private:
378  uint8_t m_addr;
379  uint8_t m_dsr;
380 
381  };
382 }
383 
384 
INT_CFG_BITS_T
Definition: hp20x.h:157
API for the HP20X-based Grove Barometer (High-Accuracy)
Definition: hp20x.h:63
DSR_BITS_T
Definition: hp20x.h:100
HP20X_REG_T
Definition: hp20x.h:115
void compensationEnable(bool enable)
Definition: hp20x.cxx:234
bool setInterruptEnable(uint8_t bits)
Definition: hp20x.cxx:242
INT_SRC_BITS_T
Definition: hp20x.h:174
void setDSR(DSR_BITS_T dsr)
Definition: hp20x.cxx:257
bool writeCmd(uint8_t cmd)
Definition: hp20x.cxx:99
bool setInterruptConfig(uint8_t bits)
Definition: hp20x.cxx:247
CHNL_BITS_T
Definition: hp20x.h:90
void softReset()
Definition: hp20x.cxx:268
float getTemperature()
Definition: hp20x.cxx:156
~HP20X()
Definition: hp20x.cxx:50
uint8_t readReg(HP20X_REG_T reg)
Definition: hp20x.cxx:127
Definition: a110x.h:29
bool waitforDeviceReady()
Definition: hp20x.cxx:78
HP20X(int bus=HP20X_I2C_BUS, uint8_t address=HP20X_DEFAULT_I2C_ADDR)
Definition: hp20x.cxx:36
int readData()
Definition: hp20x.cxx:134
bool writeReg(HP20X_REG_T reg, uint8_t data)
Definition: hp20x.cxx:111
float getAltitude()
Definition: hp20x.cxx:208
HP20X_CMD_T
Definition: hp20x.h:69
uint8_t getInterruptSource()
Definition: hp20x.cxx:252
void setAltitudeOffset(int16_t off)
Definition: hp20x.cxx:275
void recalibrateInternal()
Definition: hp20x.cxx:262
PARA_BITS_T
Definition: hp20x.h:192
void setTemperatureThreshholds(int8_t low, int8_t med, int8_t high)
Definition: hp20x.cxx:296
INT_EN_BITS_T
Definition: hp20x.h:142
bool init(DSR_BITS_T dsr=DSR_4096)
Definition: hp20x.cxx:54
void setPAThreshholds(int16_t low, int16_t med, int16_t high)
Definition: hp20x.cxx:281
float getPressure()
Definition: hp20x.cxx:182
bool isReady()
Definition: hp20x.cxx:68