Skip to content

*updated* Add .data_ready property to VL53L0X allowing end-users to predict if calls to .range will block #35

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 6 commits into from
Mar 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions adafruit_vl53l0x.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def __init__(self, i2c, address=41, io_timeout_s=0):
self._i2c = i2c
self._device = i2c_device.I2CDevice(i2c, address)
self.io_timeout_s = io_timeout_s
self._data_ready = False
# Check identification registers for expected values.
# From section 3.2 of the datasheet.
if (
Expand Down Expand Up @@ -528,6 +529,15 @@ def range(self):
self.do_range_measurement()
return self.read_range()

@property
def data_ready(self):
"""Check if data is available from the sensor. If true a call to .range
will return quickly. If false, calls to .range will wait for the sensor's
next reading to be available."""
if not self._data_ready:
self._data_ready = self._read_u8(_RESULT_INTERRUPT_STATUS) & 0x07 != 0
return self._data_ready

def do_range_measurement(self):
"""Perform a single reading of the range for an object in front of the
sensor, but without return the distance.
Expand Down Expand Up @@ -555,15 +565,14 @@ def do_range_measurement(self):

def read_range(self):
"""Return a range reading in millimeters.

Note: Avoid calling this directly. If you do single mode, you need
to call `do_range_measurement` first. Or your program will stuck or
timeout occurred.
"""
# Adapted from readRangeContinuousMillimeters in pololu code at:
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
start = time.monotonic()
while (self._read_u8(_RESULT_INTERRUPT_STATUS) & 0x07) == 0:
while not self.data_ready:
if (
self.io_timeout_s > 0
and (time.monotonic() - start) >= self.io_timeout_s
Expand All @@ -573,6 +582,7 @@ def read_range(self):
# fractional ranging is not enabled
range_mm = self._read_u16(_RESULT_RANGE_STATUS + 10)
self._write_u8(_SYSTEM_INTERRUPT_CLEAR, 0x01)
self._data_ready = False
return range_mm

@property
Expand Down