From 2088a8909648bad374f29dccfc64bbf8016561e5 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 12 Oct 2022 14:25:56 +0100 Subject: [PATCH 1/3] Add begin(TwoWire &i2cPort) - requires VL53L1X::dev_i2c to be public --- library.properties | 2 +- src/SparkFun_VL53L1X.cpp | 40 ++++++++++++++++++++++++++++++++++++++ src/SparkFun_VL53L1X.h | 7 ++++++- src/st_src/vl53l1x_class.h | 5 ++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index c3e0e97..36652fc 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun VL53L1X 4m Laser Distance Sensor -version=1.2.11 +version=1.2.12 author=SparkFun Electronics maintainer=SparkFun Electronics sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index b540436..38bb6d2 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -33,6 +33,27 @@ #include #include "SparkFun_VL53L1X.h" +SFEVL53L1X::SFEVL53L1X() +{ + _i2cPort = &Wire; + _shutdownPin = -1; + _interruptPin = -1; + _device = new VL53L1X(&Wire, -1, -1); +} +SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort) +{ + _i2cPort = &i2cPort; + _shutdownPin = -1; + _interruptPin = -1; + _device = new VL53L1X(&i2cPort, -1, -1); +} +SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin) +{ + _i2cPort = &i2cPort; + _shutdownPin = shutdownPin; + _interruptPin = -1; + _device = new VL53L1X(&i2cPort, shutdownPin, -1); +} SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) { _i2cPort = &i2cPort; @@ -54,6 +75,25 @@ bool SFEVL53L1X::begin() return _device->VL53L1X_SensorInit(); } +bool SFEVL53L1X::begin(TwoWire &i2cPort) +{ + _i2cPort = &i2cPort; + _device->dev_i2c = &i2cPort; + + if (checkID() == false) + { + Serial.println("Check ID failed..."); + return (VL53L1_ERROR_PLATFORM_SPECIFIC_START); + } + + bool result = _device->VL53L1X_SensorInit() == 0; + + if (!result) + Serial.println("Init failed..."); + + return result; +} + /*Checks the ID of the device, returns true if ID is correct*/ bool SFEVL53L1X::checkID() diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 20fb682..7ae3323 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -59,9 +59,14 @@ struct DetectionConfig class SFEVL53L1X { public: - SFEVL53L1X(TwoWire &i2cPort = Wire, int shutdownPin = -1, int interruptPin = -1); //Constructs our Distance sensor without an interrupt or shutdown pin + //Constructs our Distance sensor + SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined). + SFEVL53L1X(TwoWire &i2cPort); // Set both pins to -1 (undefined). + SFEVL53L1X(TwoWire &i2cPort, int shutdownPin); // Set interrupt pin to -1 (undefined). + SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin); bool init(); //Deprecated version of begin bool begin(); //Initialization of sensor + bool begin(TwoWire &i2cPort); //Initialization of sensor bool checkID(); //Check the ID of the sensor, returns true if ID is correct void sensorOn(); //Toggles shutdown pin to turn sensor on and off void sensorOff(); //Toggles shutdown pin to turn sensor on and off diff --git a/src/st_src/vl53l1x_class.h b/src/st_src/vl53l1x_class.h index 458014b..0af5eea 100644 --- a/src/st_src/vl53l1x_class.h +++ b/src/st_src/vl53l1x_class.h @@ -553,10 +553,13 @@ class VL53L1X : public RangeSensor - protected: + public: /* IO Device */ TwoWire *dev_i2c; + + protected: + /* Digital out pin */ int gpio0; int gpio1Int; From 2a4660a4f6a37c2889c5fd4580f34f2f6b57869e Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 12 Oct 2022 14:52:49 +0100 Subject: [PATCH 2/3] Remove unnecessary overloads --- src/SparkFun_VL53L1X.cpp | 14 -------------- src/SparkFun_VL53L1X.h | 4 +--- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 38bb6d2..980d771 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -40,20 +40,6 @@ SFEVL53L1X::SFEVL53L1X() _interruptPin = -1; _device = new VL53L1X(&Wire, -1, -1); } -SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort) -{ - _i2cPort = &i2cPort; - _shutdownPin = -1; - _interruptPin = -1; - _device = new VL53L1X(&i2cPort, -1, -1); -} -SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin) -{ - _i2cPort = &i2cPort; - _shutdownPin = shutdownPin; - _interruptPin = -1; - _device = new VL53L1X(&i2cPort, shutdownPin, -1); -} SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) { _i2cPort = &i2cPort; diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index 7ae3323..ef2dcc6 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -61,9 +61,7 @@ class SFEVL53L1X public: //Constructs our Distance sensor SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined). - SFEVL53L1X(TwoWire &i2cPort); // Set both pins to -1 (undefined). - SFEVL53L1X(TwoWire &i2cPort, int shutdownPin); // Set interrupt pin to -1 (undefined). - SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin); + SFEVL53L1X(TwoWire &i2cPort, int shutdownPin = -1, int interruptPin = -1); bool init(); //Deprecated version of begin bool begin(); //Initialization of sensor bool begin(TwoWire &i2cPort); //Initialization of sensor From 832c0b0467cbde249a74d1d839424d919194f185 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 12 Oct 2022 16:46:00 +0100 Subject: [PATCH 3/3] Add destructor. Correct begin overload --- src/SparkFun_VL53L1X.cpp | 17 +++++------------ src/SparkFun_VL53L1X.h | 1 + 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/SparkFun_VL53L1X.cpp b/src/SparkFun_VL53L1X.cpp index 980d771..4ea1cb4 100644 --- a/src/SparkFun_VL53L1X.cpp +++ b/src/SparkFun_VL53L1X.cpp @@ -47,6 +47,10 @@ SFEVL53L1X::SFEVL53L1X(TwoWire &i2cPort, int shutdownPin, int interruptPin) _interruptPin = interruptPin; _device = new VL53L1X(&i2cPort, shutdownPin, interruptPin); } +SFEVL53L1X::~SFEVL53L1X() +{ + delete (VL53L1X *)_device; +} bool SFEVL53L1X::init() { @@ -66,18 +70,7 @@ bool SFEVL53L1X::begin(TwoWire &i2cPort) _i2cPort = &i2cPort; _device->dev_i2c = &i2cPort; - if (checkID() == false) - { - Serial.println("Check ID failed..."); - return (VL53L1_ERROR_PLATFORM_SPECIFIC_START); - } - - bool result = _device->VL53L1X_SensorInit() == 0; - - if (!result) - Serial.println("Init failed..."); - - return result; + return begin(); } /*Checks the ID of the device, returns true if ID is correct*/ diff --git a/src/SparkFun_VL53L1X.h b/src/SparkFun_VL53L1X.h index ef2dcc6..fbb940a 100644 --- a/src/SparkFun_VL53L1X.h +++ b/src/SparkFun_VL53L1X.h @@ -62,6 +62,7 @@ class SFEVL53L1X //Constructs our Distance sensor SFEVL53L1X(); // Default to Wire. Set both pins to -1 (undefined). SFEVL53L1X(TwoWire &i2cPort, int shutdownPin = -1, int interruptPin = -1); + ~SFEVL53L1X(); bool init(); //Deprecated version of begin bool begin(); //Initialization of sensor bool begin(TwoWire &i2cPort); //Initialization of sensor