Update code.py

This commit is contained in:
Liz 2023-03-07 07:47:44 -05:00
parent d9b38346a4
commit c9b9328008

View file

@ -41,6 +41,12 @@ location = "America/New York"
# io HTTP for getting the time from the internet
io = IO_HTTP(aio_username, aio_key, requests)
def reset_on_error(delay, error):
print("Error:\n", str(error))
print("Resetting microcontroller in %d seconds" % delay)
time.sleep(delay)
microcontroller.reset()
# function for making http requests with try/except
def get_request(tries, ping):
for i in range(tries):
@ -56,7 +62,11 @@ def get_request(tries, ping):
return n
# get the time on start-up
now = get_request(5, io.receive_time())
# pylint: disable=broad-except
try:
now = get_request(5, io.receive_time())
except Exception as e:
reset_on_error(10, e)
print(now)
today = now.tm_mday
@ -73,7 +83,13 @@ def sun_clock():
return _rise, _set
# initial API call
sunrise, sunset = sun_clock()
try:
sunrise, sunset = sun_clock()
except Exception as e:
reset_on_error(10, e)
print(sunrise)
print(sunset)
# the sunrise/sunset time is returned as a JSON aka a string
# this function chops up the string to get the hours and minutes as integers
@ -95,6 +111,10 @@ set_time = divide_time(sunset)
def sun_countdown(sun_event):
n = get_request(5, io.receive_time())
remaining = time.mktime(sun_event) - time.mktime(n)
<<<<<<< Updated upstream
=======
r = remaining
>>>>>>> Stashed changes
# print(remaining)
# calculate the seconds remaining
secs_remaining = remaining % 60 # pylint: disable=unused-variable
@ -105,10 +125,22 @@ def sun_countdown(sun_event):
# calculate the hours remaining
hours_until = remaining % 24
remaining //= 24
<<<<<<< Updated upstream
return remaining, hours_until, minutes_until, n
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
=======
return r, hours_until, minutes_until, n
try:
total_until_rise, hours_until_sunrise, mins_until_sunrise, now = sun_countdown(rise_time)
except Exception as e:
reset_on_error(10, e)
try:
total_until_set, hours_until_sunset, mins_until_sunset, now = sun_countdown(set_time)
except Exception as e:
reset_on_error(10, e)
>>>>>>> Stashed changes
# red and yellow color percentage for neopixels
percent_red = 0
@ -121,11 +153,27 @@ PIN = board.A3 # This is the default pin on the NeoPixel Driver BFF.
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
print(total_until_set)
# check to see if the star fragment should be lit up on start-up
if total_until_set < 0:
<<<<<<< Updated upstream
=======
print("star glow true")
>>>>>>> Stashed changes
star_glow = True
percent_red = 255
percent_yellow = 125
# turn neopixels on using RGB values
pixels.fill((percent_red, percent_yellow, 0))
pixels.show()
else:
print("star glow false")
star_glow = False
percent_red = 0
percent_yellow = 0
# turn neopixels on using RGB values
pixels.fill((percent_red, percent_yellow, 0))
pixels.show()
# ticks time tracker
clock = ticks_ms()
@ -148,19 +196,33 @@ while True:
sunrise, sunset = sun_clock()
(total_until_set, hours_until_sunset,
mins_until_sunset, now) = sun_countdown(set_time)
<<<<<<< Updated upstream
=======
print(now)
>>>>>>> Stashed changes
print("%d hour(s) until sunset" % hours_until_sunset)
print("%d minutes(s) until sunset" % mins_until_sunset)
print(sunset)
print()
# less than an hour until sunset...
<<<<<<< Updated upstream
if hours_until_sunset == 0 or hours_until_sunset == 23:
# check every minute
time_check = 60000
=======
if hours_until_sunset in (0, 23):
# check every minute
time_check = 300000
>>>>>>> Stashed changes
# map color to ramp up in brightness over the course of the final hour
percent_red = simpleio.map_range(mins_until_sunset, 59, 0, 0, 255)
percent_yellow = simpleio.map_range(mins_until_sunset, 59, 0, 0, 125)
# if the sun has set..
<<<<<<< Updated upstream
if mins_until_sunset < 1:
=======
if total_until_set < 0:
>>>>>>> Stashed changes
percent_red = 255
percent_yellow = 125
time_check = 900000
@ -188,6 +250,7 @@ while True:
sunrise, sunset = sun_clock()
(total_until_rise, hours_until_sunrise,
mins_until_sunrise, now) = sun_countdown(rise_time)
<<<<<<< Updated upstream
print("%d hour(s) until sunrise" % hours_until_sunrise)
print("%d minutes(s) until sunrise" % mins_until_sunrise)
print(sunrise)
@ -197,11 +260,26 @@ while True:
if hours_until_sunrise == 0 or hours_until_sunrise == 23:
# check every minute
time_check = 60000
=======
print(now)
print("%d hour(s) until sunrise" % hours_until_sunrise)
print("%d minutes(s) until sunrise" % mins_until_sunrise)
print(sunrise)
print()
# less than an hour until sunset...
if hours_until_sunrise in (0, 23):
# check every minute
time_check = 300000
>>>>>>> Stashed changes
# map color to decrease brightness over the course of the final hour
percent_red = simpleio.map_range(mins_until_sunrise, 59, 0, 255, 0)
percent_yellow = simpleio.map_range(mins_until_sunrise, 59, 0, 125, 0)
# if the sun has risen..
<<<<<<< Updated upstream
if mins_until_sunrise < 1:
=======
if total_until_rise < 0:
>>>>>>> Stashed changes
percent_red = 0
percent_yellow = 0
time_check = 900000
@ -232,9 +310,14 @@ while True:
# turn neopixels on using RGB values
pixels.fill((percent_red, percent_yellow, 0))
pixels.show()
<<<<<<< Updated upstream
# pylint: disable=broad-except
except Exception as e:
print("Error:\n", str(e))
print("Resetting microcontroller in 10 seconds")
time.sleep(10)
microcontroller.reset()
=======
except Exception as e:
reset_on_error(10, e)
>>>>>>> Stashed changes