@@ -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_available = 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_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
+
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.
@@ -553,32 +563,26 @@ def do_range_measurement(self):
553
563
):
554
564
raise RuntimeError ("Timeout waiting for VL53L0X!" )
555
565
556
- def read_range (self , max_ready_wait_us = None ):
566
+ def read_range (self ):
557
567
"""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
-
563
568
Note: Avoid calling this directly. If you do single mode, you need
564
569
to call `do_range_measurement` first. Or your program will stuck or
565
570
timeout occurred.
566
571
"""
567
572
# Adapted from readRangeContinuousMillimeters in pololu code at:
568
573
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
569
574
start = time .monotonic ()
570
- while ( self ._read_u8 ( _RESULT_INTERRUPT_STATUS ) & 0x07 ) == 0 :
575
+ while not self .data_available :
571
576
if (
572
577
self .io_timeout_s > 0
573
578
and (time .monotonic () - start ) >= self .io_timeout_s
574
579
):
575
580
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
578
581
# assumptions: Linearity Corrective Gain is 1000 (default)
579
582
# fractional ranging is not enabled
580
583
range_mm = self ._read_u16 (_RESULT_RANGE_STATUS + 10 )
581
584
self ._write_u8 (_SYSTEM_INTERRUPT_CLEAR , 0x01 )
585
+ self ._data_available = False
582
586
return range_mm
583
587
584
588
@property
0 commit comments