upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
h3lis331dl.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 H3LIS331DL_I2C_BUS 0
31 #define H3LIS331DL_DEFAULT_I2C_ADDR 0x18
32 
33 namespace upm {
34 
58  class H3LIS331DL {
59  public:
60 
64  typedef enum {
65  // Reserved bytes must not be written into as they contain
66  // factory calibration data. Changing those values may lead to
67  // improper functioning of the device.
68 
69  // 0x00-0x0E reserved
70 
71  REG_WHOAMI = 0x0f,
72 
73  // 0x10-0x1f reserved
74 
75  REG_REG1 = 0x20,
76  REG_REG2 = 0x21,
77  REG_REG3 = 0x22,
78  REG_REG4 = 0x23,
79  REG_REG5 = 0x24,
80 
81  REG_HP_FILTER_RESET = 0x25,
82  REG_REFERENCE = 0x26,
83 
84  REG_STATUS = 0x27,
85 
86  REG_OUT_X_L = 0x28,
87  REG_OUT_X_H = 0x29,
88  REG_OUT_Y_L = 0x2a,
89  REG_OUT_Y_H = 0x2b,
90  REG_OUT_Z_L = 0x2c,
91  REG_OUT_Z_H = 0x2d,
92 
93  // 0x2e, 0x2f reserved
94 
95  REG_INT1_CFG = 0x30,
96  REG_INT1_SRC = 0x31,
97  REG_INT1_THS = 0x32,
98  REG_INT1_DUR = 0x33,
99 
100  REG_INT2_CFG = 0x34,
101  REG_INT2_SRC = 0x35,
102  REG_INT2_THS = 0x36,
103  REG_INT2_DUR = 0x37,
104 
105  // 0x38-0x3f reserved
107 
111  typedef enum {
112  REG1_XEN = 0x01, // X-axis enable
113  REG1_YEN = 0x02,
114  REG1_ZEN = 0x04,
115 
116  REG1_DR0 = 0x08, // data rate
117  REG1_DR1 = 0x10,
118  REG1_DR_SHIFT = 3, // DR shift
119 
120  REG1_PM0 = 0x20, // power mode
121  REG1_PM1 = 0x40,
122  REG1_PM2 = 0x80,
123  REG1_PM_SHIFT = 5
124  } REG1_BITS_T;
125 
129  typedef enum {
130  DR_50_37 = 0x0, // 50Hz output with 37Hz LPF cutoff
131  DR_100_74 = 0x1,
132  DR_400_292 = 0x2,
133  DR_1000_780 = 0x3
134  } DR_BITS_T;
135 
139  typedef enum {
140  PM_POWERDWN = 0x0,
141  PM_NORMAL = 0x1,
142  PM_LP05 = 0x2, // .5 updates/sec
143  PM_LP1 = 0x3, // 1 update/sec
144  PM_LP2 = 0x4,
145  PM_LP5 = 0x5,
146  PM_LP10 = 0x6
147  } PM_BITS_T;
148 
152  typedef enum {
153  REG2_HPCF0 = 0x01,
154  REG2_HPCF1 = 0x02,
155  REG2_HPCF_SHIFT = 0,
156 
157  REG2_HPEN1 = 0x04,
158  REG2_HPEN2 = 0x08,
159  REG2_FDS = 0x10,
160 
161  REG2_HPM0 = 0x20,
162  REG2_HPM1 = 0x40,
163  REG2_HPM_SHIFT = 5,
164 
165  REG2_BOOT = 0x80
166  } REG2_BITS_T;
167 
171  typedef enum {
172  HPCF_8 = 0x0,
173  HPCF_16 = 0x1,
174  HPCF_32 = 0x2,
175  HPCF_64 = 0x3,
176  } HPCF_BITS_T;
177 
181  typedef enum {
182  HPM_NORMAL0 = 0x0,
183  HPM_REF = 0x1,
184  HPM_NORMAL1 = 0x2
185  } HPM_BITS_T;
186 
190  typedef enum {
191  REG3_I1_CFG0 = 0x01,
192  REG3_I1_CFG1 = 0x02,
193  REG3_I1_CFG_SHIFT = 0,
194 
195  REG3_LIR1 = 0x04,
196 
197  REG3_I2_CFG0 = 0x08,
198  REG3_I2_CFG1 = 0x10,
199  REG3_I2_CFG_SHIFT = 3,
200 
201  REG3_LIR2 = 0x20,
202  REG3_PP_OD = 0x40,
203  REG3_IHL = 0x80
204  } REG3_BITS_T;
205 
209  typedef enum {
210  I_SRC = 0x0, // INT source
211  I_OR = 0x1, // INT1 OR INT2 source
212  I_DR = 0x2, // Data Ready
213  I_BOOTING = 0x3 // Boot is running
214  } I_CFG_BITS_T;
215 
219  typedef enum {
220  REG4_SIM = 0x01, // SPI 4 or 3 wire
221 
222  // bits 01,02,04 reserved
223 
224  REG4_FS0 = 0x10,
225  REG4_FS1 = 0x20,
226  REG4_FS_SHIFT = 4,
227 
228  REG4_BLE = 0x40, // big/little-endian
229  REG4_BDU = 0x80 // Block data update
230  } REG4_BITS_T;
231 
235  typedef enum {
236  FS_100 = 0x0, // 100g scale
237  FS_200 = 0x1, // 200g scale
238  FS_400 = 0x3 // 400g scale
239  } FS_BITS_T;
240 
244  typedef enum {
245  REG5_TURNON0 = 0x01, // turn-on mode for sleep-to-wake
246  REG5_TURNON1 = 0x02
247 
248  // bits 04-80 reserved
249  } REG5_BITS_T;
250 
254  typedef enum {
255  STATUS_XDA = 0x01, // X data available
256  STATUS_YDA = 0x02,
257  STATUS_ZDA = 0x04,
258  STATUS_ZYXDA = 0x08, // X, Y, and Z data available
259  STATUS_XOR = 0x10, // X overrun
260  STATUS_YOR = 0x20,
261  STATUS_ZOR = 0x40,
262  STATUS_ZYXOR = 0x80 // X, Y, and Z data overrun
263  } STATUS_BITS_T;
264 
268  typedef enum {
269  INT_CFG_XLIE = 0x01, // enable intr on low X event
270  INT_CFG_XHIE = 0x02, // enable intr on high X event
271  INT_CFG_YLIE = 0x04,
272  INT_CFG_YHIE = 0x08,
273  INT_CFG_ZLIE = 0x10,
274  INT_CFG_ZHIE = 0x20,
275  // 0x40 reserved
276  INT_CFG_AOI = 0x80 // AND or OR combination or intrs
277  } INT_CFG_BITS_T;
278 
282  typedef enum {
283  INT_SRC_XL = 0x01, // X low intr event
284  INT_SRC_XH = 0x02, // X high intr event
285  INT_SRC_YL = 0x04,
286  INT_SRC_YH = 0x08,
287  INT_SRC_ZL = 0x10,
288  INT_SRC_ZH = 0x20,
289  INT_SRC_IA = 0x40 // Interrupt generated (active)
290  // 0x80 reserved
291  } INT_SRC_BITS_T;
292 
299  H3LIS331DL(int bus, uint8_t address = H3LIS331DL_DEFAULT_I2C_ADDR);
300 
304  ~H3LIS331DL();
305 
314  bool init(DR_BITS_T odr=DR_50_37, PM_BITS_T pm=PM_NORMAL,
315  FS_BITS_T fs=FS_100);
316 
322  uint8_t getChipID();
323 
330  bool setDataRate(DR_BITS_T odr);
331 
338  bool setPowerMode(PM_BITS_T pm);
339 
349  bool enableAxis(uint8_t axisEnable);
350 
357  bool setFullScale(FS_BITS_T fs);
358 
365  bool setHPCF(HPCF_BITS_T val);
366 
373  bool setHPM(HPM_BITS_T val);
374 
383  bool boot();
384 
391  bool enableHPF1(bool enable);
392 
399  bool enableHPF2(bool enable);
400 
407  bool enableFDS(bool enable);
408 
415  bool setInterruptActiveLow(bool enable);
416 
423  bool setInterruptOpenDrain(bool enable);
424 
431  bool setInterrupt1Latch(bool enable);
432 
439  bool setInterrupt2Latch(bool enable);
440 
448 
456 
465  bool enableBDU(bool enable);
466 
473  bool enableBLE(bool enable);
474 
481  bool enableSleepToWake(bool enable);
482 
488  uint8_t getStatus();
489 
496  bool setInterrupt1Config(uint8_t val);
497 
504  bool setInterrupt2Config(uint8_t val);
505 
512  bool setInterrupt1Source(uint8_t val);
513 
520  bool setInterrupt2Source(uint8_t val);
521 
528  bool setInterrupt1Threshold(uint8_t val);
529 
536  bool setInterrupt2Threshold(uint8_t val);
537 
544  bool setInterrupt1Duration(uint8_t val);
545 
552  bool setInterrupt2Duration(uint8_t val);
553 
557  void update();
558 
568  void setAdjustmentOffsets(int adjX, int adjY, int adjZ);
569 
577  void getAcceleration(float *aX, float *aY, float *aZ);
578 
586  void getRawXYZ(int *x, int *y, int *z);
587 
595  void getXYZ(int *x, int *y, int *z);
596 
597 #ifdef SWIGJAVA
598 
603  float *getAcceleration();
604 
610  int *getRawXYZ();
611 
617  int *getXYZ();
618 #endif
619 
620 
627  mraa::I2c& i2cContext() { return m_i2c; };
628 
629 
630  protected:
631  int16_t m_rawX, m_rawY, m_rawZ;
632  int16_t m_adjX, m_adjY, m_adjZ;
633  mraa::I2c m_i2c;
634 
635  private:
636  uint8_t m_addr;
637  };
638 }
639 
640 
uint8_t getStatus()
Definition: h3lis331dl.cxx:432
REG2_BITS_T
Definition: h3lis331dl.h:152
I_CFG_BITS_T
Definition: h3lis331dl.h:209
DR_BITS_T
Definition: h3lis331dl.h:129
uint8_t getChipID()
Definition: h3lis331dl.cxx:78
H3LIS331DL(int bus, uint8_t address=H3LIS331DL_DEFAULT_I2C_ADDR)
Definition: h3lis331dl.cxx:37
bool setInterrupt1Source(uint8_t val)
Definition: h3lis331dl.cxx:454
bool setInterrupt1Duration(uint8_t val)
Definition: h3lis331dl.cxx:483
bool setFullScale(FS_BITS_T fs)
Definition: h3lis331dl.cxx:134
bool setInterrupt2PadConfig(I_CFG_BITS_T val)
Definition: h3lis331dl.cxx:357
REG5_BITS_T
Definition: h3lis331dl.h:244
bool setHPCF(HPCF_BITS_T val)
Definition: h3lis331dl.cxx:151
REG1_BITS_T
Definition: h3lis331dl.h:111
bool setInterrupt2Source(uint8_t val)
Definition: h3lis331dl.cxx:512
bool setDataRate(DR_BITS_T odr)
Definition: h3lis331dl.cxx:83
bool enableBDU(bool enable)
Definition: h3lis331dl.cxx:375
HPM_BITS_T
Definition: h3lis331dl.h:181
bool enableHPF1(bool enable)
Definition: h3lis331dl.cxx:207
H3LIS331DL_REG_T
Definition: h3lis331dl.h:64
bool enableHPF2(bool enable)
Definition: h3lis331dl.cxx:226
void update()
Definition: h3lis331dl.cxx:553
void getAcceleration(float *aX, float *aY, float *aZ)
Definition: h3lis331dl.cxx:580
bool enableBLE(bool enable)
Definition: h3lis331dl.cxx:394
void setAdjustmentOffsets(int adjX, int adjY, int adjZ)
Definition: h3lis331dl.cxx:573
bool setInterrupt2Threshold(uint8_t val)
Definition: h3lis331dl.cxx:529
bool setInterrupt1Latch(bool enable)
Definition: h3lis331dl.cxx:302
mraa::I2c & i2cContext()
Definition: h3lis331dl.h:627
API for the H3LIS331DL-based Grove 3-Axis Digital Accelerometer (400g)
Definition: h3lis331dl.h:58
void getXYZ(int *x, int *y, int *z)
Definition: h3lis331dl.cxx:596
bool boot()
Definition: h3lis331dl.cxx:185
bool enableSleepToWake(bool enable)
Definition: h3lis331dl.cxx:413
bool setInterruptActiveLow(bool enable)
Definition: h3lis331dl.cxx:264
bool setPowerMode(PM_BITS_T pm)
Definition: h3lis331dl.cxx:100
FS_BITS_T
Definition: h3lis331dl.h:235
bool setInterrupt1Threshold(uint8_t val)
Definition: h3lis331dl.cxx:471
PM_BITS_T
Definition: h3lis331dl.h:139
REG3_BITS_T
Definition: h3lis331dl.h:190
STATUS_BITS_T
Definition: h3lis331dl.h:254
bool setInterrupt2Duration(uint8_t val)
Definition: h3lis331dl.cxx:541
bool setInterrupt1Config(uint8_t val)
Definition: h3lis331dl.cxx:437
bool setInterrupt2Latch(bool enable)
Definition: h3lis331dl.cxx:321
bool setHPM(HPM_BITS_T val)
Definition: h3lis331dl.cxx:168
bool setInterrupt2Config(uint8_t val)
Definition: h3lis331dl.cxx:495
HPCF_BITS_T
Definition: h3lis331dl.h:171
REG4_BITS_T
Definition: h3lis331dl.h:219
bool setInterruptOpenDrain(bool enable)
Definition: h3lis331dl.cxx:283
bool init(DR_BITS_T odr=DR_50_37, PM_BITS_T pm=PM_NORMAL, FS_BITS_T fs=FS_100)
Definition: h3lis331dl.cxx:58
bool enableAxis(uint8_t axisEnable)
Definition: h3lis331dl.cxx:117
INT_CFG_BITS_T
Definition: h3lis331dl.h:268
~H3LIS331DL()
Definition: h3lis331dl.cxx:54
bool setInterrupt1PadConfig(I_CFG_BITS_T val)
Definition: h3lis331dl.cxx:340
INT_SRC_BITS_T
Definition: h3lis331dl.h:282
bool enableFDS(bool enable)
Definition: h3lis331dl.cxx:245
void getRawXYZ(int *x, int *y, int *z)
Definition: h3lis331dl.cxx:589