Skip to content

Commit a59aa35

Browse files
authored
Improved inky's sleep_for, now handles up to 28 days (#754)
1 parent b6499e7 commit a59aa35

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

micropython/modules_py/inky_frame.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,21 @@ def sleep_for(minutes):
8787
if second >= 55:
8888
minute += 1
8989

90-
minute += minutes
90+
# Can't sleep beyond a month, so clamp the sleep to a 28 day maximum
91+
minutes = min(minutes, 40320)
9192

92-
while minute >= 60:
93-
minute -= 60
94-
hour += 1
93+
# Calculate the future alarm date; first, turn the current time into seconds since epoch
94+
sec_since_epoch = time.mktime((year, month, day, hour, minute, second, dow, 0))
9595

96-
if hour >= 24:
97-
hour -= 24
96+
# Add the required minutes to this
97+
sec_since_epoch += minutes * 60
9898

99+
# And convert it back into a more useful tuple
100+
(ayear, amonth, aday, ahour, aminute, asecond, adow, adoy) = time.localtime(sec_since_epoch)
101+
102+
# And now set the alarm as before, now including the day
99103
rtc.clear_alarm_flag()
100-
rtc.set_alarm(0, minute, hour)
104+
rtc.set_alarm(0, aminute, ahour, aday)
101105
rtc.enable_alarm_interrupt(True)
102106

103107
turn_off()

0 commit comments

Comments
 (0)