Update code.py
sleep till after midnight,
also - catch communication error and restart app if error.
@@ -4,8 +4,16 @@ from adafruit_magtag.magtag import MagTag
from adafruit_display_shapes.circle import Circle
# create MagTag and connect to network
+try:
magtag = MagTag()
magtag.network.connect()
+except (ConnectionError, ValueError, RuntimeError) as e:
+ print("*** MagTag(), Some error occured, retrying! -", e)
+ # Exit program and restart in 1 seconds.
+ pause = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 1)
+ alarm.exit_and_deep_sleep_until_alarms(pause)
+
+
# displayio groups
group = displayio.Group(max_size=30)
@@ -70,7 +78,10 @@ magtag.get_local_time()
now = time.localtime()
month = now[1]
day = now[2]
-print("day is", day)
+hour = now[3]
+minutes = now[4]
+seconds_since_midnight = 60 * (hour*60 + minutes)
+print("day is", day, "(", seconds_since_midnight, "seconds since midnight")
# sets colors of circles to transparent to reveal dates that have passed & current date
for i in range(day):
@@ -82,9 +93,11 @@ magtag.display.show(group)
magtag.display.refresh()
time.sleep(5)
-# goes into deep sleep for 12 hours
+# goes into deep sleep till a 'stroke' past midnight
print("entering deep sleep")
-magtag.exit_and_deep_sleep(43200)
+seconds_to_sleep = 24*60*60 - seconds_since_midnight + 10
+print("sleeping for ",seconds_to_sleep,"seconds")
+magtag.exit_and_deep_sleep(seconds_to_sleep)
# entire code will run again after deep sleep cycle
# similar to hitting the reset button
