Skip to content

Commit 12f449b

Browse files
authored
Merge pull request #2862 from brentru/add-wheres-my-friend
Add files for upcoming guide, "Where's My Friend? Location Display Frame with PyPortal and ItsaSnap"
2 parents 0eba9f2 + db37f2c commit 12f449b

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# SPDX-FileCopyrightText: 2024 Brent Rubell, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import time
5+
import board
6+
import displayio
7+
import terminalio
8+
from adafruit_display_shapes.rect import Rect
9+
from adafruit_display_text import label
10+
from adafruit_pyportal import PyPortal
11+
12+
# Adafruit IO shared feed key
13+
IO_FEED_KEY = 'location'
14+
# Fetch the location every 5 seconds
15+
SLEEP_DELAY_SECONDS = 5
16+
# Set the backlight brightness, 0.0 (off) to 1.0 (max brightness)
17+
BACKLIGHT_BRIGHTNESS = 0.5
18+
# Location text and images
19+
LOCATION_IMAGES = { 'home': 'images/home.bmp', 'work': 'images/office.bmp',
20+
'gym': 'images/workout.bmp', 'commute': 'images/subway.bmp' }
21+
22+
# Create the PyPortal object
23+
pyportal = PyPortal(status_neopixel=board.NEOPIXEL)
24+
25+
# Configure the PyPortal's display
26+
display = board.DISPLAY
27+
display.rotation = 0
28+
display.brightness = BACKLIGHT_BRIGHTNESS
29+
30+
# Display label and image coordinates
31+
TEXT_AREA_X = display.width // 14
32+
TEXT_AREA_Y = 20
33+
TEXT_AREA_LOCATION_X = display.width // 3
34+
TEXT_AREA_LOCATION_Y = display.height - 20
35+
IMAGE_SPRITE_X = (display.width // 3) - 10
36+
IMAGE_SPRITE_Y = display.height // 5
37+
38+
# Create a displayIO Group
39+
group = displayio.Group()
40+
41+
# Draw the background
42+
bg_group = displayio.Group()
43+
rect = Rect(0, 0, display.width, display.height, fill=0xFFFFFF)
44+
bg_group.append(rect)
45+
group.append(bg_group)
46+
47+
# Use the default font
48+
font = terminalio.FONT
49+
50+
# Draw a label for the header text
51+
text_area = label.Label(font, text="Where is Trevor?", color=0x000000, scale=3)
52+
text_area.x = TEXT_AREA_X
53+
text_area.y = TEXT_AREA_Y
54+
group.append(text_area)
55+
56+
# Draw a label for the location text
57+
text_area_location = label.Label(font, text="@ home", color=0x000000, scale=3)
58+
text_area_location.x = TEXT_AREA_LOCATION_X
59+
text_area_location.y = TEXT_AREA_LOCATION_Y
60+
group.append(text_area_location)
61+
62+
# Create a group for the icon only
63+
icon_group = displayio.Group()
64+
group.append(icon_group)
65+
66+
# Show the group
67+
display.root_group = group
68+
69+
def set_image(image_group, filename):
70+
"""Sets the image file for a given group for display."""
71+
print(f"Set image to {filename}")
72+
if image_group:
73+
image_group.pop()
74+
75+
image_file = open(filename, "rb")
76+
image = displayio.OnDiskBitmap(image_file)
77+
image_sprite = displayio.TileGrid(image,
78+
pixel_shader=getattr(image, 'pixel_shader',
79+
displayio.ColorConverter()))
80+
image_sprite.x = IMAGE_SPRITE_X
81+
image_sprite.y = IMAGE_SPRITE_Y
82+
image_group.append(image_sprite)
83+
84+
prv_location = None
85+
while True:
86+
try:
87+
print("Fetching location data...")
88+
# Fetch the location data from Adafruit IO
89+
feed = pyportal.get_io_feed(IO_FEED_KEY)
90+
# If the location value is in the list of images
91+
if feed['last_value'] in LOCATION_IMAGES:
92+
# Check if the location has changed from the last time
93+
# we fetched the location
94+
if prv_location == feed['last_value']:
95+
print("Location has not changed!")
96+
else: # Location has changed
97+
print(f"Location: {feed['last_value']}")
98+
# Load the image for the current location
99+
set_image(icon_group, LOCATION_IMAGES[feed['last_value']])
100+
# Update the location text
101+
text_area_location.text=f"@ {feed['last_value']}"
102+
# Show the refreshed group
103+
display.root_group = group
104+
# Update the previous location
105+
prv_location = feed['last_value']
106+
else:
107+
print("Location not found in images!")
108+
# Update the location text
109+
text_area_location.text="Error: Unknown Value!"
110+
# Show the refreshed group
111+
display.root_group = group
112+
except RuntimeError as e:
113+
print("Failed to fetch location data: ", e)
114+
115+
# Wait 5 minutes (300 seconds) before fetching the location feed again
116+
print("Sleeping, fetching the location again in 5 minutes!")
117+
time.sleep(SLEEP_DELAY_SECONDS * 60)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)