14
14
from adafruit_bitmap_font import bitmap_font
15
15
from displayio import Bitmap
16
16
from rainbowio import colorwheel
17
-
17
+ from adafruit_ticks import ticks_ms , ticks_add , ticks_diff
18
18
import adafruit_requests
19
19
from weather_codes import weather_codes
20
20
48
48
pool = socketpool .SocketPool (wifi .radio )
49
49
requests = adafruit_requests .Session (pool , ssl .create_default_context ())
50
50
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 ()
71
74
72
75
# font edit code by Jeff Epler
73
76
tom_thumb = bitmap_font .load_font ("tom-thumb.pcf" , Bitmap )
@@ -102,10 +105,11 @@ def get_bounding_box(self):
102
105
105 : {'dx' : 0 , 'shift_x' : 2 },
103
106
33 : {'dx' : 0 , 'shift_x' : 2 },
104
107
})
108
+ # thank you Jeff for this PatchedFont() function!
105
109
106
110
# temperature for scrolling text
107
111
label = Label (text = " %s°%s " % (temp , temp_unit ), font = font )
108
- bitmap = label .bitmap
112
+ text = label .bitmap
109
113
110
114
# create 5x5 neopixels
111
115
pixels = neopixel .NeoPixel (board .A3 , 5 * 5 , brightness = .08 , auto_write = False )
@@ -114,70 +118,69 @@ def get_bounding_box(self):
114
118
# arrays to pack assets from weather_codes helper
115
119
# weather condition code
116
120
codes = []
117
- # bitmap for daytime
121
+ # bitmaps for daytime
118
122
day_images = []
119
- # bitmap for nighttime
123
+ # bitmaps for nighttime
120
124
night_images = []
121
125
122
126
for i in weather_codes :
123
127
codes .append (i ['code' ])
124
128
day_images .append (i ['day_img' ])
125
129
night_images .append (i ['night_img' ])
126
130
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 ]
160
135
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 ):
163
144
for pixel in img :
164
- pixels [count ] = pixel
145
+ pixels [c ] = pixel
165
146
pixels .show ()
166
- count += 1
147
+ c += 1
167
148
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
168
173
time .sleep (5 )
169
- hue = 0
170
- count = 0
171
174
# draw scrolling text
172
175
for v in range (2 ):
173
- for i in range (bitmap .width ):
176
+ for i in range (text .width ):
174
177
# Scoot the old text left by 1 pixel
175
178
pixels [:20 ] = pixels [5 :]
176
179
# adjust color based on temperature
177
180
color = colorwheel (temp_color )
178
181
# Draw in the next line of text
179
182
for y in range (5 ):
180
183
# 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 ]
182
185
pixels .show ()
183
186
time .sleep (.1 )
0 commit comments