forked from adafruit/Adafruit_LIS3DH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_LIS3DH.h
executable file
·157 lines (127 loc) · 4.53 KB
/
Adafruit_LIS3DH.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**************************************************************************/
/*!
@file Adafruit_LIS3DH.h
@author K. Townsend / Limor Fried (Adafruit Industries)
@license BSD (see license.txt)
This is a library for the Adafruit LIS3DH Accel breakout board
----> https://www.adafruit.com/products/2809
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#ifndef ADAFRUIT_LIS3DH_H
#define ADAFRUIT_LIS3DH_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#ifndef __AVR_ATtiny85__
#include <SPI.h>
#endif
#include <Adafruit_Sensor.h>
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define LIS3DH_DEFAULT_ADDRESS (0x18) // if SDO/SA0 is 3V, its 0x19
/*=========================================================================*/
#define LIS3DH_REG_STATUS1 0x07
#define LIS3DH_REG_OUTADC1_L 0x08
#define LIS3DH_REG_OUTADC1_H 0x09
#define LIS3DH_REG_OUTADC2_L 0x0A
#define LIS3DH_REG_OUTADC2_H 0x0B
#define LIS3DH_REG_OUTADC3_L 0x0C
#define LIS3DH_REG_OUTADC3_H 0x0D
#define LIS3DH_REG_INTCOUNT 0x0E
#define LIS3DH_REG_WHOAMI 0x0F
#define LIS3DH_REG_TEMPCFG 0x1F
#define LIS3DH_REG_CTRL1 0x20
#define LIS3DH_REG_CTRL2 0x21
#define LIS3DH_REG_CTRL3 0x22
#define LIS3DH_REG_CTRL4 0x23
#define LIS3DH_REG_CTRL5 0x24
#define LIS3DH_REG_CTRL6 0x25
#define LIS3DH_REG_REFERENCE 0x26
#define LIS3DH_REG_STATUS2 0x27
#define LIS3DH_REG_OUT_X_L 0x28
#define LIS3DH_REG_OUT_X_H 0x29
#define LIS3DH_REG_OUT_Y_L 0x2A
#define LIS3DH_REG_OUT_Y_H 0x2B
#define LIS3DH_REG_OUT_Z_L 0x2C
#define LIS3DH_REG_OUT_Z_H 0x2D
#define LIS3DH_REG_FIFOCTRL 0x2E
#define LIS3DH_REG_FIFOSRC 0x2F
#define LIS3DH_REG_INT1CFG 0x30
#define LIS3DH_REG_INT1SRC 0x31
#define LIS3DH_REG_INT1THS 0x32
#define LIS3DH_REG_INT1DUR 0x33
#define LIS3DH_REG_CLICKCFG 0x38
#define LIS3DH_REG_CLICKSRC 0x39
#define LIS3DH_REG_CLICKTHS 0x3A
#define LIS3DH_REG_TIMELIMIT 0x3B
#define LIS3DH_REG_TIMELATENCY 0x3C
#define LIS3DH_REG_TIMEWINDOW 0x3D
#define LIS3DH_REG_ACTTHS 0x3E
#define LIS3DH_REG_ACTDUR 0x3F
typedef enum
{
LIS3DH_RANGE_16_G = 0b11, // +/- 16g
LIS3DH_RANGE_8_G = 0b10, // +/- 8g
LIS3DH_RANGE_4_G = 0b01, // +/- 4g
LIS3DH_RANGE_2_G = 0b00 // +/- 2g (default value)
} lis3dh_range_t;
typedef enum
{
LIS3DH_AXIS_X = 0x0,
LIS3DH_AXIS_Y = 0x1,
LIS3DH_AXIS_Z = 0x2,
} lis3dh_axis_t;
/* Used with register 0x2A (LIS3DH_REG_CTRL_REG1) to set bandwidth */
typedef enum
{
LIS3DH_DATARATE_400_HZ = 0b0111, // 400Hz
LIS3DH_DATARATE_200_HZ = 0b0110, // 200Hz
LIS3DH_DATARATE_100_HZ = 0b0101, // 100Hz
LIS3DH_DATARATE_50_HZ = 0b0100, // 50Hz
LIS3DH_DATARATE_25_HZ = 0b0011, // 25Hz
LIS3DH_DATARATE_10_HZ = 0b0010, // 10 Hz
LIS3DH_DATARATE_1_HZ = 0b0001, // 1 Hz
LIS3DH_DATARATE_POWERDOWN = 0,
LIS3DH_DATARATE_LOWPOWER_1K6HZ = 0b1000,
LIS3DH_DATARATE_LOWPOWER_5KHZ = 0b1001,
} lis3dh_dataRate_t;
class Adafruit_LIS3DH : public Adafruit_Sensor {
public:
Adafruit_LIS3DH(void);
Adafruit_LIS3DH(TwoWire *Wi);
Adafruit_LIS3DH(int8_t cspin);
Adafruit_LIS3DH(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
bool begin(uint8_t addr = LIS3DH_DEFAULT_ADDRESS);
void read();
int16_t readADC(uint8_t a);
void setRange(lis3dh_range_t range);
lis3dh_range_t getRange(void);
void setDataRate(lis3dh_dataRate_t dataRate);
lis3dh_dataRate_t getDataRate(void);
bool getEvent(sensors_event_t *event);
void getSensor(sensor_t *sensor);
uint8_t getOrientation(void);
void setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit = 10, uint8_t timelatency = 20, uint8_t timewindow = 255);
uint8_t getClick(void);
int16_t x, y, z;
float x_g, y_g, z_g;
private:
TwoWire *I2Cinterface;
uint8_t readRegister8(uint8_t reg);
void writeRegister8(uint8_t reg, uint8_t value);
uint8_t spixfer(uint8_t x = 0xFF);
int32_t _sensorID;
int8_t _i2caddr;
// SPI
int8_t _cs, _mosi, _miso, _sck;
};
#endif