Skip to content

Commit c711604

Browse files
committed
New simple example and error handling in graph.py
1 parent 49a6199 commit c711604

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

examples/distance.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import time
5+
import sys
6+
import signal
7+
8+
import VL53L1X
9+
10+
11+
print("""distance.py
12+
13+
Display the distance read from the sensor.
14+
15+
Press Ctrl+C to exit.
16+
17+
""")
18+
19+
20+
"""
21+
Open and start the VL53L1X ranging sensor
22+
"""
23+
tof = VL53L1X.VL53L1X(i2c_bus=1, i2c_address=0x29)
24+
tof.open() # Initialise the i2c bus and configure the sensor
25+
tof.start_ranging(1) # Start ranging, 1 = Short Range, 2 = Medium Range, 3 = Long Range
26+
27+
28+
running = True
29+
30+
def exit_handler(signal, frame):
31+
global running
32+
running = False
33+
tof.stop_ranging() # Stop ranging
34+
print()
35+
sys.exit(0)
36+
37+
signal.signal(signal.SIGINT, exit_handler)
38+
39+
while running:
40+
distance_in_mm = tof.get_distance() # Grab the range in mm
41+
print("Distance: {}mm".format(distance_in_mm))
42+
time.sleep(0.1)
43+

examples/graph.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
"""
2727
Grab the width/height of the terminal using `stty size`
2828
"""
29-
rows, cols = [int(c) for c in os.popen("stty size", "r").read().split()]
30-
29+
try:
30+
rows, cols = [int(c) for c in os.popen("stty size", "r").read().split()]
31+
except ValueError:
32+
print("Cannot get size of tty! Try running in Terminal.")
33+
sys.exit(1)
3134

3235
"""
3336
Open and start the VL53L1X ranging sensor

0 commit comments

Comments
 (0)