Skip to content

Commit eff83d4

Browse files
committed
Add a .data_available property
1 parent 8150fbd commit eff83d4

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

adafruit_vl53l0x.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def __init__(self, i2c, address=41, io_timeout_s=0):
145145
self._i2c = i2c
146146
self._device = i2c_device.I2CDevice(i2c, address)
147147
self.io_timeout_s = io_timeout_s
148+
self._data_available = False
148149
# Check identification registers for expected values.
149150
# From section 3.2 of the datasheet.
150151
if (
@@ -528,6 +529,15 @@ def range(self):
528529
self.do_range_measurement()
529530
return self.read_range()
530531

532+
@property
533+
def data_available(self):
534+
"""Check if data is available from the sensor. If true a call to .range
535+
will return quickly. If false, calls to .range will wait for the sensor's
536+
next reading to be available."""
537+
if not self._data_available:
538+
self._data_available = self._read_u8(_RESULT_INTERRUPT_STATUS) & 0x07 != 0
539+
return self._data_available
540+
531541
def do_range_measurement(self):
532542
"""Perform a single reading of the range for an object in front of the
533543
sensor, but without return the distance.
@@ -553,32 +563,26 @@ def do_range_measurement(self):
553563
):
554564
raise RuntimeError("Timeout waiting for VL53L0X!")
555565

556-
def read_range(self, max_ready_wait_us = None):
566+
def read_range(self):
557567
"""Return a range reading in millimeters.
558-
559-
If max_ready_wait_us is specified, and the sensor data is not ready
560-
within that time, read_range will return -1, and you should call
561-
read_range again in the future.
562-
563568
Note: Avoid calling this directly. If you do single mode, you need
564569
to call `do_range_measurement` first. Or your program will stuck or
565570
timeout occurred.
566571
"""
567572
# Adapted from readRangeContinuousMillimeters in pololu code at:
568573
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
569574
start = time.monotonic()
570-
while (self._read_u8(_RESULT_INTERRUPT_STATUS) & 0x07) == 0:
575+
while not self.data_available:
571576
if (
572577
self.io_timeout_s > 0
573578
and (time.monotonic() - start) >= self.io_timeout_s
574579
):
575580
raise RuntimeError("Timeout waiting for VL53L0X!")
576-
if max_ready_wait_us is not None and (time.monotonic() - start)*1_000_000 >= max_ready_wait_us:
577-
return -1
578581
# assumptions: Linearity Corrective Gain is 1000 (default)
579582
# fractional ranging is not enabled
580583
range_mm = self._read_u16(_RESULT_RANGE_STATUS + 10)
581584
self._write_u8(_SYSTEM_INTERRUPT_CLEAR, 0x01)
585+
self._data_available = False
582586
return range_mm
583587

584588
@property

0 commit comments

Comments
 (0)