diff --git a/src/LIDARLite_v3HP.h b/src/LIDARLite_v3HP.h index c629274..e98464c 100644 --- a/src/LIDARLite_v3HP.h +++ b/src/LIDARLite_v3HP.h @@ -25,6 +25,8 @@ #ifndef LIDARLite_v3HP_h #define LIDARLite_v3HP_h +#define LEGACY_I2C 1 + #define LIDARLITE_ADDR_DEFAULT 0x62 #include diff --git a/src/LIDARLite_v4LED.cpp b/src/LIDARLite_v4LED.cpp index f6fef9c..12a4139 100644 --- a/src/LIDARLite_v4LED.cpp +++ b/src/LIDARLite_v4LED.cpp @@ -29,6 +29,32 @@ #include #include "LIDARLite_v4LED.h" +/*------------------------------------------------------------------------------ + Constructor + + Default constructor, the object will use the default Wire object. + + Parameters + ------------------------------------------------------------------------------ + None +------------------------------------------------------------------------------*/ +LIDARLite_v4LED::LIDARLite_v4LED(){ + i2cPort = &Wire; +} + +/*------------------------------------------------------------------------------ + Constructor + + Constructor used to point the I2C communication to another TwoWire object + + Parameters + ------------------------------------------------------------------------------ + port: TwoWire pointer, it can be use to select wich I2C port will be used. +------------------------------------------------------------------------------*/ +LIDARLite_v4LED::LIDARLite_v4LED(TwoWire *port){ + i2cPort = port; +} + /*------------------------------------------------------------------------------ Configure @@ -319,16 +345,16 @@ void LIDARLite_v4LED::write(uint8_t regAddr, uint8_t * dataBytes, { uint8_t nackCatcher; - Wire.beginTransmission(lidarliteAddress); + i2cPort->beginTransmission(lidarliteAddress); // First byte of every write sets the LidarLite's internal register address pointer - Wire.write(regAddr); + i2cPort->write(regAddr); // Subsequent bytes are data writes - Wire.write(dataBytes, numBytes); + i2cPort->write(dataBytes, numBytes); // A nack means the device is not responding. Report the error over serial. - nackCatcher = Wire.endTransmission(); + nackCatcher = i2cPort->endTransmission(); if (nackCatcher != 0) { // handle nack issues in here @@ -390,17 +416,17 @@ void LIDARLite_v4LED::read(uint8_t regAddr, uint8_t * dataBytes, #define SEND_STOP ((uint8_t) true) #define DONT_STOP ((uint8_t) false) - Wire.beginTransmission(lidarliteAddress); - Wire.write(regAddr); + i2cPort->beginTransmission(lidarliteAddress); + i2cPort->write(regAddr); // A nack means the device is not responding, report the error over serial - if (Wire.endTransmission(DONT_STOP)) // performs repeated start + if (i2cPort->endTransmission(DONT_STOP)) // performs repeated start { Serial.println("> nack"); } // Perform read, save in dataBytes array - Wire.requestFrom(lidarliteAddress, numBytes, SEND_STOP); + i2cPort->requestFrom(lidarliteAddress, numBytes, SEND_STOP); #else @@ -416,7 +442,7 @@ void LIDARLite_v4LED::read(uint8_t regAddr, uint8_t * dataBytes, // parameters to "requestFrom" see function header comments above // ************************************************************** - Wire.requestFrom + i2cPort->requestFrom ( lidarliteAddress, // Slave address numBytes, // number of consecutive bytes to read @@ -427,12 +453,12 @@ void LIDARLite_v4LED::read(uint8_t regAddr, uint8_t * dataBytes, #endif - uint8_t numHere = Wire.available(); + uint8_t numHere = i2cPort->available(); uint8_t i = 0; while (i < numHere) { - dataBytes[i] = Wire.read(); + dataBytes[i] = i2cPort->read(); i++; } diff --git a/src/LIDARLite_v4LED.h b/src/LIDARLite_v4LED.h index f753d6e..45d3304 100644 --- a/src/LIDARLite_v4LED.h +++ b/src/LIDARLite_v4LED.h @@ -25,6 +25,8 @@ #ifndef LIDARLite_v4LED_h #define LIDARLite_v4LED_h +#define LEGACY_I2C 1 + #define LIDARLITE_ADDR_DEFAULT 0x62 #include @@ -33,6 +35,8 @@ class LIDARLite_v4LED { public: + LIDARLite_v4LED(); + LIDARLite_v4LED(TwoWire *port); void configure (uint8_t configuration = 0, uint8_t lidarliteAddress = LIDARLITE_ADDR_DEFAULT); void setI2Caddr (uint8_t newAddress, uint8_t disableDefault, uint8_t lidarliteAddress = LIDARLITE_ADDR_DEFAULT); @@ -48,6 +52,8 @@ class LIDARLite_v4LED void read (uint8_t regAddr, uint8_t * dataBytes, uint8_t numBytes, uint8_t lidarliteAddress = LIDARLITE_ADDR_DEFAULT); void correlationRecordRead (int16_t * correlationArray, uint8_t numberOfReadings = 192, uint8_t lidarliteAddress = LIDARLITE_ADDR_DEFAULT); + private: + TwoWire *i2cPort; }; #endif