Skip to content

Commit 20b5752

Browse files
Re-adding ROI
1 parent f3f68ac commit 20b5752

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/SparkFun_VL53L1X_Arduino_Library.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,71 @@ uint8_t VL53L1X::getDistanceMode()
271271
return _distanceMode;
272272
}
273273

274+
//Set a custom zone from the array of sensors. Minimum of 4x4, maximum of 16x16.
275+
//lower left corner of the array is (0, 0) and upper right is (15, 15)
276+
void VL53L1X::setUserRoi(UserRoi *roi)
277+
{
278+
uint8_t centerX = (roi->topLeftX + roi->bottomRightX + 1) / 2;
279+
uint8_t centerY = (roi->topLeftY + roi->bottomRightY + 1) / 2;
280+
uint8_t width = roi->bottomRightX - roi->topLeftX;
281+
uint8_t height = roi->topLeftY - roi->bottomRightY;
282+
283+
//Check boundary conditions, if incorrect set to default values.
284+
if (width < 3 || height < 3){
285+
setCenter((uint8_t)8, (uint8_t)8);
286+
setZoneSize((uint8_t)15, (uint8_t)15);
287+
}
288+
else{
289+
setCenter(centerX, centerY);
290+
setZoneSize(width, height);
291+
}
292+
}
293+
294+
void VL53L1X::setCenter(uint8_t centerX, uint8_t centerY){
295+
uint8_t centerValue;
296+
297+
if (centerY > 7){
298+
centerValue = 128 + (centerX << 3) + (15 - centerY);
299+
}
300+
else {
301+
centerValue = ((15 - centerX) << 3) + centerY;
302+
}
303+
304+
writeRegister(VL53L1_ROI_CONFIG__USER_ROI_CENTRE_SPAD , centerValue);
305+
}
306+
307+
void VL53L1X::setZoneSize(uint8_t width, uint8_t height){
308+
uint8_t dimensions = (height << 4) + width;
309+
writeRegister(VL53L1_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, dimensions);
310+
}
311+
312+
UserRoi* VL53L1X::getUserRoi(){
313+
UserRoi* roi = new UserRoi();
314+
315+
uint8_t center = readRegister(VL53L1_ROI_CONFIG__USER_ROI_CENTRE_SPAD);
316+
uint8_t row = 0;
317+
uint8_t col = 0;
318+
if (center > 127){
319+
row = 8 + ((255-center) & 0x07);
320+
col = (center - 128) >> 3;
321+
} else {
322+
row = center & 0x07;
323+
col = (127 - center) >> 3;
324+
}
325+
326+
uint8_t dimensions = readRegister(VL53L1_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE);
327+
uint8_t height = dimensions >> 4;
328+
uint8_t width = dimensions & 0x0F;
329+
330+
331+
roi->topLeftX = (2 * col - width) >> 1;
332+
roi->topLeftY = (2 * row - height) >> 1;
333+
roi->bottomRightX = (2 * col + width) >> 1;
334+
roi->bottomRightY = (2 * row + height) >> 1;
335+
336+
return roi;
337+
}
338+
274339
//The sensor returns a range status that needs to be re-mapped to one of 9 different statuses
275340
//This does that.
276341
uint8_t VL53L1X::getRangeStatus()

src/SparkFun_VL53L1X_Arduino_Library.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ const byte defaultAddress_VL53L1X = 0x29; //The default I2C address for the VL53
7979
#endif
8080
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8181

82+
83+
typedef struct{
84+
uint8_t topLeftX;
85+
uint8_t topLeftY;
86+
uint8_t bottomRightX;
87+
uint8_t bottomRightY;
88+
}UserRoi;
89+
8290
class VL53L1X {
8391
public:
8492

@@ -96,6 +104,11 @@ class VL53L1X {
96104
void setDistanceMode(uint8_t mode = 2);//Defaults to long range
97105
uint8_t getDistanceMode();
98106
uint8_t getRangeStatus(); //Returns the results from the last measurement, 0 == valid
107+
108+
void setUserRoi(UserRoi*); //Set custom sensor zones
109+
void setCenter(uint8_t centerX, uint8_t centerY); //Set the center of a custom zone
110+
void setZoneSize(uint8_t width, uint8_t height); //Set the size of a custom zone
111+
UserRoi* getUserRoi();
99112

100113
void setupManualCalibration();
101114

0 commit comments

Comments
 (0)