-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdarksky.py
157 lines (134 loc) · 5.56 KB
/
darksky.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
import time
import json
import board
import displayio
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
from adafruit_display_text.label import Label
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_bitmap_font import bitmap_font
from secrets import secrets
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"
class darksky(displayio.Group):
def __init__(self, root_group, *, am_pm=True, celsius=True):
super().__init__(max_size=2)
self.am_pm = am_pm
self.celsius = celsius
root_group.append(self)
self._icon_group = displayio.Group(max_size=1)
self.append(self._icon_group)
self._text_group = displayio.Group(max_size=5)
self.append(self._text_group)
self._icon_sprite = None
self._icon_file = None
self.set_icon(cwd+"/weather_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)
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
self.small_font.load_glyphs(glyphs)
self.medium_font.load_glyphs(glyphs)
self.large_font.load_glyphs(glyphs)
self.large_font.load_glyphs(('°',)) # a non-ascii character we need for sure
self.city_text = None
self.time_text = Label(self.medium_font, max_glyphs=8)
self.time_text.x = 220
self.time_text.y = 25
self.time_text.color = 0xFFFFFF
self._text_group.append(self.time_text)
self.temp_text = Label(self.medium_font, max_glyphs=6)
self.temp_text.x = 230
self.temp_text.y = 205
self.temp_text.color = 0xFFFFFF
self._text_group.append(self.temp_text)
self.main_text = Label(self.medium_font, max_glyphs=20)
self.main_text.x = 10
self.main_text.y = 205
self.main_text.color = 0xFFFFFF
self._text_group.append(self.main_text)
self.description_text = Label(self.small_font, max_glyphs=60)
self.description_text.x = 10
self.description_text.y = 225
self.description_text.color = 0xFFFFFF
self._text_group.append(self.description_text)
def display_weather(self, weather):
weather = json.loads(weather)
# set the icon/background
weather_icon = weather['currently']['icon']
self.set_icon(cwd+"/icons/"+weather_icon+".bmp")
city_name = secrets['CITY_NAME']
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 = 25
self.city_text.color = 0xFFFFFF
self._text_group.append(self.city_text)
self.update_time()
main_text = weather['currently']['summary']
print(main_text)
self.main_text.text = main_text
temperature = weather['currently']['temperature']
print(temperature)
self.temp_text.text = "%d °F" % temperature
humidity = weather['currently']['humidity']
uv_index = weather['currently']['uvIndex']
icon = weather['currently']['icon']
url = 'https://groker.init.st/api/events'
headers = {
'Content-Type': 'application/json',
'X-IS-AccessKey': secrets['ACCESS_KEY'],
'X-IS-BucketKey': secrets['BUCKET_KEY'],
'Accept-Version': '~0'
}
payload = [
{'key':'summary','value': main_text},
{'key':'temperature','value': temperature},
{'key':'humidity','value': humidity},
{'key':'uvIndex','value': uv_index},
{'key':'icon','value': icon}
]
response = requests.post(url=url, headers=headers, data=json.dumps(payload))
response.close()
def update_time(self):
"""Fetch the time.localtime(), parse it out and update the display text"""
now = time.localtime(time.time())
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)
print(time_str)
self.time_text.text = 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._icon_group:
self._icon_group.pop()
if not 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)
board.DISPLAY.refresh_soon()
board.DISPLAY.wait_for_frame()