Skip to content

Commit 5fa4862

Browse files
committed
adding_displayio_example
1 parent a24b65a commit 5fa4862

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ Show how to setup Relative Humidity and Temperature Sensor Resolutions
1515
.. literalinclude:: ../examples/htu31d_setting_resolutions.py
1616
:caption: examples/htu31d_setting_resolutions.py
1717
:linenos:
18+
19+
DisplayIO Simpletest
20+
---------------------
21+
22+
This is a simple test for boards with built-in display.
23+
24+
.. literalinclude:: ../examples/htu31d_displayio_simpletest.py
25+
:caption: examples/htu31d_displayio_simpletest.py
26+
:linenos:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
import time
7+
import board
8+
from adafruit_display_text.bitmap_label import Label
9+
from terminalio import FONT
10+
from displayio import Group
11+
import adafruit_htu31d
12+
13+
# Simple demo of using the built-in display.
14+
# create a main_group to hold anything we want to show on the display.
15+
main_group = Group()
16+
# Initialize I2C bus and sensor.
17+
i2c = board.I2C() # uses board.SCL and board.SDA
18+
htu = adafruit_htu31d.HTU31D(i2c)
19+
20+
# Create Label(s) to show the readings. If you have a very small
21+
# display you may need to change to scale=1.
22+
display_output_label = Label(FONT, text="", scale=2)
23+
24+
# place the label(s) in the middle of the screen with anchored positioning
25+
display_output_label.anchor_point = (0, 0)
26+
display_output_label.anchored_position = (
27+
4,
28+
board.DISPLAY.height // 2 - 60,
29+
)
30+
31+
# add the label(s) 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+
37+
# begin main loop
38+
while True:
39+
# update the text of the label(s) to show the sensor readings
40+
temperature, relative_humidity = htu.measurements
41+
display_output_label.text = (
42+
f"Temperature: {temperature:.1f} C"
43+
+ "\n"
44+
+ f"Humidity: {relative_humidity:.1f} %"
45+
)
46+
# wait for a bit
47+
time.sleep(0.5)

0 commit comments

Comments
 (0)