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