-
Notifications
You must be signed in to change notification settings - Fork 0
/
openweather_graphics.py
215 lines (182 loc) · 7.29 KB
/
openweather_graphics.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# NOTE: This file is mostly a copy of
# - https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/701e93774c94edf440936e2fe2a5d3eb2015fa52/PyPortal_OpenWeather/openweather_graphics.py
import json
import time
import displayio
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.label import Label
cwd = ("/" + __file__).rsplit("/", 1)[
0
] # the current working directory (where this file is)
small_font = cwd + "/fonts/Arial-12.bdf"
medium_font = cwd + "/fonts/Arial-16.bdf"
large_font = cwd + "/fonts/Arial-Bold-24.bdf"
large_font2 = cwd + "/fonts/Helvetica-Bold-16.bdf"
class OpenWeather_Graphics(displayio.Group):
def __init__(self, root_group, *, am_pm=True, celsius=True):
super().__init__()
self.am_pm = am_pm
self.celsius = celsius
root_group.append(self)
self._icon_group = displayio.Group()
self.append(self._icon_group)
self._filename_cache = ""
self._text_group = displayio.Group()
self.append(self._text_group)
self._icon_sprite = None
self._icon_file = None
self.set_icon(cwd + "/init_background.bmp")
self.small_font = bitmap_font.load_font(small_font)
self.medium_font = bitmap_font.load_font(medium_font)
self.large_font = bitmap_font.load_font(large_font)
self.large_font2 = bitmap_font.load_font(large_font2)
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: "
self.small_font.load_glyphs(glyphs)
self.medium_font.load_glyphs(glyphs)
self.medium_font.load_glyphs(("°",)) # a non-ascii character we need for sure
self.large_font.load_glyphs(glyphs)
self.large_font.load_glyphs(("°",)) # a non-ascii character we need for sure
self.large_font2.load_glyphs(glyphs)
self.large_font2.load_glyphs(("()",)) # additional on large_font2
self.city_text = None
self.time_text = Label(self.large_font)
self.time_text.x = 100
self.time_text.y = 120
self.time_text.color = 0xFFFF00
self._text_group.append(self.time_text)
self.cal_text = Label(self.medium_font)
self.cal_text.x = 120
self.cal_text.y = 16
self.cal_text.color = 0xFFFFFF
self._text_group.append(self.cal_text)
self.wind_text = Label(self.small_font)
self.wind_text.x = 120
self.wind_text.y = 40
self.wind_text.color = 0x3366FF
self._text_group.append(self.wind_text)
self.out_temp_text = Label(self.medium_font)
self.out_temp_text.x = 214
self.out_temp_text.y = 195
self.out_temp_text.color = 0xFFC0C0
self._text_group.append(self.out_temp_text)
self.in_temp_text = Label(self.medium_font)
self.in_temp_text.x = 214 + 7
self.in_temp_text.y = 220
self.in_temp_text.color = 0x00FFFF
self._text_group.append(self.in_temp_text)
self.main_text = Label(self.medium_font) # Clouds
self.main_text.x = 10
self.main_text.y = 195
self.main_text.color = 0xFF6600
self._text_group.append(self.main_text)
self.description_text = Label(self.small_font) # Broken Clouds
self.description_text.x = 10
self.description_text.y = 225
self.description_text.color = 0xFFFFFF
self._text_group.append(self.description_text)
def display_inside_temp(self, temperature):
print(f"inside temperature is {temperature} °F")
if self.celsius:
self.in_temp_text.text = " in: %d °C" % ((temperature - 32) * 5 / 9)
else:
self.in_temp_text.text = " in: %d °F" % temperature
def display_weather(self, weather):
weather = json.loads(weather)
# set the icon/background
# https://openweathermap.org/weather-conditions
weather_icon = weather["weather"][0]["icon"]
self.set_icon(cwd + "/icons/" + weather_icon + ".bmp")
city_name = weather["name"] + ", " + weather["sys"]["country"]
print(city_name)
# if not self.city_text:
# self.city_text = Label(self.medium_font, text=city_name)
# self.city_text.x = 10
# self.city_text.y = 12
# self.city_text.color = 0xFFFFFF
# self._text_group.append(self.city_text)
self.update_time()
main_text = weather["weather"][0]["main"]
print(main_text)
self.main_text.text = main_text
temperature = weather["main"]["temp"] # its... units=imperial
print(temperature)
if self.celsius:
self.out_temp_text.text = "out: %d °C" % ((temperature - 32) * 5 / 9)
else:
self.out_temp_text.text = "out: %d °F" % temperature
description = weather["weather"][0]["description"]
description = description[0].upper() + description[1:]
print(description)
self.description_text.text = description
# "thunderstorm with heavy drizzle"
wind_speed = int(weather["wind"]["speed"])
print(f"wind speed: {wind_speed}")
self.wind_text.text = f"Wind: {wind_speed} mph"
def update_time(self):
"""Fetch the time.localtime(), parse it out and update the display text"""
now = time.localtime()
hour = now[3]
minute = now[4]
format_str = "%d:%02d"
if self.am_pm:
if hour >= 12:
hour -= 12
format_str = format_str + " pm"
else:
format_str = format_str + " am"
if hour == 0:
hour = 12
time_str = format_str % (hour, minute)
self.time_text.text = time_str
wday = {
0: "Mon",
1: "Tue",
2: "Wed",
3: "Thu",
4: "Fri",
5: "Sat",
6: "Sun",
}.get(now.tm_wday, "")
month = {
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec",
}.get(now.tm_mon)
cal_str = f"{wday}, {now.tm_mday}/{month}/{now.tm_year}"
self.cal_text.text = cal_str
print(f"{cal_str} {time_str}")
def set_icon(self, filename):
"""The background image to a bitmap file.
:param filename: The filename of the chosen icon
"""
print("Set icon to ", filename)
if self._filename_cache == filename:
return # we're done, no filename changes
if self._icon_group:
self._icon_group.pop()
if not filename:
self._filename_cache = filename
return # we're done, no icon desired
if self._icon_file:
self._icon_file.close()
self._icon_file = open(filename, "rb")
icon = displayio.OnDiskBitmap(self._icon_file)
try:
self._icon_sprite = displayio.TileGrid(
icon, pixel_shader=displayio.ColorConverter()
)
except TypeError:
self._icon_sprite = displayio.TileGrid(
icon, pixel_shader=displayio.ColorConverter(), position=(0, 0)
)
self._icon_group.append(self._icon_sprite)
self._filename_cache = filename