Issue:
For a VL53L1X_SetOffset method call with a value of -1, VL53L1X_GetOffset will return a value of 2047.
Diagnosis:
The right shift operation (Temp = Temp >> 5;) in VL53L1X_GetOffset does not correctly address a negative offset value. The shift right is intended to restore the offset to the original value after correctly setting the sign with the shift left operation. However, the shift right for a negative arithmetic operand in C++ is implementation dependent. For the RPI 4B, zeros are placed in the leading bit(s) and an incorrect positive offset are returned. Using the division by 2**5 will ensure the correct positive or negative value is restored.
Recommendation:
Replace the lines (vl53l1x_class.cpp 639-640):
Temp = Temp >> 5;
offset = (int16_t)(Temp);
with
offset = (static_cast(Temp))/32;
I have successfully tested this change on a RPI 4B using g++ version 10.2.0 and the C++17 standard.
Issue: For a VL53L1X_SetOffset method call with a value of -1, VL53L1X_GetOffset will return a value of 2047.
Diagnosis: The right shift operation (Temp = Temp >> 5;) in VL53L1X_GetOffset does not correctly address a negative offset value. The shift right is intended to restore the offset to the original value after correctly setting the sign with the shift left operation. However, the shift right for a negative arithmetic operand in C++ is implementation dependent. For the RPI 4B, zeros are placed in the leading bit(s) and an incorrect positive offset are returned. Using the division by 2**5 will ensure the correct positive or negative value is restored.
Recommendation:
Replace the lines (vl53l1x_class.cpp 639-640): Temp = Temp >> 5; offset = (int16_t)(Temp); with offset = (static_cast(Temp))/32;
I have successfully tested this change on a RPI 4B using g++ version 10.2.0 and the C++17 standard.