|
| 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() |
| 21 | +# For using the built-in STEMMA QT connector on a microcontroller |
| 22 | +vl53 = adafruit_vl53l1x.VL53L1X(i2c) |
| 23 | + |
| 24 | +# Create a Label to show the readings. If you have a very small |
| 25 | +# display you may need to change to scale=1. |
| 26 | +display_output_label = Label(FONT, text="", scale=1) |
| 27 | + |
| 28 | +# place the label near the top left corner with anchored positioning |
| 29 | +display_output_label.anchor_point = (0, 0) |
| 30 | +display_output_label.anchored_position = (4, 4) |
| 31 | + |
| 32 | +# add the label to the main_group |
| 33 | +main_group.append(display_output_label) |
| 34 | + |
| 35 | +# set the main_group as the root_group of the built-in DISPLAY |
| 36 | +board.DISPLAY.root_group = main_group |
| 37 | +# create a display object placeholder to be updated by the loop |
| 38 | +screen = f"Distance: {''}cm, {''}in, {''}ft" |
| 39 | +# initiate repeated sensor readings |
| 40 | +vl53.start_ranging() |
| 41 | + |
| 42 | + |
| 43 | +# begin main loop |
| 44 | +while True: |
| 45 | + # There will be no values to populate at first, just the bare 'Distance: cm, in, ft' text |
| 46 | + # Assuming the first 'try' succeeds, this will be updated once the loop starts over |
| 47 | + display_output_label.text = screen |
| 48 | + |
| 49 | + # This 'try' sequence will either update the displayed items with fresh data or repeat the |
| 50 | + # last available data. VL53L1X sensors output `None` when no object reflects the laser, |
| 51 | + # e.g., there is nothing within 4 meters, or when objects pass too quickly in and out of |
| 52 | + # view (usually perpendicular to the field of vision). |
| 53 | + try: |
| 54 | + if vl53.distance: |
| 55 | + # simple test to see there is a value to read; no value = exception |
| 56 | + distance = vl53.distance |
| 57 | + # sets the variable (used by the display) to the sensor data |
| 58 | + inches = distance * 0.394 |
| 59 | + # VL53L1X outputs distance in metric, so we convert to imperial |
| 60 | + screen = f"Distance: {distance: .1f}cm, {inches: .1f}in, {inches/12: .1f}ft" |
| 61 | + # if we made it this far, we have new data to display! |
| 62 | + except TypeError: |
| 63 | + repeat_screen = screen |
| 64 | + screen = repeat_screen |
| 65 | + # if things went sideways, we repeat the previous loop's data so we can try again |
| 66 | + |
| 67 | + time.sleep(0.25) |
0 commit comments