Repository: adafruit/Adafruit_CircuitPython_VL53L1X (language: python)

Issue:
DisplayIO Example Wanted
Add a Basic DisplayIO Based Example

We would like to have a basic displayio example for this library. The example should be written for microcontrollers with a built-in display. At a minimum it should show a Label on the display and update it with live readings from the sensor. 

The example should not be overly complex, it's intended to be a good starting point for displayio based projects that utilize this sensor library. Try to keep all visual content as near to the top left corner as possible in order to best fascilitate devices with small built-in display resolutions.

The new example should follow the naming convention `examples/libraryname_displayio_simpletest.py` with "libraryname" being replaced by the actual name of this library.

You can find an example of a Pull Request that adds this kind of example here: https://github.com/adafruit/Adafruit_CircuitPython_BME680/pull/72

We have a guide that covers the process of contributing with git and github: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github

If you're interested in contributing but need additional help, or just want to say hi, feel free to join the Discord server to ask questions: https://adafru.it/discord


Discussion / hints:

Fix patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 70ade69..4fdf32f 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -4,21 +4,24 @@
 
 repos:
   - repo: https://github.com/python/black
-    rev: 23.3.0
+    rev: 24.10.0
     hooks:
       - id: black
   - repo: https://github.com/fsfe/reuse-tool
-    rev: v1.1.2
+    rev: v4.0.3
     hooks:
       - id: reuse
   - repo: https://github.com/pre-commit/pre-commit-hooks
-    rev: v4.4.0
+    rev: v5.0.0
     hooks:
       - id: check-yaml
       - id: end-of-file-fixer
       - id: trailing-whitespace
+      - id: mixed-line-ending
+        args:
+         - --fix=lf
   - repo: https://github.com/pycqa/pylint
-    rev: v2.17.4
+    rev: v3.3.1
     hooks:
       - id: pylint
         name: pylint (library code)


Test patch:
diff --git a/examples/adafruit_vl53l1x_displayio_simpletest.py b/examples/adafruit_vl53l1x_displayio_simpletest.py
new file mode 100644
index 0000000..a00640f
--- /dev/null
+++ b/examples/adafruit_vl53l1x_displayio_simpletest.py
@@ -0,0 +1,67 @@
+# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
+# contributions by J Fletcher, adapting code by Prof Gallaugher:
+#           https://www.youtube.com/watch?v=cdx1A1xoEWc&t=5s
+# tested on ESP32-S3 Reverse TFT Feather:
+#           https://www.adafruit.com/product/5691
+# SPDX-License-Identifier: MIT
+
+import time
+import board
+from adafruit_display_text.bitmap_label import Label
+from terminalio import FONT
+from displayio import Group
+import adafruit_vl53l1x
+
+# create a main_group to hold anything we want to show on the display.
+main_group = Group()
+
+# Create sensor object, communicating over the board's default I2C bus
+# i2c = board.I2C()  # uses board.SCL and board.SDA
+i2c = board.STEMMA_I2C()
+# For using the built-in STEMMA QT connector on a microcontroller
+vl53 = adafruit_vl53l1x.VL53L1X(i2c)
+
+# Create a Label to show the readings. If you have a very small
+# display you may need to change to scale=1.
+display_output_label = Label(FONT, text="", scale=1)
+
+# place the label near the top left corner with anchored positioning
+display_output_label.anchor_point = (0, 0)
+display_output_label.anchored_position = (4, 4)
+
+# add the label to the main_group
+main_group.append(display_output_label)
+
+# set the main_group as the root_group of the built-in DISPLAY
+board.DISPLAY.root_group = main_group
+# create a display object placeholder to be updated by the loop
+screen = f"Distance: {''}cm, {''}in, {''}ft"
+# initiate repeated sensor readings
+vl53.start_ranging()
+
+
+# begin main loop
+while True:
+    # There will be no values to populate at first, just the bare 'Distance: cm, in, ft' text
+    # Assuming the first 'try' succeeds, this will be updated once the loop starts over
+    display_output_label.text = screen
+
+    # This 'try' sequence will either update the displayed items with fresh data or repeat the
+    # last available data. VL53L1X sensors output `None` when no object reflects the laser,
+    # e.g., there is nothing within 4 meters, or when objects pass too quickly in and out of
+    # view (usually perpendicular to the field of vision).
+    try:
+        if vl53.distance:
+            # simple test to see there is a value to read; no value = exception
+            distance = vl53.distance
+            # sets the variable (used by the display) to the sensor data
+            inches = distance * 0.394
+            # VL53L1X outputs distance in metric, so we convert to imperial
+            screen = f"Distance: {distance: .1f}cm, {inches: .1f}in, {inches/12: .1f}ft"
+            # if we made it this far, we have new data to display!
+    except TypeError:
+        repeat_screen = screen
+        screen = repeat_screen
+        # if things went sideways, we repeat the previous loop's data so we can try again
+
+    time.sleep(0.25)