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
tyeth
wants to merge
13
commits into
adafruit:main
Choose a base branch
from
tyeth:add-VL53L4CX
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b7ed28e
Refactored commit - VL53L4CX default settings
tyeth d9788a4
clang-format VL53L4CX
tyeth f15b6a6
Update VL53L4CX after VL53L4CD PR feedback
tyeth cd79719
VL53L4CX clang format
tyeth bd9934c
Refactor VL53L4CX
tyeth b2fef64
Doxygen commnets for VL54L4CX
tyeth 29cdd03
clang-format
tyeth 5005bb9
doxygen?
tyeth 9f2dae1
Reorder init statements for VL53L4CX
tyeth 2ced49c
Fix timing budget VL53L4CX
tyeth c73d073
Clang format
tyeth d9f6d0b
Correct failure logic
tyeth a1ec860
Refactor: VL53L4CX rename passed param + whitespace
tyeth File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 222 additions & 0 deletions
222
src/components/i2c/drivers/WipperSnapper_I2C_Driver_VL53L4CX.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
(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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.