Skip to content

Commit 24aa202

Browse files
committed
initial commit
1 parent f7ab3dd commit 24aa202

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
# contributions by J Fletcher, adapting code by Prof Gallaugher:
3+
# https://www.youtube.com/watch?v=cdx1A1xoEWc&t=5s
4+
# tested on ESP32-S3 Reverse TFT Feather:
5+
# https://www.adafruit.com/product/5691
6+
# SPDX-License-Identifier: MIT
7+
8+
import time
9+
import board
10+
from adafruit_display_text.bitmap_label import Label
11+
from terminalio import FONT
12+
from displayio import Group
13+
import adafruit_vl53l1x
14+
15+
# create a main_group to hold anything we want to show on the display.
16+
main_group = Group()
17+
18+
# Create sensor object, communicating over the board's default I2C bus
19+
# i2c = board.I2C() # uses board.SCL and board.SDA
20+
i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
21+
vl53 = adafruit_vl53l1x.VL53L1X(i2c)
22+
23+
# Create a Label to show the readings. If you have a very small
24+
# display you may need to change to scale=1.
25+
display_output_label = Label(FONT, text="", scale=1)
26+
27+
# place the label near the top left corner with anchored positioning
28+
display_output_label.anchor_point = (0, 0)
29+
display_output_label.anchored_position = (4, 4)
30+
31+
# add the label to the main_group
32+
main_group.append(display_output_label)
33+
34+
# set the main_group as the root_group of the built-in DISPLAY
35+
board.DISPLAY.root_group = main_group
36+
# create a display object placeholder to be updated by the loop
37+
screen = (f"Distance: {''}cm, {''}in, {''}ft")
38+
# initiate repeated sensor readings
39+
vl53.start_ranging()
40+
41+
42+
# begin main loop
43+
while True:
44+
# There will be no values to populate at first, just the bare 'Distance: cm, in, ft' text
45+
# Assuming the first 'try' succeeds, this will be updated once the loop starts over
46+
display_output_label.text = screen
47+
48+
# This 'try' sequence will either update the displayed items with fresh data or repeat the
49+
# last available data. VL53L1X sensors output `None` when no object reflects the laser,
50+
# e.g., there is nothing within 4 meters, or when objects pass too quickly in and out of
51+
# view (usually perpendicular to the field of vision).
52+
try:
53+
if vl53.distance: # simple test to see there is a value to read; no value = exception
54+
distance = vl53.distance # sets the variable (used by the display) to the sensor data
55+
inches = distance*0.394 # VL53L1X outputs distance in metric, so we convert to imperial
56+
screen = (f"Distance: {distance: .1f}cm, {inches: .1f}in, {inches/12: .1f}ft")
57+
# if we made it this far, we have new data to display!
58+
except TypeError:
59+
repeat_screen = screen
60+
screen = repeat_screen
61+
# if things went sideways, we repeat the previous loop's data so we can try again
62+
63+
time.sleep(0.25)

0 commit comments

Comments
 (0)