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
+ import adafruit_imageload
9
+ from adafruit_display_shapes .rect import Rect
10
+ from adafruit_display_text import label
11
+ from adafruit_pyportal import PyPortal
12
+
13
+ # Adafruit IO shared feed key
14
+ IO_FEED_KEY = 'location'
15
+ # Fetch the location every 5 seconds
16
+ SLEEP_DELAY_SECONDS = 5
17
+ # Set the backlight brightness, 0.0 (off) to 1.0 (max brightness)
18
+ BACKLIGHT_BRIGHTNESS = 0.5
19
+ # Location text and images
20
+ LOCATION_IMAGES = { 'home' : 'images/home.bmp' , 'work' : 'images/office.bmp' , '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 (group , filename ):
70
+ """Sets the image file for a given group for display."""
71
+ print (f"Set image to { filename } " )
72
+ if group :
73
+ group .pop ()
74
+
75
+ if not filename :
76
+ return # we're done, no icon desired
77
+ try :
78
+ if image_file :
79
+ image_file .close
80
+ except NameError :
81
+ pass
82
+ image_file = open (filename , "rb" )
83
+ image = displayio .OnDiskBitmap (image_file )
84
+ image_sprite = displayio .TileGrid (image , pixel_shader = getattr (image , 'pixel_shader' , displayio .ColorConverter ()))
85
+ image_sprite .x = IMAGE_SPRITE_X
86
+ image_sprite .y = IMAGE_SPRITE_Y
87
+ group .append (image_sprite )
88
+
89
+ prv_location = None
90
+ while True :
91
+ try :
92
+ print ("Fetching location data..." )
93
+ # Fetch the location data from Adafruit IO
94
+ feed = pyportal .get_io_feed (IO_FEED_KEY )
95
+ # If the location value is in the list of images
96
+ if feed ['last_value' ] in LOCATION_IMAGES :
97
+ # Check if the location has changed from the last time
98
+ # we fetched the location
99
+ if prv_location == feed ['last_value' ]:
100
+ print ("Location has not changed!" )
101
+ else : # Location has changed
102
+ print (f"Location: { feed ['last_value' ]} " )
103
+ # Load the image for the current location
104
+ set_image (icon_group , LOCATION_IMAGES [feed ['last_value' ]])
105
+ # Update the location text
106
+ text_area_location .text = f"@ { feed ['last_value' ]} "
107
+ # Show the refreshed group
108
+ display .root_group = group
109
+ # Update the previous location
110
+ prv_location = feed ['last_value' ]
111
+ else :
112
+ print ("Location not found in images!" )
113
+ # Update the location text
114
+ text_area_location .text = f"Error: Unknown Value!"
115
+ # Show the refreshed group
116
+ display .root_group = group
117
+ except Exception as e :
118
+ print (f"Failed to fetch location data: { e } " )
119
+
120
+ # Wait 5 minutes (300 seconds) before fetching the location feed again
121
+ print ("Sleeping, fetching the location again in 5 minutes!" )
122
+ time .sleep (SLEEP_DELAY_SECONDS * 60 )
0 commit comments