Adafruit ADXL343 Library
Adafruit_ADXL343.h
Go to the documentation of this file.
1 /**************************************************************************/
17 /**************************************************************************/
18 #ifndef _ADAFRUIT_SENSOR_ADXL343_H
19 #define _ADAFRUIT_SENSOR_ADXL343_H
20 
21 #if ARDUINO >= 100
22 #include "Arduino.h"
23 #else
24 #include "WProgram.h"
25 #endif
26 
27 #include <Adafruit_BusIO_Register.h>
28 #include <Adafruit_I2CDevice.h>
29 #include <Adafruit_Sensor.h>
30 #include <Wire.h>
31 
32 /*=========================================================================
33  I2C ADDRESS/BITS
34  -----------------------------------------------------------------------*/
35 #define ADXL343_ADDRESS (0x53)
36 /*=========================================================================*/
37 
38 /*=========================================================================
39  REGISTERS
40  -----------------------------------------------------------------------*/
41 #define ADXL343_REG_DEVID (0x00)
42 #define ADXL343_REG_THRESH_TAP (0x1D)
43 #define ADXL343_REG_OFSX (0x1E)
44 #define ADXL343_REG_OFSY (0x1F)
45 #define ADXL343_REG_OFSZ (0x20)
46 #define ADXL343_REG_DUR (0x21)
47 #define ADXL343_REG_LATENT (0x22)
48 #define ADXL343_REG_WINDOW (0x23)
49 #define ADXL343_REG_THRESH_ACT (0x24)
50 #define ADXL343_REG_THRESH_INACT (0x25)
51 #define ADXL343_REG_TIME_INACT (0x26)
52 #define ADXL343_REG_ACT_INACT_CTL \
53  (0x27)
54 #define ADXL343_REG_THRESH_FF (0x28)
55 #define ADXL343_REG_TIME_FF (0x29)
56 #define ADXL343_REG_TAP_AXES (0x2A)
57 #define ADXL343_REG_ACT_TAP_STATUS (0x2B)
58 #define ADXL343_REG_BW_RATE (0x2C)
59 #define ADXL343_REG_POWER_CTL (0x2D)
60 #define ADXL343_REG_INT_ENABLE (0x2E)
61 #define ADXL343_REG_INT_MAP (0x2F)
62 #define ADXL343_REG_INT_SOURCE (0x30)
63 #define ADXL343_REG_DATA_FORMAT (0x31)
64 #define ADXL343_REG_DATAX0 (0x32)
65 #define ADXL343_REG_DATAX1 (0x33)
66 #define ADXL343_REG_DATAY0 (0x34)
67 #define ADXL343_REG_DATAY1 (0x35)
68 #define ADXL343_REG_DATAZ0 (0x36)
69 #define ADXL343_REG_DATAZ1 (0x37)
70 #define ADXL343_REG_FIFO_CTL (0x38)
71 #define ADXL343_REG_FIFO_STATUS (0x39)
72 /*=========================================================================*/
73 
74 /*=========================================================================
75  REGISTERS
76  -----------------------------------------------------------------------*/
77 #define ADXL343_MG2G_MULTIPLIER (0.004)
78 /*=========================================================================*/
79 
81 typedef enum {
98 } dataRate_t;
99 
101 typedef enum {
106 } range_t;
107 
109 union int_config {
110  uint8_t value;
111  struct {
112  uint8_t overrun : 1;
113  uint8_t watermark : 1;
114  uint8_t freefall : 1;
115  uint8_t inactivity : 1;
116  uint8_t activity : 1;
117  uint8_t double_tap : 1;
118  uint8_t single_tap : 1;
119  uint8_t data_ready : 1;
120  } bits;
121 };
122 
124 typedef enum { ADXL343_INT1 = 0, ADXL343_INT2 = 1 } int_pin;
125 
129 class Adafruit_ADXL343 : public Adafruit_Sensor {
130 public:
131  Adafruit_ADXL343(int32_t sensorID);
132  Adafruit_ADXL343(int32_t sensorID, TwoWire *wireBus);
133  Adafruit_ADXL343(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t cs,
134  int32_t sensorID = -1);
135 
136  bool begin(uint8_t i2caddr = ADXL343_ADDRESS);
137  void setRange(range_t range);
138  range_t getRange(void);
139  void setDataRate(dataRate_t dataRate);
140  dataRate_t getDataRate(void);
141  bool getEvent(sensors_event_t *);
142  void getSensor(sensor_t *);
143 
144  uint8_t getDeviceID(void);
145  void writeRegister(uint8_t reg, uint8_t value);
146  uint8_t readRegister(uint8_t reg);
147  int16_t read16(uint8_t reg);
148 
149  bool enableInterrupts(int_config cfg);
150  bool mapInterrupts(int_config cfg);
151  uint8_t checkInterrupts(void);
152 
153  int16_t getX(void);
154  int16_t getY(void);
155  int16_t getZ(void);
156 
157 private:
158  Adafruit_SPIDevice *spi_dev = NULL;
159  Adafruit_I2CDevice *i2c_dev = NULL;
160 
161  TwoWire *_wire;
162  int32_t _sensorID;
163  range_t _range;
164  uint8_t _clk, _do, _di, _cs;
165  bool _i2c;
166 };
167 
168 #endif
Definition: Adafruit_ADXL343.h:86
uint8_t value
Definition: Adafruit_ADXL343.h:110
Definition: Adafruit_ADXL343.h:90
uint8_t single_tap
Definition: Adafruit_ADXL343.h:118
Definition: Adafruit_ADXL343.h:109
Definition: Adafruit_ADXL343.h:96
uint8_t inactivity
Definition: Adafruit_ADXL343.h:115
Definition: Adafruit_ADXL343.h:94
#define ADXL343_ADDRESS
Definition: Adafruit_ADXL343.h:35
uint8_t data_ready
Definition: Adafruit_ADXL343.h:119
Definition: Adafruit_ADXL343.h:88
uint8_t freefall
Definition: Adafruit_ADXL343.h:114
Definition: Adafruit_ADXL343.h:82
uint8_t watermark
Definition: Adafruit_ADXL343.h:113
uint8_t double_tap
Definition: Adafruit_ADXL343.h:117
Definition: Adafruit_ADXL343.h:85
Definition: Adafruit_ADXL343.h:104
Definition: Adafruit_ADXL343.h:87
struct int_config::@0 bits
Definition: Adafruit_ADXL343.h:83
Definition: Adafruit_ADXL343.h:92
Definition: Adafruit_ADXL343.h:102
range_t
Definition: Adafruit_ADXL343.h:101
Definition: Adafruit_ADXL343.h:103
uint8_t activity
Definition: Adafruit_ADXL343.h:116
Definition: Adafruit_ADXL343.h:105
Definition: Adafruit_ADXL343.h:129
dataRate_t
Definition: Adafruit_ADXL343.h:81
uint8_t overrun
Definition: Adafruit_ADXL343.h:112
Definition: Adafruit_ADXL343.h:97
Definition: Adafruit_ADXL343.h:91
Definition: Adafruit_ADXL343.h:93
Definition: Adafruit_ADXL343.h:95
int_pin
Definition: Adafruit_ADXL343.h:124
Definition: Adafruit_ADXL343.h:89
Definition: Adafruit_ADXL343.h:84