Skip to content

Commit 0d6b352

Browse files
authored
Merge pull request #2358 from adafruit/weather_updates
Refactoring code dot py
2 parents 245a7dc + d5a9e9a commit 0d6b352

File tree

1 file changed

+68
-65
lines changed
  • NeoPixel_Sprite_Weather_Display

1 file changed

+68
-65
lines changed

NeoPixel_Sprite_Weather_Display/code.py

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from adafruit_bitmap_font import bitmap_font
1515
from displayio import Bitmap
1616
from rainbowio import colorwheel
17-
17+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
1818
import adafruit_requests
1919
from weather_codes import weather_codes
2020

@@ -48,26 +48,29 @@
4848
pool = socketpool.SocketPool(wifi.radio)
4949
requests = adafruit_requests.Session(pool, ssl.create_default_context())
5050

51-
# make the API request
52-
response = requests.get(weather_url)
53-
# packs the response into a JSON
54-
response_as_json = response.json()
55-
print()
56-
# prints the entire JSON
57-
print(response_as_json)
58-
print()
59-
# gets current weather code
60-
weather = int(response_as_json['current_weather']['weathercode'])
61-
print(weather)
62-
# gets temperature
63-
temp = response_as_json['current_weather']['temperature']
64-
temp_int = int(temp)
65-
print("%s°%s" % (temp, temp_unit))
66-
# gets time
67-
json_time = response_as_json['current_weather']['time']
68-
new_time = json_time.rsplit("T", 1)[-1]
69-
new_time = int(new_time[:2])
70-
print(new_time)
51+
def get_the_weather():
52+
# make the API request
53+
response = requests.get(weather_url)
54+
# packs the response into a JSON
55+
response_as_json = response.json()
56+
print()
57+
# prints the entire JSON
58+
print(response_as_json)
59+
print()
60+
# gets current weather code
61+
w = int(response_as_json['current_weather']['weathercode'])
62+
# gets temperature
63+
t = response_as_json['current_weather']['temperature']
64+
temp_int = int(t)
65+
t_c = simpleio.map_range(temp_int, min_temp, max_temp, 255, 0)
66+
# gets time
67+
json_time = response_as_json['current_weather']['time']
68+
n_t = json_time.rsplit("T", 1)[-1]
69+
n_t = int(n_t[:2])
70+
return w, t, t_c, n_t
71+
72+
# initial API call
73+
weather, temp, temp_color, new_time = get_the_weather()
7174

7275
# font edit code by Jeff Epler
7376
tom_thumb = bitmap_font.load_font("tom-thumb.pcf", Bitmap)
@@ -102,10 +105,11 @@ def get_bounding_box(self):
102105
105: {'dx': 0, 'shift_x': 2},
103106
33: {'dx': 0, 'shift_x': 2},
104107
})
108+
# thank you Jeff for this PatchedFont() function!
105109

106110
# temperature for scrolling text
107111
label = Label(text=" %s°%s " % (temp, temp_unit), font=font)
108-
bitmap = label.bitmap
112+
text = label.bitmap
109113

110114
# create 5x5 neopixels
111115
pixels = neopixel.NeoPixel(board.A3, 5*5, brightness=.08, auto_write=False)
@@ -114,70 +118,69 @@ def get_bounding_box(self):
114118
# arrays to pack assets from weather_codes helper
115119
# weather condition code
116120
codes = []
117-
# bitmap for daytime
121+
# bitmaps for daytime
118122
day_images = []
119-
# bitmap for nighttime
123+
# bitmaps for nighttime
120124
night_images = []
121125

122126
for i in weather_codes:
123127
codes.append(i['code'])
124128
day_images.append(i['day_img'])
125129
night_images.append(i['night_img'])
126130

127-
clock = time.monotonic()
128-
129-
# display current weather sprite & scroll temperature
130-
while True:
131-
# check every hour
132-
if (time.monotonic() - clock) > 3600:
133-
# make the API request
134-
response = requests.get(weather_url)
135-
# packs the response into a JSON
136-
response_as_json = response.json()
137-
print()
138-
# prints the entire JSON
139-
print(response_as_json)
140-
print()
141-
# current weather code
142-
weather = int(response_as_json['current_weather']['weathercode'])
143-
print(weather)
144-
# temperature
145-
temp = response_as_json['current_weather']['temperature']
146-
temp_int = int(temp)
147-
print("%s°%s" % (temp, temp_unit))
148-
# update scrolling text
149-
label.text = " %s°%s " % (temp, temp_unit)
150-
json_time = response_as_json['current_weather']['time']
151-
new_time = json_time.rsplit("T", 1)[-1]
152-
new_time = int(new_time[:2])
153-
print(new_time)
154-
clock = time.monotonic()
155-
# map color to temp range. colder temps are cool colors, warmer temps warm colors
156-
temp_color = simpleio.map_range(temp_int, min_temp, max_temp, 255, 0)
157-
# checks if it's day or night based on hour
158-
if new_time in range(daytime_min, daytime_max):
159-
img = day_images[weather]
131+
# checks if it's day or night based on hour
132+
def day_or_night(t):
133+
if t in range(daytime_min, daytime_max):
134+
z = day_images[weather]
160135
else:
161-
img = night_images[weather]
162-
# draw bitmap sprite
136+
z = night_images[weather]
137+
return z
138+
139+
# initial sprite selection
140+
img = day_or_night(new_time)
141+
142+
# draw bitmap sprite
143+
def draw_sprite(c):
163144
for pixel in img:
164-
pixels[count] = pixel
145+
pixels[c] = pixel
165146
pixels.show()
166-
count+=1
147+
c += 1
167148
time.sleep(0.001)
149+
c = 0
150+
151+
# ticks time tracker
152+
clock = ticks_ms()
153+
154+
# 15 minutes in milliseconds
155+
weather_check = 900000
156+
157+
# display current weather sprite & scroll temperature
158+
while True:
159+
# checks the time
160+
if ticks_diff(ticks_ms(), clock) > weather_check:
161+
print("pinging Open-Meteo")
162+
# make the API request with function
163+
# return weather ID, temp, temp color & hour
164+
weather, temp, temp_color, new_time = get_the_weather()
165+
# checks if it's day or night based on hour
166+
# & returns day or night version of sprite
167+
img = day_or_night(new_time)
168+
# reset clock
169+
clock = ticks_add(clock, weather_check)
170+
# draw bitmap sprite
171+
draw_sprite(count)
172+
# blocking delay to hold the sprite on the display
168173
time.sleep(5)
169-
hue = 0
170-
count = 0
171174
# draw scrolling text
172175
for v in range(2):
173-
for i in range(bitmap.width):
176+
for i in range(text.width):
174177
# Scoot the old text left by 1 pixel
175178
pixels[:20] = pixels[5:]
176179
# adjust color based on temperature
177180
color = colorwheel(temp_color)
178181
# Draw in the next line of text
179182
for y in range(5):
180183
# Select black or color depending on the bitmap pixel
181-
pixels[20+y] = color * bitmap[i,y]
184+
pixels[20+y] = color * text[i,y]
182185
pixels.show()
183186
time.sleep(.1)

0 commit comments

Comments
 (0)