mirror of
https://git.wetfence.xyz/RandyTheSilly/Trigalarm.git
synced 2026-09-05 10:07:26 -04:00
31 lines
1.0 KiB
C
31 lines
1.0 KiB
C
// lol the code in this file is mostly stolen from https://github.com/SeaPea/GentleWake
|
|
|
|
#include <pebble.h>
|
|
|
|
// A slightly more robust wakeup scheduler that retries on certain errors and
|
|
// can retry for a different wakeup time offset by retry_diff up to retry_max times
|
|
WakeupId wakeup_schedule_simple_robust(time_t wakeup_time, int8_t retry_diff, bool is_fallback) {
|
|
WakeupId result = 0;
|
|
uint8_t try_count = 0;
|
|
|
|
result = wakeup_schedule(wakeup_time, is_fallback, true);
|
|
|
|
if (result < 0) {
|
|
// If result is negative, something went wrong, so make sure all wakeups for this app are cancelled and try again
|
|
wakeup_cancel_all();
|
|
|
|
result = wakeup_schedule(wakeup_time, is_fallback, true);
|
|
|
|
if (result == E_RANGE) {
|
|
// E_RANGE means some other app has the same wakeup time +/- 1 minute, so try to set
|
|
// the wakeup for a time up to (retry_diff * 5) minutes before/after.
|
|
while (result == E_RANGE && try_count++ < 5) {
|
|
wakeup_time += retry_diff;
|
|
result = wakeup_schedule(wakeup_time, is_fallback, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|