Adafruit LIS331 Arduino Driver
Adafruit_LIS331.h
Go to the documentation of this file.
1 
36 #ifndef ADAFRUIT_LIS331_H
37 #define ADAFRUIT_LIS331_H
38 
39 #include "Arduino.h"
40 
41 #include <SPI.h>
42 #include <Wire.h>
43 
44 #include <Adafruit_BusIO_Register.h>
45 #include <Adafruit_I2CDevice.h>
46 #include <Adafruit_SPIDevice.h>
47 #include <Adafruit_Sensor.h>
49 #define LIS331_DEFAULT_ADDRESS (0x18)
50 #define LIS331_CHIP_ID \
51  0x32
52 
53 #define LIS331_REG_WHOAMI \
54  0x0F
55 #define LIS331_REG_CTRL1 0x20
56 #define LIS331_REG_CTRL2 0x21
57 #define LIS331_REG_CTRL3 \
58  0x22
59 #define LIS331_REG_CTRL4 0x23
60 #define LIS331_REG_CTRL5 0x24
61 #define LIS331_REG_HP_FILTER_RESET 0x25
62 #define LIS331_REG_REFERENCE 0x26
63 #define LIS331_REG_STATUS 0x27
64 #define LIS331_REG_OUT_X_L 0x28
65 #define LIS331_REG_OUT_X_H 0x29
66 #define LIS331_REG_OUT_Y_L 0x2A
67 #define LIS331_REG_OUT_Y_H 0x2B
68 #define LIS331_REG_OUT_Z_L 0x2C
69 #define LIS331_REG_OUT_Z_H 0x2D
70 #define LIS331_REG_INT1CFG 0x30
71 #define LIS331_REG_INT1SRC 0x31
72 #define LIS331_REG_INT1THS 0x32
73 #define LIS331_REG_INT1DUR 0x33
74 #define LIS331_REG_INT2CFG 0x34
75 #define LIS331_REG_INT2SRC 0x35
76 #define LIS331_REG_INT2THS 0x36
77 #define LIS331_REG_INT2DUR 0x37
78 
79 
80 typedef enum hpf_cutoff {
86 
88 typedef enum {
89  LIS331_LPF_37_HZ,
90  LIS331_LPF_74_HZ,
91  LIS331_LPF_292_HZ,
92  LIS331_LPF_780_HZ,
93 
95 
97 typedef enum {
98  LIS331_DATARATE_POWERDOWN = 0,
99  LIS331_DATARATE_50_HZ = 0x4,
100  LIS331_DATARATE_100_HZ = 0x5,
101  LIS331_DATARATE_400_HZ = 0x6,
102  LIS331_DATARATE_1000_HZ = 0x7,
103  LIS331_DATARATE_LOWPOWER_0_5_HZ = 0x8,
104  LIS331_DATARATE_LOWPOWER_1_HZ = 0xC,
105  LIS331_DATARATE_LOWPOWER_2_HZ = 0x10,
106  LIS331_DATARATE_LOWPOWER_5_HZ = 0x14,
107  LIS331_DATARATE_LOWPOWER_10_HZ = 0x18,
109 
111 typedef enum {
112  LIS331_AXIS_X = 0x0,
113  LIS331_AXIS_Y = 0x1,
114  LIS331_AXIS_Z = 0x2,
115 } lis331_axis_t;
116 
121 typedef enum {
122  LIS331_MODE_SHUTDOWN,
123  LIS331_MODE_NORMAL,
124  LIS331_MODE_LOW_POWER // Low power is from 2-6 so checks against this should
125  // be 'mode >=LIS331_MODE_LOW_POWER'
126 } lis331_mode_t;
131 class Adafruit_LIS331 : public Adafruit_Sensor {
132 public:
133  Adafruit_LIS331(TwoWire *Wi = &Wire);
134  Adafruit_LIS331(int8_t cspin, SPIClass *theSPI = &SPI);
135  Adafruit_LIS331(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
136 
137  uint8_t getDeviceID(void);
138  bool configIntDataReady(uint8_t irqnum = 1, bool activelow = true,
139  bool opendrain = true);
140 
141  void read(void);
142 
143  bool getEvent(sensors_event_t *event);
144  void getSensor(sensor_t *sensor);
145  void enableHighPassFilter(bool filter_enabled,
147  bool use_reference = false);
148  void setHPFReference(int8_t reference);
149  int8_t getHPFReference(void);
150  void HPFReset(void);
151  bool setLPFCutoff(lis331_lpf_cutoff_t cutoff);
152 
153  void setDataRate(lis331_data_rate_t dataRate);
155 
156  lis331_mode_t getMode(void);
158  int16_t x;
159  int16_t y;
160  int16_t z;
162 protected:
163  float x_g;
164  float y_g;
165  float z_g;
166  virtual void _scaleValues(void);
167 
168  Adafruit_I2CDevice *i2c_dev = NULL;
169  Adafruit_SPIDevice *spi_dev = NULL;
170 
171  void writeRange(uint8_t range);
172  uint8_t readRange(void);
173 
174 private:
175  int32_t _sensorID;
176 };
177 
178 #endif
lis331_mode_t
Mode Options.
Definition: Adafruit_LIS331.h:121
Adafruit_SPIDevice * spi_dev
Pointer to I2C bus interface.
Definition: Adafruit_LIS331.h:169
hpf_cutoff
Definition: Adafruit_LIS331.h:80
void setDataRate(lis331_data_rate_t dataRate)
Sets the data rate for the LIS331 (affects power consumption)
Definition: Adafruit_LIS331.cpp:240
enum hpf_cutoff lis331_hpf_cutoff_t
ODR/50.
Definition: Adafruit_LIS331.h:81
int16_t x
Definition: Adafruit_LIS331.h:158
void read(void)
Reads x y z values at once.
Definition: Adafruit_LIS331.cpp:62
bool setLPFCutoff(lis331_lpf_cutoff_t cutoff)
Set the Low Pass Filter cutoff frequency. Useful for removing high frequency noise while sensing orie...
Definition: Adafruit_LIS331.cpp:317
ODR/200.
Definition: Adafruit_LIS331.h:83
lis331_data_rate_t
Definition: Adafruit_LIS331.h:97
bool getEvent(sensors_event_t *event)
Gets the most recent sensor event.
Definition: Adafruit_LIS331.cpp:339
int16_t z
Definition: Adafruit_LIS331.h:160
Adafruit_LIS331(TwoWire *Wi=&Wire)
Instantiates a new LIS331 class in I2C.
Definition: Adafruit_LIS331.cpp:15
ODR/400.
Definition: Adafruit_LIS331.h:84
lis331_data_rate_t getDataRate(void)
Gets the data rate for the LIS331 (affects power consumption)
Definition: Adafruit_LIS331.cpp:276
void setHPFReference(int8_t reference)
Set the reference value to offset measurements when using the High-pass filter.
Definition: Adafruit_LIS331.cpp:169
bool configIntDataReady(uint8_t irqnum=1, bool activelow=true, bool opendrain=true)
Setup the INT1 or INT2 pin to trigger when new data is ready.
Definition: Adafruit_LIS331.cpp:99
lis331_lpf_cutoff_t
Definition: Adafruit_LIS331.h:88
lis331_axis_t
Definition: Adafruit_LIS331.h:111
Class that stores state and functions for interacting with Adafruit_LIS331.
Definition: Adafruit_LIS331.h:131
void HPFReset(void)
Zero the measurement offsets while the high-pass filter is enabled when not using a reference...
Definition: Adafruit_LIS331.cpp:191
Adafruit_I2CDevice * i2c_dev
Pointer to I2C bus interface.
Definition: Adafruit_LIS331.h:168
int8_t getHPFReference(void)
Gets the current high-pass filter reference/offset.
Definition: Adafruit_LIS331.cpp:179
uint8_t readRange(void)
Get the measurement range.
Definition: Adafruit_LIS331.cpp:225
void getSensor(sensor_t *sensor)
Gets the sensor_t data.
Definition: Adafruit_LIS331.cpp:362
float z_g
Definition: Adafruit_LIS331.h:165
int16_t y
Definition: Adafruit_LIS331.h:159
void enableHighPassFilter(bool filter_enabled, lis331_hpf_cutoff_t cutoff=LIS331_HPF_0_0025_ODR, bool use_reference=false)
Enables the high pass filter and/or slope filter.
Definition: Adafruit_LIS331.cpp:135
lis331_mode_t getMode(void)
Return the current power mode from the current data rate.
Definition: Adafruit_LIS331.cpp:288
void writeRange(uint8_t range)
Set the measurement range for the sensor.
Definition: Adafruit_LIS331.cpp:209
virtual void _scaleValues(void)
Scale the acceleration measuremets from their raw value to milli-g depending on the current measureme...
Definition: Adafruit_LIS331.cpp:202
float x_g
Definition: Adafruit_LIS331.h:163
ODR/100.
Definition: Adafruit_LIS331.h:82
uint8_t getDeviceID(void)
Get Device ID from LIS331_REG_WHOAMI.
Definition: Adafruit_LIS331.cpp:52
float y_g
Definition: Adafruit_LIS331.h:164