Skip to content

Commit 88eceb4

Browse files
committed
fix requested
* remove atexit and cleanup * fix the formatting and method naming
1 parent d8495ad commit 88eceb4

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

adafruit_vl53l0x.py

+14-20
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
https://github.com/adafruit/circuitpython/releases
4646
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4747
"""
48-
import atexit
4948
import math
5049
import time
5150

@@ -321,7 +320,6 @@ def __init__(self, i2c, address=41, io_timeout_s=0):
321320
self._perform_single_ref_calibration(0x00)
322321
# "restore the previous Sequence Config"
323322
self._write_u8(_SYSTEM_SEQUENCE_CONFIG, 0xE8)
324-
atexit.register(self._cleanup)
325323

326324
def _read_u8(self, address):
327325
# Read an 8-bit unsigned value from the specified 8-bit address.
@@ -458,11 +456,6 @@ def _get_sequence_step_timeouts(self, pre_range):
458456
pre_range_mclks,
459457
)
460458

461-
def _cleanup(self):
462-
#when exiting, don't forget to also turn off continuous mode
463-
if (self._continuous_mode):
464-
self.stopContinuous()
465-
466459
@property
467460
def signal_rate_limit(self):
468461
"""The signal rate limit in mega counts per second."""
@@ -537,17 +530,17 @@ def measurement_timing_budget(self, budget_us):
537530

538531
@property
539532
def range(self):
540-
"""Perform a single (or continuous if `startContinuous` called)
533+
"""Perform a single (or continuous if `start_continuous` called)
541534
reading of the range for an object in front of the sensor and
542535
return the distance in millimeters.
543536
"""
544537
# Adapted from readRangeSingleMillimeters in pololu code at:
545538
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
546-
if (not self._continuous_mode):
547-
self.doRangeMeasurement()
548-
return self.readRange()
539+
if not self._continuous_mode:
540+
self.do_range_measurement()
541+
return self.read_range()
549542

550-
def doRangeMeasurement(self):
543+
def do_range_measurement(self):
551544
"""Perform a single reading of the range for an object in front of the
552545
sensor, but without return the distance.
553546
"""
@@ -572,11 +565,11 @@ def doRangeMeasurement(self):
572565
):
573566
raise RuntimeError("Timeout waiting for VL53L0X!")
574567

575-
def readRange(self):
568+
def read_range(self):
576569
"""Return a range reading in millimeters.
577570
578571
Note: Avoid calling this directly. If you do single mode, you need
579-
to call doRangeMeasurement first. Or your program will stuck or
572+
to call `do_range_measurement` first. Or your program will stuck or
580573
timeout occurred.
581574
"""
582575
# Adapted from readRangeContinuousMillimeters in pololu code at:
@@ -596,16 +589,17 @@ def readRange(self):
596589

597590
@property
598591
def continuous_mode(self):
592+
"""Is the sensor currently in continuous mode?"""
599593
return self._continuous_mode
600594

601595
@continuous_mode.setter
602596
def continuous_mode(self, enabled):
603-
if (enabled):
604-
self.startContinuous()
597+
if enabled:
598+
self.start_continuous()
605599
else:
606-
self.stopContinuous()
600+
self.stop_continuous()
607601

608-
def startContinuous(self):
602+
def start_continuous(self):
609603
"""Perform a continuous reading of the range for an object in front of
610604
the sensor.
611605
"""
@@ -631,7 +625,7 @@ def startContinuous(self):
631625
raise RuntimeError("Timeout waiting for VL53L0X!")
632626
self._continuous_mode = True
633627

634-
def stopContinuous(self):
628+
def stop_continuous(self):
635629
"""Stop continuous readings.
636630
"""
637631
# Adapted from stopContinuous in pololu code at:
@@ -642,7 +636,7 @@ def stopContinuous(self):
642636
(0x00, 0x00),
643637
(0x91, 0x00),
644638
(0x00, 0x01),
645-
(0xFF, 0x00)
639+
(0xFF, 0x00),
646640
):
647641
self._write_u8(pair[0], pair[1])
648642
self._continuous_mode = False

examples/vl53l0x_continuous.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@
4949
# turn on the VL53L0X to allow hardware check
5050
power_pin.value = True
5151
# instantiate the VL53L0X sensor on the I2C bus & insert it into the "vl53" list
52-
vl53.insert(i, VL53L0X(i2c)) # also performs VL53L0X hardware check
52+
vl53.insert(i, VL53L0X(i2c)) # also performs VL53L0X hardware check
5353

5454
# start continous mode
5555
vl53[i].continuous_mode = True
5656
# or alternatively with this
57-
# vl53[i].startContinous()
57+
# vl53[i].start_continous()
5858

5959
# you will see the benefit of continous mode if you set the measurement timing
6060
# budget very high.
61-
#vl53[i].measurement_timing_budget = 2000000
61+
# vl53[i].measurement_timing_budget = 2000000
6262

6363
# no need to change the address of the last VL53L0X sensor
6464
if i < len(xshut) - 1:
@@ -75,6 +75,7 @@
7575
# >>> [hex(x) for x in i2c.scan()]
7676
# >>> i2c.unlock()
7777

78+
7879
def detect_range(count=5):
7980
""" take count=5 samples """
8081
while count:
@@ -83,10 +84,22 @@ def detect_range(count=5):
8384
time.sleep(1.0)
8485
count -= 1
8586

87+
88+
def stop_continuous():
89+
""" this is not required, unless if you want to save some energy """
90+
for sensor in vl53:
91+
sensor.continuous_mode = False
92+
# or alternatively with this
93+
# sensor.stop_continuous()
94+
95+
8696
if __name__ == "__main__":
8797
detect_range()
98+
stop_continuous()
8899
else:
89100
print(
90101
"Multiple VL53L0X sensors' addresses are assigned properly\n"
91-
"execute detect_range() to read each sensors range readings"
92-
)
102+
"execute detect_range() to read each sensors range readings.\n"
103+
"When you are done with readings, execute stop_continuous()\n"
104+
"to stop the continuous mode."
105+
)

0 commit comments

Comments
 (0)