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