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

Refactored commit - VL53L4CX default settings #564

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion library.properties
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
1 change: 1 addition & 0 deletions platformio.ini
Expand Up @@ -51,6 +51,7 @@ lib_deps =
adafruit/Adafruit_VL53L0X
adafruit/Adafruit VL53L1X
stm32duino/STM32duino VL53L4CD
stm32duino/STM32duino VL53L4CX
adafruit/Adafruit_VL6180X
adafruit/Adafruit PM25 AQI Sensor
adafruit/Adafruit VEML7700 Library
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Expand Up @@ -691,6 +691,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_vl53l4cd->configureDriver(msgDeviceInitReq);
drivers.push_back(_vl53l4cd);
WS_DEBUG_PRINTLN("VL53L4CD Initialized Successfully!");
} else if (strcmp("vl53l4cx", msgDeviceInitReq->i2c_device_name) == 0) {
_vl53l4cx = new WipperSnapper_I2C_Driver_VL53L4CX(this->_i2c, i2cAddress);
if (!_vl53l4cx->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize VL53L4CX!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_vl53l4cx->configureDriver(msgDeviceInitReq);
drivers.push_back(_vl53l4cx);
WS_DEBUG_PRINTLN("VL53L4CX Initialized Successfully!");
} else if (strcmp("vl6180x", msgDeviceInitReq->i2c_device_name) == 0) {
_vl6180x = new WipperSnapper_I2C_Driver_VL6180X(this->_i2c, i2cAddress);
if (!_vl6180x->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Expand Up @@ -64,6 +64,7 @@
#include "drivers/WipperSnapper_I2C_Driver_VL53L0X.h"
#include "drivers/WipperSnapper_I2C_Driver_VL53L1X.h"
#include "drivers/WipperSnapper_I2C_Driver_VL53L4CD.h"
#include "drivers/WipperSnapper_I2C_Driver_VL53L4CX.h"
#include "drivers/WipperSnapper_I2C_Driver_VL6180X.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.
Expand Down Expand Up @@ -155,6 +156,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_VL53L0X *_vl53l0x = nullptr;
WipperSnapper_I2C_Driver_VL53L1X *_vl53l1x = nullptr;
WipperSnapper_I2C_Driver_VL53L4CD *_vl53l4cd = nullptr;
WipperSnapper_I2C_Driver_VL53L4CX *_vl53l4cx = nullptr;
WipperSnapper_I2C_Driver_VL6180X *_vl6180x = nullptr;
WipperSnapper_I2C_Driver_MAX17048 *_max17048 = nullptr;
WipperSnapper_I2C_Driver_ADT7410 *_adt7410 = nullptr;
Expand Down
222 changes: 222 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h
@@ -0,0 +1,222 @@
/*!
* @file WipperSnapper_I2C_Driver_VL53L4CX.h
*
* Device driver for the VL53L4CX ToF sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) 2024 Tyeth Gundry for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_VL53L4CX_H
#define WipperSnapper_I2C_Driver_VL53L4CX_H

#include "WipperSnapper_I2C_Driver.h"
#include <vl53l4cx_class.h>
#include <vl53l4cx_def.h>

#define VL53_SHUTDOWN_PIN -1 ///< Shutdown pin for VL53L4CX sensor
#define VL53_READING_DELAY 350 ///< Delay for reading data attempts

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a VL53L4CX sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_VL53L4CX : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a VL53L4CX sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_VL53L4CX(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an VL53L4CX sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_VL53L4CX() {
// Called when a VL53L4CX component is deleted.
delete _VL53L4CX;
}

/*******************************************************************************/
/*!
@brief Initializes the VL53L4CX sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_VL53L4CX = new VL53L4CX(_i2c, VL53_SHUTDOWN_PIN);

if (_VL53L4CX->InitSensor((uint8_t)_sensorAddress) != VL53L4CX_ERROR_NONE) {
WS_DEBUG_PRINTLN("Failed to initialize VL53L4CX sensor!");
return false;
}

if (_VL53L4CX->VL53L4CX_SetDistanceMode(VL53L4CX_DISTANCEMODE_LONG) !=
VL53L4CX_ERROR_NONE) {
WS_DEBUG_PRINTLN("Failed to set VL53L4CX distance mode to long!");
return false;
}

// Set 200ms measurement time, the possible TimingBudget is 8-200ms
if (_VL53L4CX->VL53L4CX_SetMeasurementTimingBudgetMicroSeconds(200000) !=
VL53L4CX_ERROR_NONE) {
WS_DEBUG_PRINTLN("Failed to set VL53L4CX timing budget!");
return false;
}

if (_VL53L4CX->VL53L4CX_StartMeasurement() != VL53L4CX_ERROR_NONE) {
WS_DEBUG_PRINTLN("Failed to start VL53L4CX ranging!");
return false;
}
return true;
}

/*******************************************************************************/
/*!
@brief Gets the VL53L4CX's current proximity for first object if found.
@param proximityEvent
Pointer to an Adafruit_Sensor event.
@returns True if the proximity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventProximity(sensors_event_t *proximityEvent) {
return getProximity(proximityEvent, 0);
}

/*******************************************************************************/
/*!
@brief Gets the VL53L4CX's current proximity for second object if
found.
@param proximityEvent
Pointer to an Adafruit_Sensor event.
@returns True if the proximity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *proximityEvent) {
return getProximity(proximityEvent, 1);
}

/*******************************************************************************/
/*!
@brief Gets the VL53L4CX's current proximity (first or second object).
@param proximityEvent
Pointer to an Adafruit_Sensor event.
@param whichObject
Index of the proximity object to get (0, or 1 for second
object).
@returns True if the proximity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getProximity(sensors_event_t *proximityEvent, int whichObject = 0) {
VL53L4CX_MultiRangingData_t MultiRangingData;
VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
uint8_t NewDataReady = 0;
int status;

// Start fresh reading, seemed to be accepting stale value
status = _VL53L4CX->VL53L4CX_ClearInterruptAndStartMeasurement();
if (status != VL53L4CX_ERROR_NONE) {
WS_DEBUG_PRINT(
"VL53L4CX Error clearing interrupt and starting measurement: ");
{ USBSerial.println(status); };
return false;
}
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
delay(VL53_READING_DELAY);

awaitDataReady(status, NewDataReady);

if ((status == VL53L4CX_ERROR_NONE) && (NewDataReady != 0)) {
// data ready - still to verify if one or two objects found and which
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;

// zero based index, return NaN / (Object not found) if too few objects
if (no_of_object_found - 1 < whichObject) {
WS_DEBUG_PRINT("Object not found at index #");
WS_DEBUG_PRINT(whichObject);
WS_DEBUG_PRINTLN(", returning NaN");
proximityEvent->data[0] = NAN;
return true;
}

// take the first or second detected object from ranging data, verify if
// valid and then set the event data in proximityEvent or return false
return updateDataPointIfValid(pMultiRangingData->RangeData[whichObject],
proximityEvent);

} else {
// error or no data ready
WS_DEBUG_PRINT("VL53L4CX Error checking for data ready: ");
WS_DEBUG_PRINTLN(status);
}
return false;
}

/*******************************************************************************/
/*!
@brief Gets the VL53L4CX's current proximity (first or second object).
@param rangingData
The ranging data to check.
@param proximityEvent
Pointer to an Adafruit_Sensor event.
@returns True if the proximity was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool updateDataPointIfValid(VL53L4CX_TargetRangeData_t rangingData,
sensors_event_t *proximityEvent) {
if (rangingData.RangeStatus == VL53L4CX_RANGESTATUS_RANGE_VALID) {
int16_t mm = rangingData.RangeMilliMeter;
proximityEvent->data[0] = (float)mm;
return true;
}
return false;
}

/*******************************************************************************/
/*!
@brief Ensures the data is available for the VL53L4CX sensor.
@param status
Pointer to the returned error status
@param NewDataReady
Pointer to the returned data ready status
*/
/*******************************************************************************/
void awaitDataReady(int &status, uint8_t &NewDataReady) {
for (uint8_t retries = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we return NAN, and return immediately vs retrying three times? These drivers are supposed to be very small and fast, like an interrupt routine.

(status =
_VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady)) &&
!NewDataReady && retries < 3;
retries++) {
delay(VL53_READING_DELAY);
WS_DEBUG_PRINT(" .");
}
WS_DEBUG_PRINTLN("");
}

protected:
VL53L4CX *_VL53L4CX; ///< Pointer to VL53L4CX temperature sensor object
};

#endif // WipperSnapper_I2C_Driver_VL53L4CX