|
|
@@ -28,30 +28,31 @@
|
|
|
*/
|
|
|
|
|
|
#include "LIS3DHTR.h"
|
|
|
-
|
|
|
template <class T>
|
|
|
+
|
|
|
LIS3DHTR<T>::LIS3DHTR()
|
|
|
{
|
|
|
+
|
|
|
}
|
|
|
|
|
|
-#ifdef LIS_SPI_MODE
|
|
|
template <class T>
|
|
|
-void LIS3DHTR<T>::begin(T &comm, uint8_t sspin)
|
|
|
+void LIS3DHTR<T>::begin(SPIClass &comm, uint8_t sspin)
|
|
|
{
|
|
|
chipSelectPin = sspin;
|
|
|
- _comm = &comm;
|
|
|
+ _spi_com = &comm;
|
|
|
+ _wire_com = NULL;
|
|
|
pinMode(chipSelectPin, OUTPUT);
|
|
|
digitalWrite(chipSelectPin, HIGH);
|
|
|
// start the SPI library:
|
|
|
- _comm->begin();
|
|
|
+ _spi_com->begin();
|
|
|
// Maximum SPI frequency is 10MHz, could divide by 2 here:
|
|
|
- _comm->setClockDivider(SPI_CLOCK_DIV4);
|
|
|
+ _spi_com->setClockDivider(SPI_CLOCK_DIV4);
|
|
|
// Data is read and written MSb first.
|
|
|
- _comm->setBitOrder(MSBFIRST);
|
|
|
+ _spi_com->setBitOrder(MSBFIRST);
|
|
|
// Data is captured on rising edge of clock (CPHA = 0)
|
|
|
// Base value of the clock is HIGH (CPOL = 1)
|
|
|
// MODE3 for 328p operation
|
|
|
- _comm->setDataMode(SPI_MODE3);
|
|
|
+ _spi_com->setDataMode(SPI_MODE3);
|
|
|
|
|
|
delay(200);
|
|
|
|
|
|
@@ -82,13 +83,13 @@ void LIS3DHTR<T>::begin(T &comm, uint8_t sspin)
|
|
|
setFullScaleRange(LIS3DHTR_RANGE_16G);
|
|
|
setOutputDataRate(LIS3DHTR_DATARATE_1HZ);
|
|
|
}
|
|
|
-#else
|
|
|
|
|
|
template <class T>
|
|
|
-void LIS3DHTR<T>::begin(T &wire, uint8_t address)
|
|
|
+void LIS3DHTR<T>::begin(TwoWire &wire, uint8_t address)
|
|
|
{
|
|
|
- _comm = &wire;
|
|
|
- _comm->begin();
|
|
|
+ _wire_com = &wire;
|
|
|
+ _wire_com->begin();
|
|
|
+ _spi_com = NULL;
|
|
|
devAddr = address;
|
|
|
|
|
|
uint8_t config5 = LIS3DHTR_REG_TEMP_ADC_PD_ENABLED |
|
|
|
@@ -119,7 +120,7 @@ void LIS3DHTR<T>::begin(T &wire, uint8_t address)
|
|
|
setFullScaleRange(LIS3DHTR_RANGE_16G);
|
|
|
setOutputDataRate(LIS3DHTR_DATARATE_1HZ);
|
|
|
}
|
|
|
-#endif
|
|
|
+
|
|
|
template <class T>
|
|
|
void LIS3DHTR<T>::openTemp()
|
|
|
{
|
|
|
@@ -353,17 +354,20 @@ uint16_t LIS3DHTR<T>::readbitADC3(void)
|
|
|
template <class T>
|
|
|
void LIS3DHTR<T>::writeRegister(uint8_t reg, uint8_t val)
|
|
|
{
|
|
|
-#ifdef LIS_SPI_MODE
|
|
|
- digitalWrite(chipSelectPin, LOW);
|
|
|
- _comm->transfer(reg);
|
|
|
- _comm->transfer(val);
|
|
|
- digitalWrite(chipSelectPin, HIGH);
|
|
|
-#else
|
|
|
- _comm->beginTransmission(devAddr);
|
|
|
- _comm->write(reg);
|
|
|
- _comm->write(val);
|
|
|
- _comm->endTransmission();
|
|
|
-#endif
|
|
|
+ if (_spi_com != NULL)
|
|
|
+ {
|
|
|
+ digitalWrite(chipSelectPin, LOW);
|
|
|
+ _spi_com->transfer(reg);
|
|
|
+ _spi_com->transfer(val);
|
|
|
+ digitalWrite(chipSelectPin, HIGH);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _wire_com->beginTransmission(devAddr);
|
|
|
+ _wire_com->write(reg);
|
|
|
+ _wire_com->write(val);
|
|
|
+ _wire_com->endTransmission();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
template <class T>
|
|
|
@@ -375,33 +379,36 @@ void LIS3DHTR<T>::readRegisterRegion(uint8_t *outputPointer, uint8_t reg, uint8_
|
|
|
uint8_t c = 0;
|
|
|
uint8_t tempFFCounter = 0;
|
|
|
|
|
|
-#ifdef LIS_SPI_MODE:
|
|
|
- digitalWrite(chipSelectPin, LOW);
|
|
|
- _comm->transfer(reg | 0x80 | 0x40); //Ored with "read request" bit and "auto increment" bit
|
|
|
- while (i < length) // slave may send less than requested
|
|
|
+ if (_spi_com != NULL)
|
|
|
{
|
|
|
- c = _comm->transfer(0x00); // receive a byte as character
|
|
|
- *outputPointer = c;
|
|
|
- outputPointer++;
|
|
|
- i++;
|
|
|
+ digitalWrite(chipSelectPin, LOW);
|
|
|
+ _spi_com->transfer(reg | 0x80 | 0x40); //Ored with "read request" bit and "auto increment" bit
|
|
|
+ while (i < length) // slave may send less than requested
|
|
|
+ {
|
|
|
+ c = _spi_com->transfer(0x00); // receive a byte as character
|
|
|
+ *outputPointer = c;
|
|
|
+ outputPointer++;
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ digitalWrite(chipSelectPin, HIGH);
|
|
|
}
|
|
|
- digitalWrite(chipSelectPin, HIGH);
|
|
|
-#else
|
|
|
-
|
|
|
- _comm->beginTransmission(devAddr);
|
|
|
- reg |= 0x80; //turn auto-increment bit on, bit 7 for I2C
|
|
|
- _comm->write(reg);
|
|
|
- _comm->endTransmission();
|
|
|
- _comm->requestFrom(devAddr, length);
|
|
|
-
|
|
|
- while ((_comm->available()) && (i < length)) // slave may send less than requested
|
|
|
+ else
|
|
|
{
|
|
|
- c = _comm->read(); // receive a byte as character
|
|
|
- *outputPointer = c;
|
|
|
- outputPointer++;
|
|
|
- i++;
|
|
|
+
|
|
|
+ _wire_com->beginTransmission(devAddr);
|
|
|
+ reg |= 0x80; //turn auto-increment bit on, bit 7 for I2C
|
|
|
+ _wire_com->write(reg);
|
|
|
+ _wire_com->endTransmission();
|
|
|
+ _wire_com->requestFrom(devAddr, length);
|
|
|
+
|
|
|
+ while ((_wire_com->available()) && (i < length)) // slave may send less than requested
|
|
|
+ {
|
|
|
+ c = _wire_com->read(); // receive a byte as character
|
|
|
+ *outputPointer = c;
|
|
|
+ outputPointer++;
|
|
|
+ i++;
|
|
|
+ }
|
|
|
}
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
template <class T>
|
|
|
@@ -457,10 +464,5 @@ void LIS3DHTR<T>::click(uint8_t c, uint8_t click_thresh, uint8_t limit, uint8_t
|
|
|
template <class T>
|
|
|
LIS3DHTR<T>::operator bool() { return isConnection(); }
|
|
|
|
|
|
-#ifdef LIS_SPI_MODE
|
|
|
template class LIS3DHTR<SPIClass>;
|
|
|
-#elif defined SOFTWAREWIRE
|
|
|
-template class LIS3DHTR<SoftwareWire>;
|
|
|
-#else
|
|
|
template class LIS3DHTR<TwoWire>;
|
|
|
-#endif
|