Skip to content

Commit cd61394

Browse files
authored
Merge pull request #35 from whogben/main
*updated* Add .data_ready property to VL53L0X allowing end-users to predict if calls to .range will block
2 parents edeb83c + 4db93da commit cd61394

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

adafruit_vl53l0x.py

Lines changed: 12 additions & 2 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_ready = 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_ready(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_ready:
538+
self._data_ready = self._read_u8(_RESULT_INTERRUPT_STATUS) & 0x07 != 0
539+
return self._data_ready
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.
@@ -555,15 +565,14 @@ def do_range_measurement(self):
555565

556566
def read_range(self):
557567
"""Return a range reading in millimeters.
558-
559568
Note: Avoid calling this directly. If you do single mode, you need
560569
to call `do_range_measurement` first. Or your program will stuck or
561570
timeout occurred.
562571
"""
563572
# Adapted from readRangeContinuousMillimeters in pololu code at:
564573
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
565574
start = time.monotonic()
566-
while (self._read_u8(_RESULT_INTERRUPT_STATUS) & 0x07) == 0:
575+
while not self.data_ready:
567576
if (
568577
self.io_timeout_s > 0
569578
and (time.monotonic() - start) >= self.io_timeout_s
@@ -573,6 +582,7 @@ def read_range(self):
573582
# fractional ranging is not enabled
574583
range_mm = self._read_u16(_RESULT_RANGE_STATUS + 10)
575584
self._write_u8(_SYSTEM_INTERRUPT_CLEAR, 0x01)
585+
self._data_ready = False
576586
return range_mm
577587

578588
@property

0 commit comments

Comments
 (0)