|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2025 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_mpl3115a2 |
| 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 | +sensor = adafruit_mpl3115a2.MPL3115A2(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_pressure = Label(FONT, text="", scale=2) |
| 24 | +display_output_altitude = Label(FONT, text="", scale=2) |
| 25 | +display_output_temperature = Label(FONT, text="", scale=2) |
| 26 | + |
| 27 | +# place the label(s) in the middle of the screen with anchored positioning |
| 28 | +display_output_pressure.anchor_point = (0, 0) |
| 29 | +display_output_pressure.anchored_position = ( |
| 30 | + 4, |
| 31 | + board.DISPLAY.height // 2 - 60, |
| 32 | +) |
| 33 | +display_output_altitude.anchor_point = (0, 0) |
| 34 | +display_output_altitude.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_pressure) |
| 47 | +main_group.append(display_output_altitude) |
| 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 | + pressure = sensor.pressure |
| 57 | + display_output_pressure.text = f"Pressure: {pressure:.2f} hPa" |
| 58 | + altitude = sensor.altitude |
| 59 | + display_output_altitude.text = f"Altitude: {altitude:.2f} m" |
| 60 | + temperature = sensor.temperature |
| 61 | + display_output_temperature.text = f"Temperature: {temperature:.2f} C" |
| 62 | + # wait for a bit |
| 63 | + time.sleep(2) |
0 commit comments