-
Notifications
You must be signed in to change notification settings - Fork 2
/
inky-weather.py
169 lines (127 loc) · 4.88 KB
/
inky-weather.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
# coding: utf-8
import weather
from inky import InkyPHAT
import buttonshim
try:
import configparser
except ImportError:
import ConfigParser as configparser
import textwrap
import signal
import sys
import json
from datetime import datetime
from PIL import Image, ImageFont, ImageDraw
from datetime import datetime
IMAGE_DIR = "climacons"
DISPLAY_WIDTH = 212
DISPLAY_HEIGHT = 104
WHITE = 0
BLACK = 1
RED = 2
inkyphat = InkyPHAT("black")
def draw_text(base_image, text, x, y, font_size=16):
font = ImageFont.truetype("fonts/ChiKareGo.ttf", 16)
draw = ImageDraw.Draw(base_image)
w, h = font.getsize(text)
draw.text((x, y), text, inkyphat.BLACK, font)
inkyphat.set_image(base_image)
def paste_image(base_image, image_to_paste, x, y="middle"):
image = Image.open(
"{image_dir}/{image_to_paste}.png".format(
image_dir=IMAGE_DIR, image_to_paste=image_to_paste
)
)
img_w, img_h = image.size
if y == "middle":
y = (inkyphat.HEIGHT - img_h) // 2
base_image.paste(image, (x, y))
return base_image
def read_weather_json():
# weather.json is updated by calling weather.py. This can be done via
# a cronjob or something similar.
with open("weather.json") as json_data:
weather = json.load(json_data)
return weather
def show_todays_weather():
forecast = read_weather_json()
current_weather = forecast["currently"]
daily_weather = forecast["daily"]["data"][0]
# Set the data to be drawn on the display
current_temp = str(int(round(current_weather["apparentTemperature"])))
high_temp = str(int(round(daily_weather["apparentTemperatureHigh"])))
low_temp = str(int(round(daily_weather["apparentTemperatureLow"])))
# Create the base background which we will paste our images to
base_image = Image.new("P", (inkyphat.WIDTH, inkyphat.HEIGHT))
base_image = paste_image(base_image, current_weather["icon"], x=130)
inkyphat.set_image(base_image)
draw_text(base_image, current_weather["summary"], x=10, y=20)
draw_text(base_image, current_temp, x=150, y=80)
draw_text(base_image, "High: %s" % high_temp, x=10, y=60)
draw_text(base_image, "Low: %s" % low_temp, x=10, y=80)
# Draw everything
inkyphat.show()
def show_daily_weather():
forecast = read_weather_json()
weekly_weather = forecast["daily"]["data"]
today = weekly_weather[0]
tomorrow = weekly_weather[1]
overmorrow = weekly_weather[2]
# Datetime objects
today_time = datetime.fromtimestamp(today["time"]).strftime("%a")
tomorrow_time = datetime.fromtimestamp(tomorrow["time"]).strftime("%a")
overmorrow_time = datetime.fromtimestamp(overmorrow["time"]).strftime("%a")
base_image = Image.new("P", (inkyphat.WIDTH, inkyphat.HEIGHT))
base_image = paste_image(base_image, today["icon"], x=10, y="middle")
base_image = paste_image(base_image, tomorrow["icon"], x=75, y="middle")
base_image = paste_image(base_image, overmorrow["icon"], x=140, y="middle")
inkyphat.set_image(base_image)
draw_text(base_image, today_time, x=25, y=10)
draw_text(base_image, tomorrow_time, x=90, y=10)
draw_text(base_image, overmorrow_time, x=155, y=10)
today_temp_range = "{low}-{high}".format(
low=str(int(today["apparentTemperatureLow"])),
high=str(int(today["apparentTemperatureHigh"])),
)
tomorrow_temp_range = "{low}-{high}".format(
low=str(int(tomorrow["apparentTemperatureLow"])),
high=str(int(tomorrow["apparentTemperatureHigh"])),
)
overmorrow_temp_range = "{low}-{high}".format(
low=str(int(overmorrow["apparentTemperatureLow"])),
high=str(int(overmorrow["apparentTemperatureHigh"])),
)
draw_text(base_image, today_temp_range, x=25, y=83)
draw_text(base_image, tomorrow_temp_range, x=90, y=83)
draw_text(base_image, overmorrow_temp_range, x=155, y=83)
# Draw everything
inkyphat.show()
@buttonshim.on_press(buttonshim.BUTTON_A, repeat=False)
def button_todays_weather(button, pressed):
show_todays_weather()
@buttonshim.on_press(buttonshim.BUTTON_B, repeat=False)
def button_daily_weather(button, pressed):
show_daily_weather()
# Gracefully stop on Ctrl-C
def signal_handler(signal, frame):
print("\nGracefully closing, please wait a moment")
sys.exit(0)
def main():
# Setup the e-ink display
print("Setting everything up...")
inkyphat.set_border(inkyphat.WHITE)
config = configparser.ConfigParser()
config.read("config.py")
weather.get_weather_json()
# Support for Button SHIM
if config.getboolean("raspberry_pi", "button_shim"):
print("Using button shim support")
show_todays_weather()
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
else:
# Show today's weather on the inkyphat
print("No button shim - showing weather")
show_todays_weather()
if __name__ == "__main__":
main()