Skip to content

Commit b3cd655

Browse files
authored
Merge pull request #41 from jposada202020/adding_displayio_example
adding displayio example
2 parents 8fb7304 + 5149b60 commit b3cd655

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ Provides an example on how to use the sensor as an inclinometer
3535
.. literalinclude:: ../examples/mpu6050_inclinometer.py
3636
:caption: examples/mpu6050_inclinometer.py
3737
:linenos:
38+
39+
40+
DisplayIO Simpletest
41+
---------------------
42+
43+
This is a simple test for boards with built-in display.
44+
45+
.. literalinclude:: ../examples/mpu6050_displayio_simpletest.py
46+
:caption: examples/mpu6050_displayio_simpletest.py
47+
:linenos:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_mpu6050
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+
mpu = adafruit_mpu6050.MPU6050(i2c)
19+
20+
21+
# Create Label(s) to show the readings. If you have a very small
22+
# display you may need to change to scale=1.
23+
display_output_acceleration = Label(FONT, text="", scale=1)
24+
display_output_gyro = Label(FONT, text="", scale=1)
25+
display_output_temperature = Label(FONT, text="", scale=1)
26+
27+
# place the label(s) in the middle of the screen with anchored positioning
28+
display_output_acceleration.anchor_point = (0, 0)
29+
display_output_acceleration.anchored_position = (
30+
4,
31+
board.DISPLAY.height // 2 - 60,
32+
)
33+
display_output_gyro.anchor_point = (0, 0)
34+
display_output_gyro.anchored_position = (
35+
4,
36+
board.DISPLAY.height // 2 - 40,
37+
)
38+
display_output_temperature.anchor_point = (0, 0)
39+
display_output_temperature.anchored_position = (
40+
4,
41+
board.DISPLAY.height // 2 - 20,
42+
)
43+
44+
45+
# add the label(s) to the main_group
46+
main_group.append(display_output_acceleration)
47+
main_group.append(display_output_gyro)
48+
main_group.append(display_output_temperature)
49+
50+
# set the main_group as the root_group of the built-in DISPLAY
51+
board.DISPLAY.root_group = main_group
52+
53+
# begin main loop
54+
while True:
55+
# update the text of the label(s) to show the sensor readings
56+
accx, accy, accz = mpu.acceleration
57+
gyro_x, gyro_y, gyro_z = mpu.gyro
58+
display_output_acceleration.text = (
59+
f"Acc X:{accx:.1f} Y:{accy:.1f} Z:{accz:.1f} m/s^2"
60+
)
61+
display_output_gyro.text = (
62+
f"Gyro X:{gyro_x:.1f} Y:{gyro_y:.1f} Z:{gyro_z:.1f} rad/s"
63+
)
64+
display_output_temperature.text = f"Temp: {mpu.temperature:.1f} C"
65+
# wait for a bit
66+
time.sleep(0.5)

0 commit comments

Comments
 (0)