source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/Adafruit_CPlay_LIS3DH.cpp

Last change on this file was 3, checked in by markus, 7 years ago

Setup project structure

File size: 14.3 KB
Line 
1/**************************************************************************/
2/*!
3    @file     Adafruit_CPlay_LIS3DH.cpp
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#if ARDUINO >= 100
19 #include "Arduino.h"
20#else
21 #include "WProgram.h"
22#endif
23
24#include <Wire.h>
25#include <Adafruit_CPlay_LIS3DH.h>
26
27#ifdef __SAMD21G18A__
28 #define Wire Wire1 // CP Express has LIS on Wire1 bus
29#endif
30
31/**************************************************************************/
32/*!
33    @brief  Instantiates a new LIS3DH class in I2C mode
34*/
35/**************************************************************************/
36Adafruit_CPlay_LIS3DH::Adafruit_CPlay_LIS3DH()
37  : _cs(-1), _mosi(-1), _miso(-1), _sck(-1), _sensorID(-1)
38{
39}
40
41/**************************************************************************/
42/*!
43    @brief  Instantiates a new LIS3DH class in hardware SPI mode
44    @param cspin the pin used as a chip select
45*/
46/**************************************************************************/
47Adafruit_CPlay_LIS3DH::Adafruit_CPlay_LIS3DH(int8_t cspin)
48  : _cs(cspin), _mosi(-1), _miso(-1), _sck(-1), _sensorID(-1)
49{ }
50
51/**************************************************************************/
52/*!
53    @brief  Instantiates a new LIS3DH class in software SPI mode
54    @param cspin the pin used as a chip select
55    @param mosipin the pin used as master-out-slave-in
56    @param misopin the pin used as master-in-slave-out
57    @param sckpin the pin used as SPI serial clock
58*/
59/**************************************************************************/
60Adafruit_CPlay_LIS3DH::Adafruit_CPlay_LIS3DH(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin)
61  : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin), _sensorID(-1)
62{ }
63
64
65
66/**************************************************************************/
67/*!
68    @brief  Setups the HW (reads coefficients values, etc.)
69    @param i2caddr the I2C address the device can be found on. defaults to 0x18
70    @return true on success, false on any failure
71*/
72/**************************************************************************/
73bool Adafruit_CPlay_LIS3DH::begin(uint8_t i2caddr) {
74  _i2caddr = i2caddr;
75
76  if (_cs == -1) {
77    // i2c
78    Wire.begin();
79  } else {
80    digitalWrite(_cs, HIGH);
81    pinMode(_cs, OUTPUT);
82
83#ifndef __AVR_ATtiny85__
84    if (_sck == -1) {
85      // hardware SPI
86      SPI.begin();
87    } else {
88      // software SPI
89      pinMode(_sck, OUTPUT);
90      pinMode(_mosi, OUTPUT);
91      pinMode(_miso, INPUT);
92    }
93#endif
94  }
95
96  /*
97  for (uint8_t i=0; i<0x30; i++) {
98    Serial.print("$");
99    Serial.print(i, HEX); Serial.print(" = 0x");
100    Serial.println(readRegister8(i), HEX);
101  }
102  */
103
104  /* Check connection */
105  uint8_t deviceid = readRegister8(LIS3DH_REG_WHOAMI);
106  if (deviceid != 0x33)
107  {
108    /* No LIS3DH detected ... return false */
109    //Serial.println(deviceid, HEX);
110    return false;
111  }
112
113  // enable all axes, normal mode
114  writeRegister8(LIS3DH_REG_CTRL1, 0x07);
115  // 400Hz rate
116  setDataRate(LIS3DH_DATARATE_400_HZ);
117
118  // High res & BDU enabled
119  writeRegister8(LIS3DH_REG_CTRL4, 0x88);
120
121  // DRDY on INT1
122  writeRegister8(LIS3DH_REG_CTRL3, 0x10);
123
124  // Turn on orientation config
125  //writeRegister8(LIS3DH_REG_PL_CFG, 0x40);
126
127  // enable adcs
128  writeRegister8(LIS3DH_REG_TEMPCFG, 0x80);
129
130  /*
131  for (uint8_t i=0; i<0x30; i++) {
132    Serial.print("$");
133    Serial.print(i, HEX); Serial.print(" = 0x");
134    Serial.println(readRegister8(i), HEX);
135  }
136  */
137
138  return true;
139}
140
141/**************************************************************************/
142/*!
143    @brief  read x, y, and z axis data and store in class variables.
144*/
145/**************************************************************************/
146void Adafruit_CPlay_LIS3DH::read(void) {
147  // read x y z at once
148
149  if (_cs == -1) {
150    // i2c
151    Wire.beginTransmission(_i2caddr);
152    Wire.write(LIS3DH_REG_OUT_X_L | 0x80); // 0x80 for autoincrement
153    Wire.endTransmission();
154
155    Wire.requestFrom(_i2caddr, 6);
156    x = Wire.read(); x |= ((uint16_t)Wire.read()) << 8;
157    y = Wire.read(); y |= ((uint16_t)Wire.read()) << 8;
158    z = Wire.read(); z |= ((uint16_t)Wire.read()) << 8;
159  } 
160  #ifndef __AVR_ATtiny85__
161  else {
162#if SPI_HAS_TRANSACTION
163    if (_sck == -1)
164      SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
165#endif
166    digitalWrite(_cs, LOW);
167    spixfer(LIS3DH_REG_OUT_X_L | 0x80 | 0x40); // read multiple, bit 7&6 high
168
169    x = spixfer(); x |= ((uint16_t)spixfer()) << 8;
170    y = spixfer(); y |= ((uint16_t)spixfer()) << 8;
171    z = spixfer(); z |= ((uint16_t)spixfer()) << 8;
172
173    digitalWrite(_cs, HIGH);
174#if SPI_HAS_TRANSACTION
175    if (_sck == -1)
176      SPI.endTransaction();              // release the SPI bus
177#endif
178  }
179  #endif
180  uint8_t range = getRange();
181  uint16_t divider = 1;
182  if (range == LIS3DH_RANGE_16_G) divider = 1365;
183  if (range == LIS3DH_RANGE_8_G) divider = 4096;
184  if (range == LIS3DH_RANGE_4_G) divider = 8190;
185  if (range == LIS3DH_RANGE_2_G) divider = 16380;
186
187  x_g = (float)x / divider;
188  y_g = (float)y / divider;
189  z_g = (float)z / divider;
190
191}
192
193/**************************************************************************/
194/*!
195    @brief  Read the auxilary ADC
196    @param adc the adc number to read. Valid adc numbers are 1, 2, and 3.
197    @return the measured value
198*/
199/**************************************************************************/
200int16_t Adafruit_CPlay_LIS3DH::readADC(uint8_t adc) {
201  if ((adc < 1) || (adc > 3)) return 0;
202  uint16_t value;
203
204  adc--;
205
206  uint8_t reg = LIS3DH_REG_OUTADC1_L + adc*2;
207
208  if (_cs == -1) {
209    // i2c
210    Wire.beginTransmission(_i2caddr);
211    Wire.write(reg | 0x80);   // 0x80 for autoincrement
212    Wire.endTransmission();
213    Wire.requestFrom(_i2caddr, 2);
214    value = Wire.read();  value |= ((uint16_t)Wire.read()) << 8;
215  } 
216  #ifndef __AVR_ATtiny85__
217  else {
218#if SPI_HAS_TRANSACTION
219    if (_sck == -1)
220      SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
221#endif
222    digitalWrite(_cs, LOW);
223    spixfer(reg | 0x80 | 0x40); // read multiple, bit 7&6 high
224
225    value = spixfer(); value |= ((uint16_t)spixfer()) << 8;
226
227    digitalWrite(_cs, HIGH);
228#if SPI_HAS_TRANSACTION
229    if (_sck == -1)
230      SPI.endTransaction();              // release the SPI bus
231#endif
232  }
233  #endif
234
235  return value;
236}
237
238
239/**************************************************************************/
240/*!
241    @brief  Set INT to output for single or double click
242    @param c the number of taps to detect. pass 1 for single tap on all axis, 2 for double tap on all axis. Pass 0 to disable taps.
243    @param clickthresh the threshold over which to register a tap.
244    @param timelimit Optional time limit for a tap. Defaults to 10
245    @param timelatency Optional tap latency. defaults to 20
246    @param timewindow Optional time window. defaults to 255
247*/
248/**************************************************************************/
249void Adafruit_CPlay_LIS3DH::setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit, uint8_t timelatency, uint8_t timewindow) {
250  if (!c) {
251    //disable int
252    uint8_t r = readRegister8(LIS3DH_REG_CTRL3);
253    r &= ~(0x80); // turn off I1_CLICK
254    writeRegister8(LIS3DH_REG_CTRL3, r);
255    writeRegister8(LIS3DH_REG_CLICKCFG, 0);
256    return;
257  }
258  // else...
259
260  writeRegister8(LIS3DH_REG_CTRL3, 0x80); // turn on int1 click
261  writeRegister8(LIS3DH_REG_CTRL5, 0x08); // latch interrupt on int1
262
263  if (c == 1)
264    writeRegister8(LIS3DH_REG_CLICKCFG, 0x15); // turn on all axes & singletap
265  if (c == 2)
266    writeRegister8(LIS3DH_REG_CLICKCFG, 0x2A); // turn on all axes & doubletap
267
268
269  writeRegister8(LIS3DH_REG_CLICKTHS, clickthresh); // arbitrary
270  writeRegister8(LIS3DH_REG_TIMELIMIT, timelimit); // arbitrary
271  writeRegister8(LIS3DH_REG_TIMELATENCY, timelatency); // arbitrary
272  writeRegister8(LIS3DH_REG_TIMEWINDOW, timewindow); // arbitrary
273}
274
275/**************************************************************************/
276/*!
277    @brief  get a tap reading
278    @return the tap reading
279*/
280/**************************************************************************/
281uint8_t Adafruit_CPlay_LIS3DH::getClick(void) {
282  return readRegister8(LIS3DH_REG_CLICKSRC);
283}
284
285
286/**************************************************************************/
287/*!
288    @brief  Sets the g range for the accelerometer
289    @param range the desired range of the device. Pass LIS3DH_RANGE_2_G for highest sensitivity
290      and lowest max/min value (+-2G), LIS3DH_RANGE_16_G for lowest sensitivity and highest max/min value (+-16G).
291      Other acceptable values are LIS3DH_RANGE_4_G and LIS3DH_RANGE_8_G.
292*/
293/**************************************************************************/
294void Adafruit_CPlay_LIS3DH::setRange(lis3dh_range_t range)
295{
296  uint8_t r = readRegister8(LIS3DH_REG_CTRL4);
297  r &= ~(0x30);
298  r |= range << 4;
299  writeRegister8(LIS3DH_REG_CTRL4, r);
300}
301
302/**************************************************************************/
303/*!
304    @brief  Sets the g range for the accelerometer
305    @return the range reading from the sensor.
306*/
307/**************************************************************************/
308lis3dh_range_t Adafruit_CPlay_LIS3DH::getRange(void)
309{
310  /* Read the data format register to preserve bits */
311  return (lis3dh_range_t)((readRegister8(LIS3DH_REG_CTRL4) >> 4) & 0x03);
312}
313
314/**************************************************************************/
315/*!
316    @brief  Sets the data rate for the LIS3DH (controls power consumption)
317    @param dataRate the desired datarate.
318*/
319/**************************************************************************/
320void Adafruit_CPlay_LIS3DH::setDataRate(lis3dh_dataRate_t dataRate)
321{
322  uint8_t ctl1 = readRegister8(LIS3DH_REG_CTRL1);
323  ctl1 &= ~(0xF0); // mask off bits
324  ctl1 |= (dataRate << 4);
325  writeRegister8(LIS3DH_REG_CTRL1, ctl1);
326}
327
328/**************************************************************************/
329/*!
330    @brief  Sets the data rate for the LIS3DH (controls power consumption)
331    @return the data rate reading from the sensor.
332*/
333/**************************************************************************/
334lis3dh_dataRate_t Adafruit_CPlay_LIS3DH::getDataRate(void)
335{
336  return (lis3dh_dataRate_t)((readRegister8(LIS3DH_REG_CTRL1) >> 4)& 0x0F);
337}
338
339/**************************************************************************/
340/*!
341    @brief  Gets the most recent sensor event
342    @param event the pointer to place the event reading in
343    @return none
344*/
345/**************************************************************************/
346bool Adafruit_CPlay_LIS3DH::getEvent(sensors_event_t *event) {
347  /* Clear the event */
348  memset(event, 0, sizeof(sensors_event_t));
349
350  event->version   = sizeof(sensors_event_t);
351  event->sensor_id = _sensorID;
352  event->type      = SENSOR_TYPE_ACCELEROMETER;
353  event->timestamp = 0;
354
355  read();
356
357  event->acceleration.x = x_g * SENSORS_GRAVITY_STANDARD;
358  event->acceleration.y = y_g * SENSORS_GRAVITY_STANDARD;
359  event->acceleration.z = z_g * SENSORS_GRAVITY_STANDARD;
360}
361
362/**************************************************************************/
363/*!
364    @brief  Gets the sensor_t data
365    @param sensor the pointer to place sensor information into.
366*/
367/**************************************************************************/
368void Adafruit_CPlay_LIS3DH::getSensor(sensor_t *sensor) {
369  /* Clear the sensor_t object */
370  memset(sensor, 0, sizeof(sensor_t));
371
372  /* Insert the sensor name in the fixed length char array */
373  strncpy (sensor->name, "LIS3DH", sizeof(sensor->name) - 1);
374  sensor->name[sizeof(sensor->name)- 1] = 0;
375  sensor->version     = 1;
376  sensor->sensor_id   = _sensorID;
377  sensor->type        = SENSOR_TYPE_ACCELEROMETER;
378  sensor->min_delay   = 0;
379  sensor->max_value   = 0;
380  sensor->min_value   = 0;
381  sensor->resolution  = 0;
382}
383
384
385/**************************************************************************/
386/*!
387    @brief  Low level SPI
388    @param x the byte to transfer
389    @return the read byte
390*/
391/**************************************************************************/
392
393uint8_t Adafruit_CPlay_LIS3DH::spixfer(uint8_t x) {
394  #ifndef __AVR_ATtiny85__
395  if (_sck == -1)
396    return SPI.transfer(x);
397
398  // software spi
399  //Serial.println("Software SPI");
400  uint8_t reply = 0;
401  for (int i=7; i>=0; i--) {
402    reply <<= 1;
403    digitalWrite(_sck, LOW);
404    digitalWrite(_mosi, x & (1<<i));
405    digitalWrite(_sck, HIGH);
406    if (digitalRead(_miso))
407      reply |= 1;
408  }
409  return reply;
410  #endif
411}
412
413
414/**************************************************************************/
415/*!
416    @brief  Writes 8-bits to the specified destination register
417*/
418/**************************************************************************/
419void Adafruit_CPlay_LIS3DH::writeRegister8(uint8_t reg, uint8_t value) {
420  if (_cs == -1) {
421    Wire.beginTransmission((uint8_t)_i2caddr);
422    Wire.write((uint8_t)reg);
423    Wire.write((uint8_t)value);
424    Wire.endTransmission();
425  } 
426  #ifndef __AVR_ATtiny85__
427  else {
428#if SPI_HAS_TRANSACTION
429    if (_sck == -1)
430      SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
431#endif
432    digitalWrite(_cs, LOW);
433    spixfer(reg & ~0x80); // write, bit 7 low
434    spixfer(value);
435    digitalWrite(_cs, HIGH);
436#if SPI_HAS_TRANSACTION
437    if (_sck == -1)
438      SPI.endTransaction();              // release the SPI bus
439#endif
440  }
441  #endif
442}
443
444/**************************************************************************/
445/*!
446    @brief  Reads 8-bits from the specified register
447*/
448/**************************************************************************/
449uint8_t Adafruit_CPlay_LIS3DH::readRegister8(uint8_t reg) {
450  uint8_t value;
451
452  if (_cs == -1) {
453    Wire.beginTransmission(_i2caddr);
454    Wire.write((uint8_t)reg);
455    Wire.endTransmission();
456
457    Wire.requestFrom(_i2caddr, 1);
458    value = Wire.read();
459  } 
460  #ifndef __AVR_ATtiny85__
461  else {
462#if SPI_HAS_TRANSACTION
463    if (_sck == -1)
464      SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
465#endif
466    digitalWrite(_cs, LOW);
467    spixfer(reg | 0x80); // read, bit 7 high
468    value = spixfer(0);
469    digitalWrite(_cs, HIGH);
470#if SPI_HAS_TRANSACTION
471    if (_sck == -1)
472      SPI.endTransaction();              // release the SPI bus
473#endif
474  }
475  #endif
476  return value;
477}
Note: See TracBrowser for help on using the repository browser.