Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new constructor to be possible select witch Wire object will be used #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/LIDARLite_v3HP.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef LIDARLite_v3HP_h
#define LIDARLite_v3HP_h

#define LEGACY_I2C 1

#define LIDARLITE_ADDR_DEFAULT 0x62

#include <Arduino.h>
Expand Down
48 changes: 37 additions & 11 deletions src/LIDARLite_v4LED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@
#include <stdint.h>
#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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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++;
}

Expand Down
6 changes: 6 additions & 0 deletions src/LIDARLite_v4LED.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef LIDARLite_v4LED_h
#define LIDARLite_v4LED_h

#define LEGACY_I2C 1

#define LIDARLITE_ADDR_DEFAULT 0x62

#include <Arduino.h>
Expand All @@ -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);
Expand All @@ -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