Skip to content

Commit

Permalink
Merge pull request #16 from ahnlak/longer_sleep_for
Browse files Browse the repository at this point in the history
Improved sleep_for, allows alarms up to 28 days long
  • Loading branch information
Gadgetoid authored May 24, 2023
2 parents ab24e85 + bc1a666 commit 5d3b139
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions firmware/PIMORONI_BADGER2040W/lib/badger2040.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,21 @@ def sleep_for(minutes):
if second >= 55:
minute += 1

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

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

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

# And convert it back into a more useful tuple
(ayear, amonth, aday, ahour, aminute, asecond, adow, adoy) = time.localtime(sec_since_epoch)

# And now set the alarm as before, now including the day
rtc.clear_alarm_flag()
rtc.set_alarm(0, minute, hour)
rtc.set_alarm(0, aminute, ahour, aday)
rtc.enable_alarm_interrupt(True)

turn_off()
Expand Down

0 comments on commit 5d3b139

Please sign in to comment.