|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 Trevor Beaton for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +import terminalio |
| 8 | +import displayio |
| 9 | +from adafruit_matrixportal.matrixportal import MatrixPortal |
| 10 | +from adafruit_display_text import label |
| 11 | + |
| 12 | +# --- Display setup --- |
| 13 | +matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True, width=64, height=64) |
| 14 | +# Use the built-in font |
| 15 | +font = terminalio.FONT |
| 16 | +# Define colors |
| 17 | +ORANGE = 0xFFA500 |
| 18 | +WHITE = 0xFFFFFF |
| 19 | +RED = 0xFF0000 |
| 20 | + |
| 21 | +# Create a Group to hold all the text areas and the heart |
| 22 | +group = displayio.Group() |
| 23 | + |
| 24 | +# Create a small heart bitmap |
| 25 | +heart_bitmap = displayio.Bitmap(7, 7, 2) |
| 26 | +heart_palette = displayio.Palette(2) |
| 27 | +heart_palette[0] = 0x000000 # Black (transparent) |
| 28 | +heart_palette[1] = RED # Red heart |
| 29 | + |
| 30 | +# Draw the heart |
| 31 | +heart_pixels = [ |
| 32 | + 0,1,1,0,1,1,0, |
| 33 | + 1,1,1,1,1,1,1, |
| 34 | + 1,1,1,1,1,1,1, |
| 35 | + 0,1,1,1,1,1,0, |
| 36 | + 0,0,1,1,1,0,0, |
| 37 | + 0,0,0,1,0,0,0, |
| 38 | +] |
| 39 | +for heart_idx, pixel in enumerate(heart_pixels): |
| 40 | + heart_bitmap[heart_idx % 7, heart_idx // 7] = pixel |
| 41 | + |
| 42 | +# Create a TileGrid using the heart bitmap and palette |
| 43 | +heart_tile = displayio.TileGrid(heart_bitmap, pixel_shader=heart_palette, x=56, y=1) |
| 44 | +group.append(heart_tile) |
| 45 | + |
| 46 | +# Add text labels for titles and values |
| 47 | +for label_idx in range(3): |
| 48 | + # Title |
| 49 | + title_label = label.Label(font, text="", color=ORANGE, x=2, y=7 + label_idx*21) |
| 50 | + group.append(title_label) |
| 51 | + # Value |
| 52 | + value_label = label.Label(font, text="", color=WHITE, x=2, y=17 + label_idx*21) |
| 53 | + group.append(value_label) |
| 54 | + |
| 55 | +# Add the group to the display |
| 56 | +matrixportal.display.show(group) |
| 57 | + |
| 58 | +# Define feed keys for your data |
| 59 | +TITLES = ["STEPS", "WORKOUTS", "MILES"] |
| 60 | +VALUE_FEEDS = ["stepcount", "numofworkouts", "distance"] |
| 61 | +UPDATE_DELAY = 1800 # Update every 30 minutes |
| 62 | + |
| 63 | +def get_feed_data(feed_key): |
| 64 | + try: |
| 65 | + data = matrixportal.get_io_data(feed_key) |
| 66 | + if data: |
| 67 | + return data[0]["value"] |
| 68 | + except (ValueError, RuntimeError) as feed_error: |
| 69 | + print(f"Error fetching data from feed {feed_key}: {feed_error}") |
| 70 | + return None |
| 71 | + |
| 72 | +def update_display(): |
| 73 | + for display_idx, (title, value_feed) in enumerate(zip(TITLES, VALUE_FEEDS)): |
| 74 | + value = get_feed_data(value_feed) or "N/A" |
| 75 | + group[display_idx*2 + 1].text = title # Update title |
| 76 | + group[display_idx*2 + 2].text = str(value) # Update value |
| 77 | + |
| 78 | +# Initial display update |
| 79 | +update_display() |
| 80 | + |
| 81 | +# Main loop |
| 82 | +while True: |
| 83 | + try: |
| 84 | + time.sleep(UPDATE_DELAY) |
| 85 | + update_display() |
| 86 | + except (ValueError, RuntimeError) as loop_error: |
| 87 | + print("Some error occurred, retrying! -", loop_error) |
| 88 | + continue |
0 commit comments