Skip to content

Commit eb2ecac

Browse files
committed
User ROI
Added method to get ROI values currently being used. Debugged errors with ROI that were causing incorrect values to be stored. Added documentation.
1 parent e64418e commit eb2ecac

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/SparkFun_VL53L1X_Arduino_Library.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ uint8_t VL53L1X::getDistanceMode()
251251
}
252252

253253
//Set a custom zone from the array of sensors. Minimum of 4x4, maximum of 16x16.
254+
//lower left corner of the array is (0, 0) and upper right is (16, 16)
254255
void VL53L1X::setUserRoi(UserRoi *roi)
255256
{
256257
uint8_t centerX = (roi->topLeftX + roi->bottomRightX + 1) / 2;
@@ -259,9 +260,9 @@ void VL53L1X::setUserRoi(UserRoi *roi)
259260
uint8_t height = roi->topLeftY - roi->bottomRightY;
260261

261262
//Check boundary conditions, if incorrect set to default values.
262-
if (width < 3 || width > 15 || height < 3 || height > 15){
263+
if (width < 3 || height < 3){
263264
setCenter((uint8_t)8, (uint8_t)8);
264-
setZoneSize((uint8_t)15, (uint8_t)8);
265+
setZoneSize((uint8_t)15, (uint8_t)15);
265266
}
266267
else{
267268
setCenter(centerX, centerY);
@@ -272,21 +273,48 @@ void VL53L1X::setUserRoi(UserRoi *roi)
272273
void VL53L1X::setCenter(uint8_t centerX, uint8_t centerY){
273274
uint8_t centerValue;
274275

275-
if (centerX > 7){
276-
centerValue = 128 + (centerY << 3) + (15 - centerX);
276+
if (centerY > 7){
277+
centerValue = 128 + (centerX << 3) + (15 - centerY);
277278
}
278279
else {
279-
centerValue = ((15 - centerY) << 3) + centerX;
280+
centerValue = ((15 - centerX) << 3) + centerY;
280281
}
281282

282283
writeRegister(VL53L1_ROI_CONFIG__USER_ROI_CENTRE_SPAD , centerValue);
283284
}
284285

285286
void VL53L1X::setZoneSize(uint8_t width, uint8_t height){
286-
uint8_t dimensions = (height << 4) + width;
287+
uint8_t dimensions = (height << 4) + width;
287288
writeRegister(VL53L1_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, dimensions);
288289
}
289290

291+
UserRoi* VL53L1X::getUserRoi(){
292+
UserRoi* roi = new UserRoi();
293+
294+
uint8_t center = readRegister(VL53L1_ROI_CONFIG__USER_ROI_CENTRE_SPAD);
295+
uint8_t row = 0;
296+
uint8_t col = 0;
297+
if (center > 127){
298+
row = 8 + ((255-center) & 0x07);
299+
col = (center - 128) >> 3;
300+
} else {
301+
row = center & 0x07;
302+
col = (127 - center) >> 3;
303+
}
304+
305+
uint8_t dimensions = readRegister(VL53L1_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE);
306+
uint8_t height = dimensions >> 4;
307+
uint8_t width = dimensions & 0x0F;
308+
309+
310+
roi->topLeftX = (2 * col - width) >> 1;
311+
roi->topLeftY = (2 * row - height) >> 1;
312+
roi->bottomRightX = (2 * col + width) >> 1;
313+
roi->bottomRightY = (2 * row + height) >> 1;
314+
315+
return roi;
316+
}
317+
290318
//The sensor returns a range status that needs to be re-mapped to one of 9 different statuses
291319
//This does that.
292320
uint8_t VL53L1X::getRangeStatus()

src/SparkFun_VL53L1X_Arduino_Library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class VL53L1X {
109109
void setUserRoi(UserRoi*); //Set custom sensor zones
110110
void setCenter(uint8_t centerX, uint8_t centerY); //Set the center of a custom zone
111111
void setZoneSize(uint8_t width, uint8_t height); //Set the size of a custom zone
112+
UserRoi* getUserRoi();
112113

113114
private:
114115

0 commit comments

Comments
 (0)