/ Adafruit_LIS331.h
Adafruit_LIS331.h
  1  /*!
  2   *  @file Adafruit_LIS331.h
  3   *
  4   *  @mainpage Adafruit LIS331 breakout board
  5   *
  6   *  @section intro_sec Introduction
  7   * *  This is a library for the Adafruit LIS331X Family of Accelerometer
  8   breakout boards
  9   *
 10   *  Designed specifically to work with:
 11   *  * [Adafruit LIS331HH Triple-Axis Accelerometer
 12   (+-6g/12g/24g)](https://www.adafruit.com/product/4XXX)
 13   *  * [Adafruit H3LIS331 High-G Triple-Axis Accelerometer
 14   (+-100g/200g/400g)](https://www.adafruit.com/product/4XXX)
 15  
 16   *  Pick one up today in the adafruit shop!
 17   *
 18   *	These sensors communicate over I2C or SPI (our library code supports
 19   *both) so you can share it with a bunch of other sensors on the same I2C bus.
 20   *  There's an address selection pin so you can have two accelerometers share an
 21   *I2C bus.
 22   *
 23   *  Adafruit invests time and resources providing this open source code,
 24   *  please support Adafruit andopen-source hardware by purchasing products
 25   *  from Adafruit!
 26   *
 27   *  @section author Author
 28   *
 29   *  Bryan Siepert / K. Townsend / Limor Fried (Adafruit Industries)
 30   *
 31   *  @section license License
 32   *
 33   *  BSD license, all text above must be included in any redistribution
 34   */
 35  
 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>
 48  /** Default I2C ADDRESS. If SDO/SA0 is 3V, its 0x19**/
 49  #define LIS331_DEFAULT_ADDRESS (0x18)
 50  #define LIS331_CHIP_ID                                                         \
 51    0x32 ///< The default response to WHO_AM_I for the H3LIS331 and LIS331HH
 52  
 53  #define LIS331_REG_WHOAMI                                                      \
 54    0x0F /**< Device identification register. [0, 0, 1, 1, 0, 0, 1, 1] */
 55  #define LIS331_REG_CTRL1 0x20 ///< Power mode, data rate, axis enable
 56  #define LIS331_REG_CTRL2 0x21 ///< Memory reboot, HPF config
 57  #define LIS331_REG_CTRL3                                                       \
 58    0x22 ///< Interrupt config, poarity, pin mode, latching, pin enable
 59  #define LIS331_REG_CTRL4 0x23           ///< BDU, Endianness, Range, SPI mode
 60  #define LIS331_REG_CTRL5 0x24           ///< Sleep to wake enable
 61  #define LIS331_REG_HP_FILTER_RESET 0x25 ///< Dummy register to reset filter
 62  #define LIS331_REG_REFERENCE 0x26       ///< HPF reference value
 63  #define LIS331_REG_STATUS 0x27  ///< Data overrun status, Data available status
 64  #define LIS331_REG_OUT_X_L 0x28 /**< X-axis acceleration data. Low value */
 65  #define LIS331_REG_OUT_X_H 0x29 /**< X-axis acceleration data. High value */
 66  #define LIS331_REG_OUT_Y_L 0x2A /**< Y-axis acceleration data. Low value */
 67  #define LIS331_REG_OUT_Y_H 0x2B /**< Y-axis acceleration data. High value */
 68  #define LIS331_REG_OUT_Z_L 0x2C /**< Z-axis acceleration data. Low value */
 69  #define LIS331_REG_OUT_Z_H 0x2D /**< Z-axis acceleration data. High value */
 70  #define LIS331_REG_INT1CFG 0x30 ///< INT1 config. Enable on hi/low for each axis
 71  #define LIS331_REG_INT1SRC 0x31 ///< INT1 source info
 72  #define LIS331_REG_INT1THS 0x32 ///< INT1 acceleration threshold
 73  #define LIS331_REG_INT1DUR 0x33 ///< INT1 duration threshold
 74  #define LIS331_REG_INT2CFG 0x34 ///< INT2 config. Enable on hi/low for each axis
 75  #define LIS331_REG_INT2SRC 0x35 ///< INT2 source info
 76  #define LIS331_REG_INT2THS 0x36 ///< INT2 acceleration threshold
 77  #define LIS331_REG_INT2DUR 0x37 ///< INT3 duration threshold
 78  
 79  /** The high pass filter cutoff frequency */
 80  typedef enum hpf_cutoff {
 81    LIS331_HPF_0_02_ODR,   ///< ODR/50
 82    LIS331_HPF_0_01_ODR,   ///< ODR/100
 83    LIS331_HPF_0_005_ODR,  ///< ODR/200
 84    LIS331_HPF_0_0025_ODR, ///< ODR/400
 85  } lis331_hpf_cutoff_t;
 86  
 87  /** The low pass filter cutoff frequency **/
 88  typedef enum {
 89    LIS331_LPF_37_HZ,
 90    LIS331_LPF_74_HZ,
 91    LIS331_LPF_292_HZ,
 92    LIS331_LPF_780_HZ,
 93  
 94  } lis331_lpf_cutoff_t;
 95  
 96  /** Used with register 0x2A (LIS331HH_REG_CTRL_REG1) to set bandwidth **/
 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,
108  } lis331_data_rate_t;
109  
110  /** A structure to represent axes **/
111  typedef enum {
112    LIS331_AXIS_X = 0x0,
113    LIS331_AXIS_Y = 0x1,
114    LIS331_AXIS_Z = 0x2,
115  } lis331_axis_t;
116  
117  /**
118   * @brief Mode Options
119   *
120   */
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;
127  /*!
128   *  @brief  Class that stores state and functions for interacting with
129   *          Adafruit_LIS331
130   */
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,
146                              lis331_hpf_cutoff_t cutoff = LIS331_HPF_0_0025_ODR,
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);
154    lis331_data_rate_t getDataRate(void);
155  
156    lis331_mode_t getMode(void);
157    lis331_mode_t getMode(lis331_data_rate_t rate);
158    int16_t x; /**< x axis value */
159    int16_t y; /**< y axis value */
160    int16_t z; /**< z axis value */
161  
162  protected:
163    float x_g; /**< x_g axis value (calculated by selected range) */
164    float y_g; /**< y_g axis value (calculated by selected range) */
165    float z_g; /**< z_g axis value (calculated by selected scale) */
166    virtual void _scaleValues(void);
167  
168    Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface
169    Adafruit_SPIDevice *spi_dev = NULL; ///< Pointer to I2C bus interface
170  
171    void writeRange(uint8_t range);
172    uint8_t readRange(void);
173  
174  private:
175    int32_t _sensorID;
176  };
177  
178  #endif