Skip to content

Commit 4f88df7

Browse files
authored
Merge pull request #19 from johnnohj/display
DisplayIO example
2 parents 0823a84 + 9224eeb commit 4f88df7

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

.pre-commit-config.yaml

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
repos:
66
- repo: https://github.com/python/black
7-
rev: 23.3.0
7+
rev: 24.10.0
88
hooks:
99
- id: black
1010
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v1.1.2
11+
rev: v4.0.3
1212
hooks:
1313
- id: reuse
1414
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.4.0
15+
rev: v5.0.0
1616
hooks:
1717
- id: check-yaml
1818
- id: end-of-file-fixer
1919
- id: trailing-whitespace
20+
- id: mixed-line-ending
21+
args:
22+
- --fix=lf
2023
- repo: https://github.com/pycqa/pylint
21-
rev: v2.17.4
24+
rev: v3.3.1
2225
hooks:
2326
- id: pylint
2427
name: pylint (library code)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)