Skip to content

Commit dd60983

Browse files
committed
remove atexit and cleanup, also fix the formatting
1 parent d8495ad commit dd60983

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

adafruit_vl53l0x.py

Lines changed: 3 additions & 10 deletions
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."""
@@ -543,7 +536,7 @@ def range(self):
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):
539+
if not self._continuous_mode:
547540
self.doRangeMeasurement()
548541
return self.readRange()
549542

@@ -600,7 +593,7 @@ def continuous_mode(self):
600593

601594
@continuous_mode.setter
602595
def continuous_mode(self, enabled):
603-
if (enabled):
596+
if enabled:
604597
self.startContinuous()
605598
else:
606599
self.stopContinuous()
@@ -642,7 +635,7 @@ def stopContinuous(self):
642635
(0x00, 0x00),
643636
(0x91, 0x00),
644637
(0x00, 0x01),
645-
(0xFF, 0x00)
638+
(0xFF, 0x00),
646639
):
647640
self._write_u8(pair[0], pair[1])
648641
self._continuous_mode = False

examples/vl53l0x_continuous.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
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
@@ -58,7 +58,7 @@
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.stopContinuous()
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)