-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-weather-display-vertical.py
395 lines (348 loc) · 15.6 KB
/
simple-weather-display-vertical.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from PIL import Image, ImageDraw, ImageFont, ImageColor
from epdconfig import *
from fonts import *
from settings import *
import epd_7_in_5 as driver
background_colour = 'white'
text_colour = 'black'
display_width, display_height = driver.EPD_HEIGHT, driver.EPD_WIDTH
image = Image.new('RGB', (display_width, display_height), background_colour)
## DEFINE THE BOUNDS OF THE CURRENT CONFIG, imported from settings.py
b_left = left_edge
b_right = right_edge
b_top = top_edge
b_bottom = bottom_edge
width = int(b_right - b_left) # this width and height is the width and height of inside the picture frame mat
height = int(b_bottom - b_top)
print('inside of picture frame mat is', width, 'wide by', height, 'high')
d_image = Image.new('RGB', (width, height), background_colour) # creating a smaller image, based on the inside of the mat
draw = ImageDraw.Draw(d_image)
## DEFINE FONTS
regular_font_path = '/home/pi/simple-weather/fonts/NotoSans-hinted/NotoSans-Condensed.ttf'
summary_font_path = '/home/pi/simple-weather/fonts/NotoSans-hinted/NotoSans-CondensedItalic.ttf'
hilo_font_path = '/home/pi/simple-weather/fonts/NotoSans-hinted/NotoSans-Condensed.ttf'
weather_font_path = '/home/pi/simple-weather/fonts/WeatherFont/weathericons-regular-webfont.ttf'
font = ImageFont.truetype(regular_font_path, 20)
## GET WEATHER INFO ##
import requests
import json
import math
if debugging_choice_weather == True:
icon = 'rain'
# icon = 'clear-day'
hi = 77.3
lo = 68
hi_time = 1599935700
lo_time = 1599935700
today_summary = "Rain (with a chance of 2-4 in. of snow) starting in the afternoon."
#today_summary = "Rain"
week_summary = "Light rain on Thursday through next Monday."
else:
r = requests.get('https://api.darksky.net/forecast/'+dark_sky_api_key+'/'+dark_sky_coords)
weather = r.json()
# print(json.dumps(weather, indent=4))
weather_file_path = "/home/pi/simple-weather/forecast.json"
with open(weather_file_path, 'w') as outfile:
json.dump(weather, outfile)
icon = weather['daily']['data'][0]['icon']
hi = weather['daily']['data'][0]['temperatureHigh']
lo = weather['daily']['data'][0]['temperatureLow']
hi_time = weather['daily']['data'][0]['temperatureHighTime']
lo_time = weather['daily']['data'][0]['temperatureLowTime']
today_summary = weather['daily']['data'][0]['summary']
week_summary = weather['daily']['summary']
## GET DATE INFO ##
import datetime
dayy = datetime.datetime.now().strftime('%d')
if dayy[-1] == "1" and dayy[0] != "1":
end = "st"
elif dayy[-1] == "2" and dayy[0] != "1":
end = "nd"
elif dayy[-1] == "3" and dayy[0] != "1":
end = "rd"
else:
end = "th"
todayis_string = "Today is "+datetime.datetime.now().strftime('%A')+","
date_string = datetime.datetime.now().strftime('%B %-d')+end
hilo_string = 'High of '+str(round(hi))+'°, low of '+str(round(lo))+'°'
summary_string = today_summary.replace(".", "")
allowed_width = int(width*0.75)
## GET FONT SIZE ##
font_size = 20
consistent_strings = [todayis_string, date_string, hilo_string]
max_size = 0
while max_size <= allowed_width:
max_size = 0
for line in consistent_strings:
cs = ImageFont.truetype(regular_font_path, font_size).getsize(line)
if cs[0] > max_size:
max_size = cs[0]
font_size = font_size + 2
font = ImageFont.truetype(regular_font_path, font_size)
print(font_size)
## SPLIT FONT INTO TWO LINES IF NECESSARY ##
# the summary message is frequently too long to fit on one line.
# summary_font = ImageFont.truetype(summary_font_path, font_size)
summary_font = ImageFont.truetype(hilo_font_path, font_size)
hilo_font = ImageFont.truetype(summary_font_path, font_size)
summary_first_size = summary_font.getsize(summary_string)
# test to see if the width of the summary line is larger than the allowed width.
if summary_first_size[0] > allowed_width:
print('ooh, one line is not enough for the weather description, spilling onto two')
# if it is, split it into two lines by finding the first space before the middle and the first space after the middle of the string
summary_find = [
summary_string[0:summary_string.find(" ",int(len(summary_string)/2))],
summary_string[summary_string.find(" ",int(len(summary_string)/2))+1:len(summary_string)]
]
summary_rfind = [
summary_string[0:summary_string.rfind(" ",0,int(len(summary_string)/2))],
summary_string[summary_string.rfind(" ",0,int(len(summary_string)/2))+1:len(summary_string)]
]
summary_find_diff = abs(summary_font.getsize(summary_find[0])[0] - summary_font.getsize(summary_find[1])[0])
summary_rfind_diff = abs(summary_font.getsize(summary_rfind[0])[0] - summary_font.getsize(summary_rfind[1])[0])
print(summary_find_diff, summary_rfind_diff)
# we're going to find the one where the two lines are closest in length and pick that one
if summary_find_diff <= summary_rfind_diff:
summary_array = summary_find
else:
summary_array = summary_rfind
else:
# if it's not longer than the allowed width, we'll keep it on one line
summary_array = [summary_string]
if len(summary_array) == 2:
if summary_font.getsize(summary_array[0])[0] > allowed_width or summary_font.getsize(summary_array[1])[0] > allowed_width:
print('wow, two lines are not enough for the weather description, spilling onto three')
summary_find = [
summary_string[0:summary_string.find(" ",int(len(summary_string)/3))],
summary_string[summary_string.find(" ",int(len(summary_string)/3))+1:summary_string.find(" ",int(len(summary_string)/3*2))],
summary_string[summary_string.find(" ",int(len(summary_string)/3*2))+1:len(summary_string)]
]
summary_rfind = [
summary_string[0:summary_string.rfind(" ",0,int(len(summary_string)/3))],
summary_string[summary_string.rfind(" ",0,int(len(summary_string)/3))+1:summary_string.rfind(" ",0,int(len(summary_string)/3*2))],
summary_string[summary_string.rfind(" ",0,int(len(summary_string)/3*2))+1:len(summary_string)]
]
a, b, c = summary_font.getsize(summary_find[0])[0], summary_font.getsize(summary_find[1])[0], summary_font.getsize(summary_find[2])[0]
ra, rb, rc = summary_font.getsize(summary_rfind[0])[0], summary_font.getsize(summary_rfind[1])[0], summary_font.getsize(summary_rfind[2])[0]
summary_find_diff = (abs(a-b)+abs(b-c)+abs(c-a))/3
summary_rfind_diff = (abs(ra-rb)+abs(rb-rc)+abs(rc-ra))/3
if summary_find_diff <= summary_rfind_diff:
summary_array = summary_find
else:
summary_array = summary_rfind
summary_find_diff = abs(summary_font.getsize(summary_find[0])[0] - summary_font.getsize(summary_find[1])[0])
summary_rfind_diff = abs(summary_font.getsize(summary_rfind[0])[0] - summary_font.getsize(summary_rfind[1])[0])
summary_font_size = font_size
for i in summary_array:
while summary_font.getsize(i)[0] > allowed_width and summary_font_size != 0:
summary_font_size = summary_font_size - 1
print("and the font is still too big. making it smaller. font is now", summary_font_size)
summary_font = ImageFont.truetype(hilo_font_path, summary_font_size)
### GET WEATHER ICON ###
iconmap = {
'clear-day':
{
'icon':'\uf00d',
'y-correct': 10/60
},
'clear-night':
{
'icon':'\uf02e',
'y-correct': 51/150
},
'rain':
{
'icon':'\uf019',
'y-correct': 21/60
},
'snow':
{
'icon':'\uf01b',
'y-correct': 21/60
},
'sleet':
{
'icon':'\uf0b5',
'y-correct': 51/150
},
'wind':
{
'icon':'\uf050',
'y-correct': 25/60
},
'cloudy':
{
'icon':'\uf013',
'y-correct': 51/150
},
'partly-cloudy-day':
{
'icon':'\uf002',
'y-correct': 0
},
'partly-cloudy-night':
{
'icon':'\uf031',
'y-correct':29/100
},
'hail':
{
'icon':'\uf015',
'y-correct': 21/60
},
'thunderstorm':
{
'icon':'\uf01e',
'y-correct': 52/150
},
'tornado':
{
'icon':'\uf056',
'y-correct': 51/150
},
'other':
{
'icon':'\uf053',
'y-correct': 14/150
}
}
try:
icon_json = iconmap[icon]
except:
icon_json = iconmap['other']
## DEFINE WHERE TEXT IS TO BE DRAWN
text_to_display = []
line_height = 5
small_gap = 12
large_gap = 30
# Today is Thursday
text_to_display = [
{
"text": todayis_string,
"font": font,
"top": 0,
"bottom": 0+font.getsize(todayis_string)[1] - 11,
"left": int(width/2 - font.getsize(todayis_string)[0]/2),
"size": font.getsize(todayis_string),
"ycorrect": 11
}
]
# September 10th
text_to_display.append(
{
"text": date_string,
"font": font,
"top": text_to_display[0]["bottom"] + small_gap,
"bottom": text_to_display[0]["bottom"] + small_gap + font.getsize(date_string)[1] - 11,
"left": int(width/2 - font.getsize(date_string)[0]/2),
"size": font.getsize(date_string),
"ycorrect": 11
}
)
# weather icon
icon_size = 80
weather_font = ImageFont.truetype(weather_font_path, icon_size)
text_to_display.append(
{
"text": icon_json['icon'],
"font": weather_font,
"top": text_to_display[1]["bottom"] + large_gap,
"bottom": text_to_display[1]["bottom"] + large_gap - icon_json['y-correct']*icon_size + weather_font.getsize(icon_json['icon'])[1],
"left": int(width/2 - weather_font.getsize(icon_json['icon'])[0]/2),
"size": (weather_font.getsize(icon_json['icon'])[0], weather_font.getsize(icon_json['icon'])[1] - icon_json['y-correct']*icon_size),
"ycorrect": icon_json['y-correct']*icon_size
}
)
# Hi of 77, low of 68
text_to_display.append(
{
"text": hilo_string,
"font": hilo_font,
"top": text_to_display[2]["bottom"] + large_gap,
"bottom": text_to_display[2]["bottom"] + large_gap + hilo_font.getsize(hilo_string)[1] - 11,
"left": int(width/2 - hilo_font.getsize(hilo_string)[0]/2),
"size": hilo_font.getsize(hilo_string),
"ycorrect": 11
}
)
# Rain and humid
# throughout the day
counter = 3
for sum in summary_array:
if counter == 3:
text_to_display.append(
{
"text": sum,
"font": summary_font,
"top": text_to_display[counter]["bottom"] + small_gap*1.5,
"bottom": text_to_display[counter]["bottom"] + small_gap*1.5 + summary_font.getsize(sum)[1] - 11,
"left": int(width/2 - summary_font.getsize(sum)[0]/2),
"size": summary_font.getsize(sum),
"ycorrect": 11
}
)
else:
text_to_display.append(
{
"text": sum,
"font": summary_font,
"top": text_to_display[counter]["bottom"] + small_gap,
"bottom": text_to_display[counter]["bottom"] + small_gap + summary_font.getsize(sum)[1] - 11,
"left": int(width/2 - summary_font.getsize(sum)[0]/2),
"size": summary_font.getsize(sum),
"ycorrect": 11
}
)
counter = counter + 1
y_start = int(height/2 - text_to_display[-1]['bottom']/2)
## draw all the text onto the display
for item in text_to_display:
draw.text((item['left'], item['top']+y_start-item['ycorrect']), item['text'], text_colour, font = item['font'])
## Add a last updated line, if requested
if last_updated_choice == True:
last_updated_font_size = 14
nowtext = datetime.datetime.now().strftime("%Y-%m%-d %H:%M")
last_updated_text = "Last updated at "+nowtext
last_updated_font = ImageFont.truetype(regular_font_path, last_updated_font_size)
last_updated_size = last_updated_font.getsize(last_updated_text)
draw.text((width - last_updated_size[0], height - last_updated_size[1]),last_updated_text, text_colour,last_updated_font)
## PASTE THE SMALLER INSIDE IMAGE ONTO THE FULL SCREEN
# box_draw.line(((0,0), (100, 0)), 'black', width=3)
image.paste(d_image, (b_left, b_top))
## Draw the calibration boxes (to check numbers) onto full screen, if requested
box_draw = ImageDraw.Draw(image)
if check_calibration_choice == True:
box_draw.line(((b_left + width/2, b_top), (b_left+width/2, b_bottom)), 'black', width=3)
for item in text_to_display:
print(y_start)
print(item)
box_draw.line(((b_left, b_top+y_start+item['top']), (b_right, b_top+y_start+item['top'])), 'black', width=1)
box_draw.line(((b_left, b_top+y_start+item['bottom']), (b_right, b_top+y_start+item['bottom'])), 'black', width=1)
# weather_point = (weather_point[0]+b_left, weather_point[1]+b_top)
# box_draw.line((weather_point, (weather_point[0], weather_point[1]+weather_size[1])), 'black', width=3)
# box_draw.line((weather_point, (weather_point[0]+weather_size[0], weather_point[1])), 'black', width=3)
# box_draw.line(((weather_point[0]+weather_size[0], weather_point[1]), (weather_point[0]+weather_size[0], weather_point[1]+weather_size[1])), 'black', width=3)
# box_draw.line(((weather_point[0], weather_point[1]+weather_size[1]), (weather_point[0]+weather_size[0], weather_point[1]+weather_size[1])), 'black', width=3)
if display_outside_bounds_choice == True:
box_draw.line(((b_left,b_top),(b_left,b_bottom)),'black', width = 3)
box_draw.line(((b_right,b_top),(b_right,b_bottom)),'black', width = 3)
box_draw.line(((b_left,b_top),(b_right,b_top)),'black', width = 3)
box_draw.line(((b_left,b_bottom),(b_right,b_bottom)),'black', width = 3)
# box_draw.line(((0,0),(0,display_height)),'black', width = 3)
# box_draw.line(((display_width,0),(display_width,display_height)),'black', width = 3)
# box_draw.line(((0,0),(display_width,0)),'black', width = 3)
# box_draw.line(((0,display_height),(display_width,display_height)),'black', width = 3)
## SAVE TO FILE
image.save('/home/pi/test.png')
print('image saved')
if debugging_choice_screen == False:
# PRINT ONTO SCREEN
epaper = driver.EPD()
print('Initialising E-Paper...', end = '')
epaper.init()
print('Done')
print('Sending image data and refreshing display...', end='')
# epaper.display(epaper.getbuffer(image), epaper.getbuffer(image_col))
epaper.display(epaper.getbuffer(image))
print('Done')
epaper.sleep()