forked from raphv/galactic-weather-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherclock.py
More file actions
239 lines (198 loc) · 6.54 KB
/
weatherclock.py
File metadata and controls
239 lines (198 loc) · 6.54 KB
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
import time
import ntptime
import urequests
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN
from connect import connect, isconnected
import weatherclock_assets
import secrets
graphics = PicoGraphics(display=DISPLAY_GALACTIC_UNICORN)
gu = GalacticUnicorn()
# Initialize state variables
brightness = 0.2
sleep_mode = False
WIDTH = gu.WIDTH
HEIGHT = gu.HEIGHT
current_tz = 0 # UTC by default, can be updated via Open Meteo
REFRESH_NTP = 3600
REFRESH_WEATHER = 1200
WEATHER_URL = "http://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s¤t=temperature_2m,apparent_temperature,weather_code&timezone=%s&forecast_days=1"%(
secrets.LATITUDE,
secrets.LONGITUDE,
secrets.TZ_DEF.replace('/', '%2F')
)
PENS = [ graphics.create_pen(*color) for color in weatherclock_assets.BASE_COLORS ]
def update_time():
global last_ntp_update
print('Updating time')
try:
if not isconnected():
print('Reconnecting')
connect()
ntptime.settime()
last_ntp_update = time.time()
except (RuntimeError, OSError) as e:
print('Error fetching time', e)
def update_weather():
global last_weather_update, current_tz, forecasts
print('Updating weather')
try:
print(WEATHER_URL)
r = urequests.get(
WEATHER_URL)
j = r.json()
print(j)
forecasts = [
(int(j["current"]["time"][-5:-3]),
j["current"]["temperature_2m"],
j["current"]["weather_code"],
j["current"]["apparent_temperature"]
)
]
print(forecasts)
current_tz = j["utc_offset_seconds"]
last_weather_update = time.time()
except (Exception) as e:
print('Error getting weather', e)
def get_weather_type(weather_code):
for weather in weatherclock_assets.WEATHER_TYPES:
if weather_code in weather[0]:
return weather
return None
@micropython.native
def draw_weather(weather_code, frame_parity, offset_x=0, offset_y=0):
weather_tuple = get_weather_type(weather_code)
if weather_tuple is None:
return
pixels = weather_tuple[1+frame_parity]
for y in range(11):
ypos = offset_y + y
if (ypos >= 0) or (ypos < HEIGHT):
line = pixels[y]
for x in range(15):
xpos = offset_x + x
if (xpos >= 0 or xpos < WIDTH):
pixelvalue = (line >> 4*x) & 0xf
if pixelvalue:
graphics.set_pen(PENS[pixelvalue])
graphics.pixel(xpos, ypos)
@micropython.native
def draw_digit(i, offset_x, offset_y):
digit = weatherclock_assets.DIGITS3x5[i]
for y in range(5):
ypos = offset_y + y
if (ypos >= 0) or (ypos < HEIGHT):
line = (digit >> y*3) & 7
for x in range(3):
xpos = offset_x + x
if line & 1:
graphics.pixel(xpos, ypos)
line = (line >> 1)
def char_to_digit(digit_char):
if type(digit_char) == int:
return digit_char & 0xf
try:
ch = str(digit_char)[0]
if ch == '-':
return 0xa
elif ch == '.':
return 0xb
elif ch == '+':
return 0xe
return int(ch,16)
except (Exception):
return 0xf
def draw_number( num, offset_x, offset_y, from_right=False):
numstr = str(num)
x = offset_x
if from_right:
x = x + 1 - 4*len(numstr)
for digit in numstr:
draw_digit(char_to_digit(digit), x, offset_y)
x += 4
def draw_forecast(forecast, offset_y):
graphics.set_pen(PENS[15])
draw_number('%.0fd'%forecast[1],34,0+offset_y,True)
draw_number('%.0fd'%forecast[3],34,6+offset_y,True)
draw_weather(forecast[2],parity,38,offset_y)
# print(forecast[3])
# print(forecast[1])
def handle_switch_actions():
"""Handle Galactic Unicorn switches for specific actions."""
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
print("SWITCH_A pressed: Updating time")
update_time()
if gu.is_pressed(GalacticUnicorn.SWITCH_B):
print("SWITCH_B pressed: Updating weather")
update_weather()
# Brightness control and sleep mode functions
def handle_brightness_change():
global brightness
if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
brightness = max(0.1, brightness - 0.1)
gu.set_brightness(brightness)
elif gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
brightness = min(1.0, brightness + 0.1)
gu.set_brightness(brightness)
def handle_sleep_mode():
global sleep_mode
if gu.is_pressed(GalacticUnicorn.SWITCH_SLEEP):
sleep_mode = not sleep_mode
if sleep_mode:
graphics.set_pen(PENS[0])
graphics.clear()
gu.update(graphics)
graphics.set_pen(PENS[0])
graphics.clear()
graphics.set_pen(PENS[15])
graphics.set_font('display8')
graphics.text("Hello!", 0, 0, scale=.5)
gu.set_brightness(.2)
gu.update(graphics)
connect()
forecasts = None
last_ntp_update = 0
last_weather_update = 0
last_hour = 0
last_second = 0
scrolling_pos = 0
displayed_forecast_index = 0
year, month, day, hour, minute, second, weekday = (0,0,0,0,0,0,0)
is_scrolling = False
cycles = 0
while True:
handle_brightness_change()
handle_sleep_mode()
handle_switch_actions() # Check for switch presses
if sleep_mode:
time.sleep(0.1)
continue
now = time.time()
msecs = time.ticks_ms()
parity = (msecs // 500) & 1
if now != last_second:
if (now - last_ntp_update) > REFRESH_NTP:
print('Time to update NTP Time')
update_time()
if (now - last_weather_update) > REFRESH_WEATHER:
print('Time to update weather')
update_weather()
local_now = now + current_tz
year, month, day, hour, minute, second, weekday, _ = time.localtime(local_now)
graphics.set_pen(PENS[0])
graphics.clear()
graphics.set_pen(PENS[1]) # Change the color of the clock
draw_number(f"{hour:02}", 0, 0)
draw_number(f"{minute:02}", 10, 0)
graphics.set_pen(PENS[2]) # Change the color of the date
draw_number(f"{day:02}", 0, 6)
draw_number(f"{month:02}", 10, 6)
graphics.set_pen(PENS[15]) # Change the color of the time dots
if parity:
graphics.pixel(8, 1)
graphics.pixel(8, 3)
if forecasts is not None:
draw_forecast(forecasts[displayed_forecast_index], -scrolling_pos)
time.sleep(0.1)
gu.update(graphics)
cycles += 1