@@ -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.
276341uint8_t VL53L1X::getRangeStatus ()
0 commit comments