Skip to content

Commit 5d50d69

Browse files
authored
Merge pull request #2884 from adafruit/Matrix_S3_itsaSNAP_Daily_Forecast
Added itsaSNAP project contents
2 parents 7601d79 + 8aaa601 commit 5d50d69

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed
Binary file not shown.
8.13 KB
Binary file not shown.
8.13 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# SPDX-FileCopyrightText: 2024 Trevor Beaton for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import terminalio
7+
from adafruit_matrixportal.matrixportal import MatrixPortal
8+
9+
# --- Display setup ---
10+
matrixportal = MatrixPortal(width=64, height=32, bit_depth=6, debug=True)
11+
12+
# Create a label for the temperature
13+
matrixportal.add_text(
14+
text_font=terminalio.FONT,
15+
text_position=(33, 24), # Positioned on the right side, near the bottom
16+
scrolling=False,
17+
)
18+
19+
# Create a label for the weather condition
20+
matrixportal.add_text(
21+
text_font=terminalio.FONT,
22+
text_position=(33, 8), # Positioned on the right side, above the temperature
23+
scrolling=False,
24+
)
25+
26+
# Dictionary mapping weather conditions to BMP filenames
27+
WEATHER_IMAGES = {
28+
"Sunny": "images/sunny.bmp",
29+
"Clear": "images/moon.bmp",
30+
"Cldy": "images/cloudy.bmp", # Updated to use shortened version
31+
"Drizzle": "images/rain.bmp",
32+
"Rainy": "images/cloudy.bmp",
33+
"Heavy rain": "images/rain.bmp",
34+
"TStorms": "images/thunder.bmp",
35+
"Sun showers": "images/rain.bmp",
36+
"Snow": "images/snow.bmp",
37+
}
38+
39+
# Update this to your weather feed
40+
WEATHER_FEED = "weather-feed"
41+
UPDATE_DELAY = 1800 # 30 minutes
42+
43+
def get_last_data(feed_key):
44+
try:
45+
data = matrixportal.get_io_data(feed_key)
46+
if data:
47+
return data[0]["value"]
48+
except (KeyError, IndexError) as e:
49+
print(f"Error fetching data from feed {feed_key}: {e}")
50+
return None
51+
52+
def is_daytime(hour):
53+
return 5 <= hour < 18 # True if between 5:00 AM and 5:59 PM
54+
55+
def clean_condition(condition, is_day):
56+
condition = condition.replace("Mostly ", "").replace("Partly ", "")
57+
condition_mapping = {
58+
"Cloudy": "Cldy", # Added shortened version of Cloudy
59+
"Drizzle or light rain": "Rainy",
60+
"Heavy rain": "Rainy",
61+
"Isolated thunderstorms": "TStorms",
62+
"Sun showers": "Rainy",
63+
"Scattered thunderstorms": "TStorms",
64+
"Strong storms": "TStorms",
65+
"Light snow": "Snow",
66+
"Heavy snow": "Snow",
67+
}
68+
if condition == "Sunny" and not is_day:
69+
return "Clear"
70+
return condition_mapping.get(condition, condition)
71+
72+
def parse_weather_data(data):
73+
try:
74+
_, weather_info = data.split(" at ")
75+
time_str, weather_data = weather_info.split(" ", 1)
76+
hour = int(time_str.split(":")[0])
77+
if "PM" in time_str and hour != 12:
78+
hour += 12
79+
elif "AM" in time_str and hour == 12:
80+
hour = 0
81+
temperature, condition = weather_data.split(" and ")
82+
return hour, temperature, condition
83+
except ValueError as e:
84+
print(f"Error parsing weather data: {e}")
85+
return None, None, None
86+
87+
def update_display():
88+
weather_data = get_last_data(WEATHER_FEED)
89+
if weather_data:
90+
hour, temperature, condition = parse_weather_data(weather_data)
91+
if hour is not None and temperature is not None and condition is not None:
92+
is_day = is_daytime(hour)
93+
current_condition = clean_condition(condition, is_day)
94+
95+
matrixportal.set_text(temperature, 0)
96+
matrixportal.set_text(current_condition, 1)
97+
98+
# Determine which image to show based on condition and time
99+
if current_condition == "Sunny" and is_day:
100+
image_key = "images/sunny.bmp"
101+
elif current_condition == "Clear" or (current_condition == "Sunny" and not is_day):
102+
image_key = "images/moon.bmp"
103+
else:
104+
image_key = WEATHER_IMAGES.get(current_condition, "images/sunny.bmp")
105+
106+
try:
107+
matrixportal.set_background(image_key)
108+
except OSError as e:
109+
print(f"Error loading image for {current_condition}: {e}")
110+
else:
111+
print(f"Failed to parse weather data: {weather_data}")
112+
matrixportal.set_text("Error", 0)
113+
matrixportal.set_text("", 1)
114+
else:
115+
print("Failed to retrieve data from feed")
116+
matrixportal.set_text("No Data", 0)
117+
matrixportal.set_text("", 1)
118+
119+
last_update = time.monotonic()
120+
update_display()
121+
122+
# Main loop
123+
while True:
124+
current_time = time.monotonic()
125+
if current_time - last_update > UPDATE_DELAY:
126+
update_display()
127+
last_update = current_time
128+
129+
time.sleep(1) # Sleep for 1 second

0 commit comments

Comments
 (0)