Skip to content

VL53L1X GetOffset Method Returns Incorrect Value for Negative Offset #45

Open
@dhrynyk

Description

@dhrynyk

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<int16_t>(Temp))/32;

I have successfully tested this change on a RPI 4B using g++ version 10.2.0 and the C++17 standard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions