@@ -145,6 +145,7 @@ def __init__(self, i2c, address=41, io_timeout_s=0):
145
145
self ._i2c = i2c
146
146
self ._device = i2c_device .I2CDevice (i2c , address )
147
147
self .io_timeout_s = io_timeout_s
148
+ self ._data_ready = False
148
149
# Check identification registers for expected values.
149
150
# From section 3.2 of the datasheet.
150
151
if (
@@ -528,6 +529,15 @@ def range(self):
528
529
self .do_range_measurement ()
529
530
return self .read_range ()
530
531
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
+
531
541
def do_range_measurement (self ):
532
542
"""Perform a single reading of the range for an object in front of the
533
543
sensor, but without return the distance.
@@ -555,15 +565,14 @@ def do_range_measurement(self):
555
565
556
566
def read_range (self ):
557
567
"""Return a range reading in millimeters.
558
-
559
568
Note: Avoid calling this directly. If you do single mode, you need
560
569
to call `do_range_measurement` first. Or your program will stuck or
561
570
timeout occurred.
562
571
"""
563
572
# Adapted from readRangeContinuousMillimeters in pololu code at:
564
573
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
565
574
start = time .monotonic ()
566
- while ( self ._read_u8 ( _RESULT_INTERRUPT_STATUS ) & 0x07 ) == 0 :
575
+ while not self .data_ready :
567
576
if (
568
577
self .io_timeout_s > 0
569
578
and (time .monotonic () - start ) >= self .io_timeout_s
@@ -573,6 +582,7 @@ def read_range(self):
573
582
# fractional ranging is not enabled
574
583
range_mm = self ._read_u16 (_RESULT_RANGE_STATUS + 10 )
575
584
self ._write_u8 (_SYSTEM_INTERRUPT_CLEAR , 0x01 )
585
+ self ._data_ready = False
576
586
return range_mm
577
587
578
588
@property
0 commit comments