LynnL4 5 лет назад
Родитель
Сommit
47e2d7ef5d

+ 46 - 49
examples/LIS3DHTR_IIC/LIS3DHTR_IIC.ino

@@ -1,55 +1,52 @@
 // This example use I2C.
-// if you use the software I2C to drive the sensor, you can uncommnet the define SOFTWAREWIRE which in LIS3DHTR.h.
-
 #include "LIS3DHTR.h"
-#ifdef SOFTWAREWIRE
-  #include <SoftwareWire.h>
-  SoftwareWire myWire(3, 2);
-  LIS3DHTR<SoftwareWire> LIS;//IIC
-  #define WIRE myWire
-#else
-  #include <Wire.h>
-  LIS3DHTR<TwoWire> LIS;//IIC
-  #define WIRE Wire
-#endif
-
+#include <Wire.h>
+LIS3DHTR<TwoWire> LIS; //IIC
+#define WIRE Wire
 
-void setup() {
-    Serial.begin(115200);
-    while (!Serial) {};
-    LIS.begin(WIRE); //IIC init
-    LIS.openTemp();//If ADC3 is used, the temperature detection needs to be turned off.
-    //  LIS.closeTemp();//default
-    delay(100);
-    //  LIS.setFullScaleRange(LIS3DHTR_RANGE_2G);
-    //  LIS.setFullScaleRange(LIS3DHTR_RANGE_4G);
-    //  LIS.setFullScaleRange(LIS3DHTR_RANGE_8G);
-    //  LIS.setFullScaleRange(LIS3DHTR_RANGE_16G);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_1HZ);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_10HZ);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
-    LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_100HZ);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_200HZ);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_1_6KHZ);
-    //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_5KHZ);
+void setup()
+{
+  Serial.begin(115200);
+  while (!Serial)
+  {
+  };
+  LIS.begin(WIRE); //IIC init
+  LIS.openTemp();  //If ADC3 is used, the temperature detection needs to be turned off.
+  //  LIS.closeTemp();//default
+  delay(100);
+  //  LIS.setFullScaleRange(LIS3DHTR_RANGE_2G);
+  //  LIS.setFullScaleRange(LIS3DHTR_RANGE_4G);
+  //  LIS.setFullScaleRange(LIS3DHTR_RANGE_8G);
+  //  LIS.setFullScaleRange(LIS3DHTR_RANGE_16G);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_1HZ);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_10HZ);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
+  LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_100HZ);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_200HZ);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_1_6KHZ);
+  //  LIS.setOutputDataRate(LIS3DHTR_DATARATE_5KHZ);
 }
-void loop() {
-    if (!LIS) {
-        Serial.println("LIS3DHTR didn't connect.");
-        while (1);
-        return;
-    }
-    //3 axis
-    //  Serial.print("x:"); Serial.print(LIS.getAccelerationX()); Serial.print("  ");
-    //  Serial.print("y:"); Serial.print(LIS.getAccelerationY()); Serial.print("  ");
-    //  Serial.print("z:"); Serial.println(LIS.getAccelerationZ());
-    //ADC
-    //    Serial.print("adc1:"); Serial.println(LIS.readbitADC1());
-    //    Serial.print("adc2:"); Serial.println(LIS.readbitADC2());
-    //    Serial.print("adc3:"); Serial.println(LIS.readbitADC3());
+void loop()
+{
+  if (!LIS)
+  {
+    Serial.println("LIS3DHTR didn't connect.");
+    while (1)
+      ;
+    return;
+  }
+  //3 axis
+  //  Serial.print("x:"); Serial.print(LIS.getAccelerationX()); Serial.print("  ");
+  //  Serial.print("y:"); Serial.print(LIS.getAccelerationY()); Serial.print("  ");
+  //  Serial.print("z:"); Serial.println(LIS.getAccelerationZ());
+  //ADC
+  //    Serial.print("adc1:"); Serial.println(LIS.readbitADC1());
+  //    Serial.print("adc2:"); Serial.println(LIS.readbitADC2());
+  //    Serial.print("adc3:"); Serial.println(LIS.readbitADC3());
 
-    //temperature
-    Serial.print("temp:"); Serial.println(LIS.getTemperature());
-    delay(500);
+  //temperature
+  Serial.print("temp:");
+  Serial.println(LIS.getTemperature());
+  delay(500);
 }

+ 3 - 12
examples/LIS3DHTR_IIC_ADC/LIS3DHTR_IIC_ADC.ino

@@ -1,17 +1,8 @@
 // This example USES the ADC function.
-// if you use the software I2C to drive the sensor, you can uncommnet the define SOFTWAREWIRE which in LIS3DHTR.h.
-
 #include "LIS3DHTR.h"
-#ifdef SOFTWAREWIRE
-  #include <SoftwareWire.h>
-  SoftwareWire myWire(3, 2);
-  LIS3DHTR<SoftwareWire> LIS;//IIC
-  #define WIRE myWire
-#else
-  #include <Wire.h>
-  LIS3DHTR<TwoWire> LIS;//IIC
-  #define WIRE Wire
-#endif
+#include <Wire.h>
+LIS3DHTR<TwoWire> LIS; //IIC
+#define WIRE Wire
 
 
 void setup() {

+ 3 - 13
examples/LIS3DHTR_IIC_AcceleratorReadings/LIS3DHTR_IIC_AcceleratorReadings.ino

@@ -1,18 +1,8 @@
 // This example shows the 3 axis acceleration.
-// if you use the software I2C to drive the sensor, you can uncommnet the define SOFTWAREWIRE which in LIS3DHTR.h.
-
 #include "LIS3DHTR.h"
-#ifdef SOFTWAREWIRE
-  #include <SoftwareWire.h>
-  SoftwareWire myWire(3, 2);
-  LIS3DHTR<SoftwareWire> LIS;//IIC
-  #define WIRE myWire
-#else
-  #include <Wire.h>
-  LIS3DHTR<TwoWire> LIS;//IIC
-  #define WIRE Wire
-#endif
-
+#include <Wire.h>
+LIS3DHTR<TwoWire> LIS; //IIC
+#define WIRE Wire
 
 void setup() {
     Serial.begin(115200);

+ 54 - 52
src/LIS3DHTR.cpp

@@ -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

+ 8 - 15
src/LIS3DHTR.h

@@ -30,15 +30,9 @@
     #include "WProgram.h"
 #endif
 
-//#define LIS_SPI_MODE
-
-#ifdef LIS_SPI_MODE
-#include <SPI.h>
-#elif defined SOFTWAREWIRE
-#include <SoftwareWire.h>
-#else
 #include <Wire.h>
-#endif
+#include <SPI.h>
+
 
 #ifndef SEEED_LIS3DHTR_H
 #define SEEED_LIS3DHTR_H
@@ -200,13 +194,11 @@ public:
     LIS3DHTR();
 
     bool isConnection(void);
-#ifdef LIS_SPI_MODE
-    void begin(T &comm = SPI, uint8_t sspin = SS); //init
-    void begin(uint8_t sspin) { begin(SPI, sspin); };
-#else
-    void begin(T &comm = Wire, uint8_t address = LIS3DHTR_DEFAULT_ADDRESS); //init
+
+    void begin(SPIClass &comm = SPI, uint8_t sspin = SS); //init
+    void begin(TwoWire &comm = Wire, uint8_t address = LIS3DHTR_DEFAULT_ADDRESS); //init
     void begin(uint8_t address) { begin(Wire, address); };
-#endif
+
 
     void setPoweMode(power_type_t mode);
     void setFullScaleRange(scale_type_t range);
@@ -242,7 +234,8 @@ private:
     int16_t accRange;
     uint8_t commInterface;
     uint8_t chipSelectPin;
-    T *_comm;
+    SPIClass *_spi_com;
+    TwoWire *_wire_com;
 };
 
 #endif /*SEEED_LIS3DHTR_H*/