Adafruit Library
Adafruit_CPlay_LIS3DH.h
Go to the documentation of this file.
1 
27 #ifndef ADAFRUIT_CPLAY_LIS3DH_H
28 #define ADAFRUIT_CPLAY_LIS3DH_H
29 
30 #include "Arduino.h"
31 
32 #include <SPI.h>
33 #include <Wire.h>
34 
35 #include "Adafruit_CPlay_Sensor.h"
36 
38 #define LIS3DH_DEFAULT_ADDRESS (0x18) // if SDO/SA0 is 3V, its 0x19
39 
59 #define LIS3DH_REG_STATUS1 0x07
60 #define LIS3DH_REG_OUTADC1_L 0x08
61 #define LIS3DH_REG_OUTADC1_H 0x09
62 #define LIS3DH_REG_OUTADC2_L 0x0A
63 #define LIS3DH_REG_OUTADC2_H 0x0B
64 #define LIS3DH_REG_OUTADC3_L 0x0C
65 #define LIS3DH_REG_OUTADC3_H 0x0D
66 #define LIS3DH_REG_INTCOUNT \
67  0x0E
68 #define LIS3DH_REG_WHOAMI \
69  0x0F
78 #define LIS3DH_REG_TEMPCFG 0x1F
79 
90 #define LIS3DH_REG_CTRL1 0x20
91 
105 #define LIS3DH_REG_CTRL2 0x21
106 
124 #define LIS3DH_REG_CTRL3 0x22
125 
141 #define LIS3DH_REG_CTRL4 0x23
142 
154 #define LIS3DH_REG_CTRL5 0x24
155 
160 #define LIS3DH_REG_CTRL6 0x25
161 #define LIS3DH_REG_REFERENCE 0x26
181 #define LIS3DH_REG_STATUS2 0x27
182 #define LIS3DH_REG_OUT_X_L 0x28
183 #define LIS3DH_REG_OUT_X_H 0x29
184 #define LIS3DH_REG_OUT_Y_L 0x2A
185 #define LIS3DH_REG_OUT_Y_H 0x2B
186 #define LIS3DH_REG_OUT_Z_L 0x2C
187 #define LIS3DH_REG_OUT_Z_H 0x2D
197 #define LIS3DH_REG_FIFOCTRL 0x2E
198 #define LIS3DH_REG_FIFOSRC \
199  0x2F
221 #define LIS3DH_REG_INT1CFG 0x30
240 #define LIS3DH_REG_INT1SRC 0x31
241 #define LIS3DH_REG_INT1THS \
242  0x32
243 #define LIS3DH_REG_INT1DUR \
244  0x33
265 #define LIS3DH_REG_CLICKCFG 0x38
283 #define LIS3DH_REG_CLICKSRC 0x39
289 #define LIS3DH_REG_CLICKTHS 0x3A
295 #define LIS3DH_REG_TIMELIMIT 0x3B
301 #define LIS3DH_REG_TIMELATENCY 0x3C
307 #define LIS3DH_REG_TIMEWINDOW 0x3D
310 typedef enum {
311  LIS3DH_RANGE_16_G = 0b11, // +/- 16g
312  LIS3DH_RANGE_8_G = 0b10, // +/- 8g
313  LIS3DH_RANGE_4_G = 0b01, // +/- 4g
314  LIS3DH_RANGE_2_G = 0b00 // +/- 2g (default value)
316 
318 typedef enum {
319  LIS3DH_AXIS_X = 0x0,
320  LIS3DH_AXIS_Y = 0x1,
321  LIS3DH_AXIS_Z = 0x2,
322 } lis3dh_axis_t;
323 
325 typedef enum {
326  LIS3DH_DATARATE_400_HZ = 0b0111, // 400Hz
327  LIS3DH_DATARATE_200_HZ = 0b0110, // 200Hz
328  LIS3DH_DATARATE_100_HZ = 0b0101, // 100Hz
329  LIS3DH_DATARATE_50_HZ = 0b0100, // 50Hz
330  LIS3DH_DATARATE_25_HZ = 0b0011, // 25Hz
331  LIS3DH_DATARATE_10_HZ = 0b0010, // 10 Hz
332  LIS3DH_DATARATE_1_HZ = 0b0001, // 1 Hz
333  LIS3DH_DATARATE_POWERDOWN = 0,
334  LIS3DH_DATARATE_LOWPOWER_1K6HZ = 0b1000,
335  LIS3DH_DATARATE_LOWPOWER_5KHZ = 0b1001,
336 
338 
343 class Adafruit_CPlay_LIS3DH : public Adafruit_Sensor {
344 public:
345  Adafruit_CPlay_LIS3DH(TwoWire *Wi = &Wire);
346  Adafruit_CPlay_LIS3DH(int8_t cspin, SPIClass *theSPI = &SPI);
347  Adafruit_CPlay_LIS3DH(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
348 
349  bool begin(uint8_t addr = LIS3DH_DEFAULT_ADDRESS, uint8_t nWAI = 0x33);
350 
351  uint8_t getDeviceID();
352  bool haveNewData();
353 
354  void read();
355  int16_t readADC(uint8_t a);
356 
357  void setRange(lis3dh_range_t range);
358  lis3dh_range_t getRange(void);
359 
360  void setDataRate(lis3dh_dataRate_t dataRate);
362 
363  bool getEvent(sensors_event_t *event);
364  void getSensor(sensor_t *sensor);
365 
366  void setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit = 10,
367  uint8_t timelatency = 20, uint8_t timewindow = 255);
368  uint8_t getClick(void);
369 
370  int16_t x;
371  int16_t y;
372  int16_t z;
374  float x_g;
375  float y_g;
376  float z_g;
378 protected:
379  uint8_t spixfer(uint8_t x = 0xFF);
380  void writeRegister8(uint8_t reg, uint8_t value);
381  uint8_t readRegister8(uint8_t reg);
382 
383 private:
384  TwoWire *I2Cinterface;
385  SPIClass *SPIinterface;
386 
387  uint8_t _wai;
388 
389  int8_t _cs, _mosi, _miso, _sck;
390 
391  int8_t _i2caddr;
392 
393  int32_t _sensorID;
394 };
395 
396 #endif
Adafruit_CPlay_LIS3DH(TwoWire *Wi=&Wire)
Instantiates a new LIS3DH class in I2C.
Definition: Adafruit_CPlay_LIS3DH.cpp:34
Sensor API class for CircuitPlayground board.
Definition: Adafruit_CPlay_Sensor.h:143
bool begin(uint8_t addr=LIS3DH_DEFAULT_ADDRESS, uint8_t nWAI=0x33)
Setups the HW (reads coefficients values, etc.)
Definition: Adafruit_CPlay_LIS3DH.cpp:83
lis3dh_dataRate_t getDataRate(void)
Gets the data rate for the LIS3DH (controls power consumption)
Definition: Adafruit_CPlay_LIS3DH.cpp:353
float y_g
Definition: Adafruit_CPlay_LIS3DH.h:376
Class that stores state and functions for interacting with Adafruit_LIS3DH.
Definition: Adafruit_CPlay_LIS3DH.h:344
void getSensor(sensor_t *sensor)
Gets the sensor_t data.
Definition: Adafruit_CPlay_LIS3DH.cpp:386
uint8_t readRegister8(uint8_t reg)
Reads 8-bits from the specified register.
Definition: Adafruit_CPlay_LIS3DH.cpp:462
uint8_t getClick(void)
Get uint8_t for single or double click.
Definition: Adafruit_CPlay_LIS3DH.cpp:312
bool getEvent(sensors_event_t *event)
Gets the most recent sensor event.
Definition: Adafruit_CPlay_LIS3DH.cpp:363
Definition: Adafruit_CPlay_Sensor.h:126
void setRange(lis3dh_range_t range)
Sets the g range for the accelerometer.
Definition: Adafruit_CPlay_LIS3DH.cpp:321
float x_g
Definition: Adafruit_CPlay_LIS3DH.h:375
lis3dh_axis_t
Definition: Adafruit_CPlay_LIS3DH.h:319
int16_t z
Definition: Adafruit_CPlay_LIS3DH.h:373
int16_t readADC(uint8_t a)
Read the auxilary ADC.
Definition: Adafruit_CPlay_LIS3DH.cpp:229
lis3dh_dataRate_t
Definition: Adafruit_CPlay_LIS3DH.h:326
uint8_t getDeviceID()
Get Device ID from LIS3DH_REG_WHOAMI.
Definition: Adafruit_CPlay_LIS3DH.cpp:157
int16_t y
Definition: Adafruit_CPlay_LIS3DH.h:372
#define LIS3DH_DEFAULT_ADDRESS
Definition: Adafruit_CPlay_LIS3DH.h:38
void writeRegister8(uint8_t reg, uint8_t value)
Writes 8-bits to the specified destination register.
Definition: Adafruit_CPlay_LIS3DH.cpp:435
Definition: Adafruit_CPlay_Sensor.h:99
bool haveNewData()
Check to see if new data available.
Definition: Adafruit_CPlay_LIS3DH.cpp:165
int16_t x
Definition: Adafruit_CPlay_LIS3DH.h:371
void read()
Reads x y z values at once.
Definition: Adafruit_CPlay_LIS3DH.cpp:173
uint8_t spixfer(uint8_t x=0xFF)
Low level SPI.
Definition: Adafruit_CPlay_LIS3DH.cpp:408
float z_g
Definition: Adafruit_CPlay_LIS3DH.h:377
void setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit=10, uint8_t timelatency=20, uint8_t timewindow=255)
Set INT to output for single or double click.
Definition: Adafruit_CPlay_LIS3DH.cpp:281
lis3dh_range_t
Definition: Adafruit_CPlay_LIS3DH.h:311
void setDataRate(lis3dh_dataRate_t dataRate)
Sets the data rate for the LIS3DH (controls power consumption)
Definition: Adafruit_CPlay_LIS3DH.cpp:342
lis3dh_range_t getRange(void)
Gets the g range for the accelerometer.
Definition: Adafruit_CPlay_LIS3DH.cpp:332