Skip to content

Commit e100ce8

Browse files
committed
implements continuous mode context manager, plus an example for simple continuous
1 parent 88eceb4 commit e100ce8

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

adafruit_vl53l0x.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -588,16 +588,26 @@ def read_range(self):
588588
return range_mm
589589

590590
@property
591-
def continuous_mode(self):
592-
"""Is the sensor currently in continuous mode?"""
591+
def is_continuous_mode(self):
592+
"""Is the sensor currently in continuous mode?
593+
"""
593594
return self._continuous_mode
594595

595-
@continuous_mode.setter
596-
def continuous_mode(self, enabled):
597-
if enabled:
598-
self.start_continuous()
599-
else:
600-
self.stop_continuous()
596+
def continuous_mode(self):
597+
"""Create a new context manager to manage the continuous mode
598+
"""
599+
class continuous_manager:
600+
def __init__(self, driver):
601+
self.driver = driver
602+
603+
def __enter__(self):
604+
self.driver.start_continuous()
605+
return self.driver
606+
607+
def __exit__(self, exc_type, exc_value, tb):
608+
self.driver.stop_continuous()
609+
610+
return continuous_manager(self)
601611

602612
def start_continuous(self):
603613
"""Perform a continuous reading of the range for an object in front of
@@ -641,6 +651,9 @@ def stop_continuous(self):
641651
self._write_u8(pair[0], pair[1])
642652
self._continuous_mode = False
643653

654+
# restore the sensor to single ranging mode
655+
self.do_range_measurement()
656+
644657
def set_address(self, new_address):
645658
"""Set a new I2C address to the instantaited object. This is only called when using
646659
multiple VL53L0X sensors on the same I2C bus (SDA & SCL pins). See also the

examples/vl53l0x_continuous.py renamed to examples/vl53l0x_multiple_sensors_continuous.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
vl53.insert(i, VL53L0X(i2c)) # also performs VL53L0X hardware check
5353

5454
# start continous mode
55-
vl53[i].continuous_mode = True
56-
# or alternatively with this
57-
# vl53[i].start_continous()
55+
vl53[i].start_continous()
5856

5957
# you will see the benefit of continous mode if you set the measurement timing
6058
# budget very high.
@@ -86,11 +84,11 @@ def detect_range(count=5):
8684

8785

8886
def stop_continuous():
89-
""" this is not required, unless if you want to save some energy """
87+
""" this is not required, if you use XSHUT to reset the sensor.
88+
unless if you want to save some energy
89+
"""
9090
for sensor in vl53:
91-
sensor.continuous_mode = False
92-
# or alternatively with this
93-
# sensor.stop_continuous()
91+
sensor.stop_continuous()
9492

9593

9694
if __name__ == "__main__":

examples/vl53l0x_simplecontinuous.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Simple demo of the VL53L0X distance sensor with continuous mode.
2+
# Will print the sensed range/distance as fast as possible.
3+
import time
4+
5+
import board
6+
import busio
7+
8+
import adafruit_vl53l0x
9+
10+
# Initialize I2C bus and sensor.
11+
i2c = busio.I2C(board.SCL, board.SDA)
12+
vl53 = adafruit_vl53l0x.VL53L0X(i2c)
13+
14+
# Optionally adjust the measurement timing budget to change speed and accuracy.
15+
# See the example here for more details:
16+
# https://github.com/pololu/vl53l0x-arduino/blob/master/examples/Single/Single.ino
17+
# For example a higher speed but less accurate timing budget of 20ms:
18+
# vl53.measurement_timing_budget = 20000
19+
# Or a slower but more accurate timing budget of 200ms:
20+
vl53.measurement_timing_budget = 200000
21+
# The default timing budget is 33ms, a good compromise of speed and accuracy.
22+
23+
# You will see the benefit of continous mode if you set the measurement timing
24+
# budget very high, while your program doing something else. When your program done
25+
# with something else, and the sensor already calculated the distance, the result
26+
# will return instantly, instead of waiting the sensor measuring first.
27+
28+
# Main loop will read the range and print it every second.
29+
with vl53.continuous_mode():
30+
while True:
31+
# try to adjust the sleep time (simulating program doing something else)
32+
# and see how fast the sensor returns the range
33+
time.sleep(0.1)
34+
35+
curTime = time.time()
36+
print("Range: {0}mm ({1:.2f}ms)".format(vl53.range, time.time() - curTime))

0 commit comments

Comments
 (0)