-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-weather-display.py
192 lines (158 loc) · 7.06 KB
/
simple-weather-display.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
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_WIDTH, driver.EPD_HEIGHT
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)
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'
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
curr_temp = 76
else:
r = requests.get('https://api.darksky.net/forecast/'+dark_sky_api_key+'/'+dark_sky_coords)
weather = r.json()
weather_file_path = "/home/pi/simple-weather/forecast.json"
with open(weather_file_path, 'w') as outfile:
json.dump(weather, outfile)
curr_temp = weather['currently']['temperature']
icon = weather['currently']['icon']
hi = weather['daily']['data'][0]['temperatureHigh']
lo = weather['daily']['data'][0]['temperatureLow']
## 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"
now_string = datetime.datetime.now().strftime('%A, %B %-d')+end
display_text = ["",
'Today is '+now_string,
"",
'High of '+str(round(hi))+'°, low of '+str(round(lo))+'°',
""]
allowed_width = int(width*0.8)
## GET FONT SIZE ##
font_size = 20
max_size = 0
while max_size <= allowed_width:
max_size = 0
for line in display_text:
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)
line_height = int(height/len(display_text))
## DRAW THE TEXT TO DISPLAY ##
for line in range(0,len(display_text)):
x = int((width - font.getsize(display_text[line])[0])/2)
font_height = font.getsize(display_text[line])[1]
y = int(height//len(display_text)*line) + int((line_height - font_height)/2)
draw.text((x, y),display_text[line], text_colour, font = font)
### DRAW WEATHER ###
iconmap = {
'clear-day':'\uf00d', 'clear-night':'\uf02e',
'rain':'\uf019', 'snow':'\uf01b',
'sleet':'\uf0b5', 'wind':'\uf050',
'cloudy':'\uf013', 'partly-cloudy-day': '\uf002',
'partly-cloudy-night': '\uf031', 'hail':'\uf015',
'thunderstorm':'\uf01e', 'tornado':'\uf056',
'other':'\uf053'
}
try:
icon_text = iconmap[icon]
except:
icon_text = iconmap['other']
temp_text = str(round(curr_temp))+"°"
### FIND ICON FONT SIZE ###
icon_font = 40
size = ImageFont.truetype(weather_font_path, icon_font).getsize(icon_text)[1]
while size < line_height*0.8:
size = ImageFont.truetype(weather_font_path, icon_font).getsize(icon_text)[1]
icon_font = icon_font + 2
### FIND TEMP FONT SIZE ###
temp_font = 40
size = ImageFont.truetype(regular_font_path, temp_font).getsize(temp_text)[1]
while size < line_height*0.8:
size = ImageFont.truetype(regular_font_path, temp_font).getsize(temp_text)[1]
temp_font = temp_font + 2
weather_font = ImageFont.truetype(weather_font_path, icon_font)
weather_size = weather_font.getsize(icon_text)
temp_font_font = ImageFont.truetype(regular_font_path, temp_font)
temp_size = temp_font_font.getsize(temp_text)
## DOING A BUNCH OF CALCULATIONS FOR THE CENTER ICON AND TEMP TEXT
"""
The center line was stupid complicated. I had to combine the icon (which is in the
weather font) with the temperature (which is in the regular font).
I took the sizes of the icon and the temp text and added a gap between them and then
centered them as best I could.
"""
gap_width = 10
weather_start = int((width - (weather_size[0]+gap_width+temp_size[0]))/2)
temp_start = weather_start + weather_size[0] + gap_width
line3start = int(height//len(display_text)*2)
weather_correction = 3
size_correction = 5
draw.text((weather_start, line3start + int((line_height - weather_size[1])/2)+weather_correction-size_correction),icon_text, text_colour, font = weather_font)
draw.text((temp_start, line3start + int((line_height - temp_size[1])/2)-size_correction),temp_text, text_colour, font = temp_font_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
image.paste(d_image, (b_left, b_top))
## Draw the calibration boxes (to check numbers) onto full screen, if requested
if check_calibration_choice == True:
box_draw = ImageDraw.Draw(image)
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')
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()