Skip to content

Commit

Permalink
Merge pull request #564 from tyeth/add-VL53L4CX
Browse files Browse the repository at this point in the history
Refactored commit - VL53L4CX default settings
  • Loading branch information
tyeth authored Jun 25, 2024
2 parents c3c475c + cbc36ca commit df388f2
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
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 MCP3421, Adafruit NAU7802 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 MCP3421, Adafruit NAU7802 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -159,6 +160,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
206 changes: 206 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*!
* @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 250 ///< Delay for reading data attempts
#define VL53_TIMING_BUDGET_NS 200000 ///< Timing budget for VL53L4CX sensor

/**************************************************************************/
/*!
@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(
VL53_TIMING_BUDGET_NS) != 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: ");
WS_DEBUG_PRINTLN(status);
return false;
}

// Wait for data read period then update data ready status
WS_DEBUG_PRINT("Waiting for VL53L4CX data ready...");
delay(VL53_READING_DELAY);
status = _VL53L4CX->VL53L4CX_GetMeasurementDataReady(&NewDataReady);

if ((status != VL53L4CX_ERROR_NONE) || (NewDataReady == 0)) {
// error or no data ready
WS_DEBUG_PRINT("VL53L4CX Error checking for data ready: ");
WS_DEBUG_PRINTLN(status);
return false;
}

// get data - still to verify which of one or two objects found
status = _VL53L4CX->VL53L4CX_GetMultiRangingData(pMultiRangingData);
if (status != VL53L4CX_ERROR_NONE) {
WS_DEBUG_PRINT("VL53L4CX Error getting multi ranging data: ");
WS_DEBUG_PRINTLN(status);
return false;
}

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

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

/*******************************************************************************/
/*!
@brief Checks the VL53L4CX's proximity result and sets event value.
@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;
}

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

#endif // WipperSnapper_I2C_Driver_VL53L4CX

0 comments on commit df388f2

Please sign in to comment.