Skip to content

Added getter and setter functions for ROI #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 2, 2024
41 changes: 41 additions & 0 deletions adafruit_vl53l1x.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
_RANGE_CONFIG__VALID_PHASE_HIGH = const(0x0069)
_SD_CONFIG__WOI_SD0 = const(0x0078)
_SD_CONFIG__INITIAL_PHASE_SD0 = const(0x007A)
_ROI_CONFIG__USER_ROI_CENTRE_SPAD = const(0x007F)
_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE = const(0x0080)
_SYSTEM__INTERRUPT_CLEAR = const(0x0086)
_SYSTEM__MODE_START = const(0x0087)
_VL53L1X_RESULT__RANGE_STATUS = const(0x0089)
Expand Down Expand Up @@ -295,6 +297,45 @@ def distance_mode(self, mode):
raise ValueError("Unsupported mode.")
self.timing_budget = self._timing_budget

@property
def roi_xy(self):
"""Returns the x and y coordinates of the sensor's region of interest"""
temp = self._read_register(_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE)

x = (int.from_bytes(temp) & 0x0F) + 1
y = ((int.from_bytes(temp) & 0xF0) >> 4) + 1

return x, y

@roi_xy.setter
def roi_xy(self, data):
x, y = data
optical_center = 0

x = min(x, 16)
y = min(y, 16)

if x > 10 or y > 10:
optical_center = 199

self._write_register(
_ROI_CONFIG__USER_ROI_CENTRE_SPAD, optical_center.to_bytes()
)
self._write_register(
_ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE,
((y - 1) << 4 | (x - 1)).to_bytes(),
)

@property
def roi_center(self):
"""Returns the center of the sensor's region of interest"""
temp = self._read_register(_ROI_CONFIG__USER_ROI_CENTRE_SPAD)
return int.from_bytes(temp)

@roi_center.setter
def roi_center(self, center):
self._write_register(_ROI_CONFIG__USER_ROI_CENTRE_SPAD, center.to_bytes())

def _write_register(self, address, data, length=None):
if length is None:
length = len(data)
Expand Down