Skip to content

Commit c9b9328

Browse files
committed
Update code.py
1 parent d9b3834 commit c9b9328

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

Star_Fragment_Lamp/code.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
# io HTTP for getting the time from the internet
4242
io = IO_HTTP(aio_username, aio_key, requests)
4343

44+
def reset_on_error(delay, error):
45+
print("Error:\n", str(error))
46+
print("Resetting microcontroller in %d seconds" % delay)
47+
time.sleep(delay)
48+
microcontroller.reset()
49+
4450
# function for making http requests with try/except
4551
def get_request(tries, ping):
4652
for i in range(tries):
@@ -56,7 +62,11 @@ def get_request(tries, ping):
5662
return n
5763

5864
# get the time on start-up
59-
now = get_request(5, io.receive_time())
65+
# pylint: disable=broad-except
66+
try:
67+
now = get_request(5, io.receive_time())
68+
except Exception as e:
69+
reset_on_error(10, e)
6070
print(now)
6171
today = now.tm_mday
6272

@@ -73,7 +83,13 @@ def sun_clock():
7383
return _rise, _set
7484

7585
# initial API call
76-
sunrise, sunset = sun_clock()
86+
try:
87+
sunrise, sunset = sun_clock()
88+
except Exception as e:
89+
reset_on_error(10, e)
90+
91+
print(sunrise)
92+
print(sunset)
7793

7894
# the sunrise/sunset time is returned as a JSON aka a string
7995
# this function chops up the string to get the hours and minutes as integers
@@ -95,6 +111,10 @@ def divide_time(z):
95111
def sun_countdown(sun_event):
96112
n = get_request(5, io.receive_time())
97113
remaining = time.mktime(sun_event) - time.mktime(n)
114+
<<<<<<< Updated upstream
115+
=======
116+
r = remaining
117+
>>>>>>> Stashed changes
98118
# print(remaining)
99119
# calculate the seconds remaining
100120
secs_remaining = remaining % 60 # pylint: disable=unused-variable
@@ -105,10 +125,22 @@ def sun_countdown(sun_event):
105125
# calculate the hours remaining
106126
hours_until = remaining % 24
107127
remaining //= 24
128+
<<<<<<< Updated upstream
108129
return remaining, hours_until, minutes_until, n
109130

110131
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
111132
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
133+
=======
134+
return r, hours_until, minutes_until, n
135+
try:
136+
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
137+
except Exception as e:
138+
reset_on_error(10, e)
139+
try:
140+
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
141+
except Exception as e:
142+
reset_on_error(10, e)
143+
>>>>>>> Stashed changes
112144

113145
# red and yellow color percentage for neopixels
114146
percent_red = 0
@@ -121,11 +153,27 @@ def sun_countdown(sun_event):
121153

122154
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
123155

156+
print(total_until_set)
124157
# check to see if the star fragment should be lit up on start-up
125158
if total_until_set < 0:
159+
<<<<<<< Updated upstream
160+
=======
161+
print("star glow true")
162+
>>>>>>> Stashed changes
126163
star_glow = True
164+
percent_red = 255
165+
percent_yellow = 125
166+
# turn neopixels on using RGB values
167+
pixels.fill((percent_red, percent_yellow, 0))
168+
pixels.show()
127169
else:
170+
print("star glow false")
128171
star_glow = False
172+
percent_red = 0
173+
percent_yellow = 0
174+
# turn neopixels on using RGB values
175+
pixels.fill((percent_red, percent_yellow, 0))
176+
pixels.show()
129177

130178
# ticks time tracker
131179
clock = ticks_ms()
@@ -148,19 +196,33 @@ def sun_countdown(sun_event):
148196
sunrise, sunset = sun_clock()
149197
(total_until_set, hours_until_sunset,
150198
mins_until_sunset, now) = sun_countdown(set_time)
199+
<<<<<<< Updated upstream
200+
=======
201+
print(now)
202+
>>>>>>> Stashed changes
151203
print("%d hour(s) until sunset" % hours_until_sunset)
152204
print("%d minutes(s) until sunset" % mins_until_sunset)
153205
print(sunset)
154206
print()
155207
# less than an hour until sunset...
208+
<<<<<<< Updated upstream
156209
if hours_until_sunset == 0 or hours_until_sunset == 23:
157210
# check every minute
158211
time_check = 60000
212+
=======
213+
if hours_until_sunset in (0, 23):
214+
# check every minute
215+
time_check = 300000
216+
>>>>>>> Stashed changes
159217
# map color to ramp up in brightness over the course of the final hour
160218
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
161219
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
162220
# if the sun has set..
221+
<<<<<<< Updated upstream
163222
if mins_until_sunset < 1:
223+
=======
224+
if total_until_set < 0:
225+
>>>>>>> Stashed changes
164226
percent_red = 255
165227
percent_yellow = 125
166228
time_check = 900000
@@ -188,6 +250,7 @@ def sun_countdown(sun_event):
188250
sunrise, sunset = sun_clock()
189251
(total_until_rise, hours_until_sunrise,
190252
mins_until_sunrise, now) = sun_countdown(rise_time)
253+
<<<<<<< Updated upstream
191254
print("%d hour(s) until sunrise" % hours_until_sunrise)
192255
print("%d minutes(s) until sunrise" % mins_until_sunrise)
193256
print(sunrise)
@@ -197,11 +260,26 @@ def sun_countdown(sun_event):
197260
if hours_until_sunrise == 0 or hours_until_sunrise == 23:
198261
# check every minute
199262
time_check = 60000
263+
=======
264+
print(now)
265+
print("%d hour(s) until sunrise" % hours_until_sunrise)
266+
print("%d minutes(s) until sunrise" % mins_until_sunrise)
267+
print(sunrise)
268+
print()
269+
# less than an hour until sunset...
270+
if hours_until_sunrise in (0, 23):
271+
# check every minute
272+
time_check = 300000
273+
>>>>>>> Stashed changes
200274
# map color to decrease brightness over the course of the final hour
201275
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
202276
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
203277
# if the sun has risen..
278+
<<<<<<< Updated upstream
204279
if mins_until_sunrise < 1:
280+
=======
281+
if total_until_rise < 0:
282+
>>>>>>> Stashed changes
205283
percent_red = 0
206284
percent_yellow = 0
207285
time_check = 900000
@@ -232,9 +310,14 @@ def sun_countdown(sun_event):
232310
# turn neopixels on using RGB values
233311
pixels.fill((percent_red, percent_yellow, 0))
234312
pixels.show()
313+
<<<<<<< Updated upstream
235314
# pylint: disable=broad-except
236315
except Exception as e:
237316
print("Error:\n", str(e))
238317
print("Resetting microcontroller in 10 seconds")
239318
time.sleep(10)
240319
microcontroller.reset()
320+
=======
321+
except Exception as e:
322+
reset_on_error(10, e)
323+
>>>>>>> Stashed changes

0 commit comments

Comments
 (0)