#include "scheduler.h" #include struct TheTime the_time; static void glance_reload_callback(AppGlanceReloadSession *session, size_t limit, void *target) { static char s_glance_buffer[22]; time_t alarm_time = (time_t)(uintptr_t)target; struct tm wakeup_tm = *localtime(&alarm_time); snprintf(s_glance_buffer, sizeof(s_glance_buffer), "%02u:%02u:%02u, %02u/%02u", wakeup_tm.tm_hour, wakeup_tm.tm_min, wakeup_tm.tm_sec, wakeup_tm.tm_mon + 1, wakeup_tm.tm_mday); AppGlanceSlice slice = { .expiration_time = (time_t)(uintptr_t)target, .layout.icon = APP_GLANCE_SLICE_DEFAULT_ICON, .layout.subtitle_template_string = s_glance_buffer, }; app_glance_add_slice(session, slice); } // Mostly stolen from https://github.com/SeaPea/GentleWake; // 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, WakeupReason wakeup_reason) { WakeupId result = 0; uint8_t try_count = 0; result = wakeup_schedule(wakeup_time, wakeup_reason, 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, wakeup_reason, 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 * 8) minutes before/after. while (result == E_RANGE && try_count++ < 8) { wakeup_time += retry_diff; result = wakeup_schedule(wakeup_time, wakeup_reason, true); } } } if (result >= 0) { the_time.HourSet = the_time.HourWIP; the_time.MinuteSet = the_time.MinuteWIP; app_glance_reload(glance_reload_callback, (void *)(uintptr_t)wakeup_time); } return result; }