upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
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/common.hpp>
28 #include <mraa/i2c.hpp>
29 
30 #define HP20X_I2C_BUS 0
31 #define HP20X_DEFAULT_I2C_ADDR 0x76
32 
33 namespace upm {
34 
64  class HP20X {
65  public:
66 
70  typedef enum {
71  CMD_SOFT_RST = 0x06,
72 
73  CMD_ADC_CVT = 0x40, // mask - ANDed with DSR and CHNL bits
74 
75  CMD_READ_PT = 0x10, // read pressure/temp
76  CMD_READ_AT = 0x11, // read alt/temp
77 
78  CMD_READ_P = 0x30, // read pressure only
79  CMD_READ_A = 0x31, // read alt only
80  CMD_READ_T = 0x32, // read temp only
81 
82  CMD_ANA_CAL = 0x28, // recalibrate internal analog blocks
83 
84  CMD_READ_REG = 0x80, // mask - ANDed with reg addr
85  CMD_WRITE_REG = 0xc0 // mask - ANDed with reg addr
86  } HP20X_CMD_T;
87 
91  typedef enum {
92  CHNL_PT = 0x00, // pressure and temperature
93  CHNL_T = 0x02, // temperature
94 
95  CHNL_SHIFT = 0 // don't use, indicates position in REG
96  } CHNL_BITS_T;
97 
101  typedef enum {
102  DSR_4096 = 0x00, // decimation rate of digital filter
103  DSR_2048 = 0x01,
104  DSR_1024 = 0x02,
105  DSR_512 = 0x03,
106  DSR_256 = 0x04,
107  DSR_128 = 0x05,
108 
109  DSR_SHIFT = 2 // don't use, indicates position in REG
110  } DSR_BITS_T;
111 
112 
116  typedef enum {
117  REG_ALT_OFF_LSB = 0x00,
118  REG_ALT_OFF_MSB = 0x01,
119 
120  REG_PA_H_TH_LSB = 0x02, // Pres/Alt high threshold
121  REG_PA_H_TH_MSB = 0x03,
122 
123  REG_PA_M_TH_LSB = 0x04, // Pres/Alt medium threshold
124  REG_PA_M_TH_MSB = 0x05,
125 
126  REG_PA_L_TH_LSB = 0x06, // Pres/Alt low threshold
127  REG_PA_L_TH_MSB = 0x07,
128 
129  REG_T_H_TH = 0x08, // temperature high threshold
130  REG_T_M_TH = 0x09,
131  REG_T_L_TH = 0x0a,
132 
133  REG_INT_EN = 0x0b, // interrupt enables
134  REG_INT_CFG = 0x0c, // interrupt configuration
135  REG_INT_SRC = 0x0d, // interrupt sources
136 
137  REG_PARA = 0x0e // parameters config
138  } HP20X_REG_T;
139 
143  typedef enum {
144  INT_EN_T_WIN_EN = 0x01,
145  INT_EN_PA_WIN_EN = 0x02,
146 
147  INT_EN_T_TRAV_EN = 0x04,
148  INT_EN_PA_TRAV_EN = 0x08,
149 
150  INT_EN_T_RDY_EN = 0x10,
151  INT_EN_PA_RDY_EN = 0x20
152  // 0x40, 0x80 reserved
153  } INT_EN_BITS_T;
154 
158  typedef enum {
159  INT_CFG_T_WIN_CFG = 0x01,
160  INT_CFG_PA_WIN_CFG = 0x02,
161 
162  INT_CFG_T_TRAV_CFG = 0x04,
163  INT_CFG_PA_TRAV_CFG = 0x08,
164 
165  INT_CFG_T_RDY_CFG = 0x10,
166  INT_CFG_PA_RDY_CFG = 0x20,
167 
168  INT_CFG_PA_MODE = 0x40
169  // 0x80 reserved
170  } INT_CFG_BITS_T;
171 
175  typedef enum {
176  INT_SRC_T_WIN = 0x01,
177  INT_SRC_PA_WIN = 0x02,
178 
179  INT_SRC_T_TRAV = 0x04,
180  INT_SRC_PA_TRAV = 0x08,
181 
182  INT_SRC_T_RDY = 0x10,
183  INT_SRC_PA_RDY = 0x20,
184 
185  INT_SRC_DEV_RDY = 0x40, // device is ready
186 
187  INT_SRC_TH_ERR = 0x80 // threshold error
188  } INT_SRC_BITS_T;
189 
193  typedef enum {
194  // 0x01-0x40 reserved
195  PARA_CMPS_EN = 0x80 // compensation enable
196  } PARA_BITS_T;
197 
204  HP20X(int bus=HP20X_I2C_BUS, uint8_t address=HP20X_DEFAULT_I2C_ADDR);
205 
209  ~HP20X();
210 
217  bool init(DSR_BITS_T dsr=DSR_4096);
218 
225  bool writeCmd(uint8_t cmd);
226 
234  bool writeReg(HP20X_REG_T reg, uint8_t data);
235 
242  uint8_t readReg(HP20X_REG_T reg);
243 
250  int readData();
251 
258  bool isReady();
259 
266  bool waitforDeviceReady();
267 
273  float getTemperature();
274 
280  float getPressure();
281 
287  float getAltitude();
288 
295  void compensationEnable(bool enable);
296 
305  bool setInterruptEnable(uint8_t bits);
306 
314  bool setInterruptConfig(uint8_t bits);
315 
323  uint8_t getInterruptSource();
324 
331  void setDSR(DSR_BITS_T dsr);
332 
333 
338  void recalibrateInternal();
339 
345  void softReset();
346 
354  void setAltitudeOffset(int16_t off);
355 
363  void setPAThreshholds(int16_t low, int16_t med, int16_t high);
364 
372  void setTemperatureThreshholds(int8_t low, int8_t med, int8_t high);
373 
374 
375  protected:
376  mraa::I2c m_i2c;
377 
378  private:
379  uint8_t m_addr;
380  uint8_t m_dsr;
381 
382  };
383 }
384 
385 
API for the HP20X-based Grove Barometer (High-Accuracy)
Definition: hp20x.h:64
CHNL_BITS_T
Definition: hp20x.h:91
void compensationEnable(bool enable)
Definition: hp20x.cxx:214
PARA_BITS_T
Definition: hp20x.h:193
bool setInterruptEnable(uint8_t bits)
Definition: hp20x.cxx:222
INT_EN_BITS_T
Definition: hp20x.h:143
void setDSR(DSR_BITS_T dsr)
Definition: hp20x.cxx:237
HP20X_REG_T
Definition: hp20x.h:116
bool writeCmd(uint8_t cmd)
Definition: hp20x.cxx:100
bool setInterruptConfig(uint8_t bits)
Definition: hp20x.cxx:227
void softReset()
Definition: hp20x.cxx:248
float getTemperature()
Definition: hp20x.cxx:160
~HP20X()
Definition: hp20x.cxx:51
INT_SRC_BITS_T
Definition: hp20x.h:175
uint8_t readReg(HP20X_REG_T reg)
Definition: hp20x.cxx:130
INT_CFG_BITS_T
Definition: hp20x.h:158
bool waitforDeviceReady()
Definition: hp20x.cxx:79
HP20X(int bus=HP20X_I2C_BUS, uint8_t address=HP20X_DEFAULT_I2C_ADDR)
Definition: hp20x.cxx:37
int readData()
Definition: hp20x.cxx:137
HP20X_CMD_T
Definition: hp20x.h:70
DSR_BITS_T
Definition: hp20x.h:101
bool writeReg(HP20X_REG_T reg, uint8_t data)
Definition: hp20x.cxx:113
float getAltitude()
Definition: hp20x.cxx:196
uint8_t getInterruptSource()
Definition: hp20x.cxx:232
void setAltitudeOffset(int16_t off)
Definition: hp20x.cxx:255
void recalibrateInternal()
Definition: hp20x.cxx:242
void setTemperatureThreshholds(int8_t low, int8_t med, int8_t high)
Definition: hp20x.cxx:276
bool init(DSR_BITS_T dsr=DSR_4096)
Definition: hp20x.cxx:55
void setPAThreshholds(int16_t low, int16_t med, int16_t high)
Definition: hp20x.cxx:261
float getPressure()
Definition: hp20x.cxx:178
bool isReady()
Definition: hp20x.cxx:69