Skip to content

Commit 53b8717

Browse files
author
Ramsay, Grant (NZ)
committed
Update examples to interface change
1 parent 9c998ed commit 53b8717

4 files changed

Lines changed: 49 additions & 37 deletions

File tree

python/VL53L0X_TCA9548A_example.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,37 @@
2626
import VL53L0X
2727

2828
# Create a VL53L0X object for device on TCA9548A bus 1
29-
tof1 = VL53L0X.VL53L0X(TCA9548A_Num=1, TCA9548A_Addr=0x70)
29+
tof1 = VL53L0X.VL53L0X(tca9548a_num=1, tca9548a_addr=0x70)
3030
# Create a VL53L0X object for device on TCA9548A bus 2
31-
tof2 = VL53L0X.VL53L0X(TCA9548A_Num=2, TCA9548A_Addr=0x70)
31+
tof2 = VL53L0X.VL53L0X(tca9548a_num=2, tca9548a_addr=0x70)
32+
tof1.open()
33+
tof2.open()
3234

3335
# Start ranging on TCA9548A bus 1
34-
tof1.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
36+
tof1.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)
3537
# Start ranging on TCA9548A bus 2
36-
tof2.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
38+
tof2.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)
3739

3840
timing = tof1.get_timing()
39-
if (timing < 20000):
41+
if timing < 20000:
4042
timing = 20000
41-
print ("Timing %d ms" % (timing/1000))
43+
print("Timing %d ms" % (timing/1000))
4244

43-
for count in range(1,101):
45+
for count in range(1, 101):
4446
# Get distance from VL53L0X on TCA9548A bus 1
4547
distance = tof1.get_distance()
46-
if (distance > 0):
47-
print ("1: %d mm, %d cm, %d" % (distance, (distance/10), count))
48+
if distance > 0:
49+
print("1: %d mm, %d cm, %d" % (distance, (distance/10), count))
4850

4951
# Get distance from VL53L0X on TCA9548A bus 2
5052
distance = tof2.get_distance()
51-
if (distance > 0):
52-
print ("2: %d mm, %d cm, %d" % (distance, (distance/10), count))
53+
if distance > 0:
54+
print("2: %d mm, %d cm, %d" % (distance, (distance/10), count))
5355

5456
time.sleep(timing/1000000.00)
5557

5658
tof1.stop_ranging()
5759
tof2.stop_ranging()
5860

61+
tof1.close()
62+
tof2.close()

python/VL53L0X_example.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,22 @@
2727

2828
# Create a VL53L0X object
2929
tof = VL53L0X.VL53L0X()
30-
30+
tof.open()
3131
# Start ranging
32-
tof.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
32+
tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)
3333

3434
timing = tof.get_timing()
35-
if (timing < 20000):
35+
if timing < 20000:
3636
timing = 20000
37-
print ("Timing %d ms" % (timing/1000))
37+
print("Timing %d ms" % (timing/1000))
3838

39-
for count in range(1,101):
39+
for count in range(1, 101):
4040
distance = tof.get_distance()
41-
if (distance > 0):
42-
print ("%d mm, %d cm, %d" % (distance, (distance/10), count))
41+
if distance > 0:
42+
print("%d mm, %d cm, %d" % (distance, (distance/10), count))
4343

4444
time.sleep(timing/1000000.00)
4545

4646
tof.stop_ranging()
47+
tof.close()
4748

python/VL53L0X_example_livegraph.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
import VL53L0X
2929

3030
fig = plt.figure()
31-
ax1 = fig.add_subplot(1,1,1)
31+
ax1 = fig.add_subplot(1, 1, 1)
3232

3333
xarr = []
3434
yarr = []
3535
count = 0
3636

37+
3738
def animate(i):
3839
global count
3940
distance = tof.get_distance()
@@ -42,22 +43,24 @@ def animate(i):
4243
yarr.append(distance)
4344
time.sleep(timing/1000000.00)
4445
ax1.clear()
45-
ax1.plot(xarr,yarr)
46+
ax1.plot(xarr, yarr)
47+
4648

4749
# Create a VL53L0X object
4850
tof = VL53L0X.VL53L0X()
49-
51+
tof.open()
5052
# Start ranging
51-
tof.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
53+
tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)
5254

5355
timing = tof.get_timing()
54-
if (timing < 20000):
56+
if timing < 20000:
5557
timing = 20000
56-
print ("Timing %d ms" % (timing/1000))
58+
print("Timing %d ms" % (timing/1000))
5759

58-
print ("Press ctrl-c to exit")
60+
print("Press ctrl-c to exit")
5961

6062
ani = animation.FuncAnimation(fig, animate, interval=100)
6163
plt.show()
6264

6365
tof.stop_ranging()
66+
tof.close()

python/VL53L0X_multi_example.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,40 @@
4747

4848
# Create one object per VL53L0X passing the address to give to
4949
# each.
50-
tof = VL53L0X.VL53L0X(address=0x2B)
51-
tof1 = VL53L0X.VL53L0X(address=0x2D)
50+
tof = VL53L0X.VL53L0X(i2c_address=0x2B)
51+
tof1 = VL53L0X.VL53L0X(i2c_address=0x2D)
52+
tof.open()
53+
tof1.open()
5254

5355
# Set shutdown pin high for the first VL53L0X then
5456
# call to start ranging
5557
GPIO.output(sensor1_shutdown, GPIO.HIGH)
5658
time.sleep(0.50)
57-
tof.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
59+
tof.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)
5860

5961
# Set shutdown pin high for the second VL53L0X then
6062
# call to start ranging
6163
GPIO.output(sensor2_shutdown, GPIO.HIGH)
6264
time.sleep(0.50)
63-
tof1.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
65+
tof1.start_ranging(VL53L0X.Vl53l0xAccuracyMode.BETTER)
6466

6567
timing = tof.get_timing()
66-
if (timing < 20000):
68+
if timing < 20000:
6769
timing = 20000
68-
print ("Timing %d ms" % (timing/1000))
70+
print("Timing %d ms" % (timing/1000))
6971

7072
for count in range(1,101):
7173
distance = tof.get_distance()
72-
if (distance > 0):
73-
print ("sensor %d - %d mm, %d cm, iteration %d" % (tof.my_object_number, distance, (distance/10), count))
74+
if distance > 0:
75+
print("sensor %d - %d mm, %d cm, iteration %d" % (1, distance, (distance/10), count))
7476
else:
75-
print ("%d - Error" % tof.my_object_number)
77+
print("%d - Error" % 1)
7678

7779
distance = tof1.get_distance()
78-
if (distance > 0):
79-
print ("sensor %d - %d mm, %d cm, iteration %d" % (tof1.my_object_number, distance, (distance/10), count))
80+
if distance > 0:
81+
print("sensor %d - %d mm, %d cm, iteration %d" % (2, distance, (distance/10), count))
8082
else:
81-
print ("%d - Error" % tof.my_object_number)
83+
print("%d - Error" % 2)
8284

8385
time.sleep(timing/1000000.00)
8486

@@ -87,3 +89,5 @@
8789
tof.stop_ranging()
8890
GPIO.output(sensor1_shutdown, GPIO.LOW)
8991

92+
tof.close()
93+
tof1.close()

0 commit comments

Comments
 (0)