diff --git a/src/c/main.c b/src/c/main.c index ddef882..166f666 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -1,3 +1,4 @@ +#include "gentlewake.h" #include "triangles.h" #include #include @@ -88,7 +89,7 @@ static char *s_options_under[2]; ///////////////////////////////// static void glance_reload_callback(AppGlanceReloadSession *session, size_t limit, void *target) { - static char s_glance_buffer[11]; + static char s_glance_buffer[13]; snprintf(s_glance_buffer, sizeof(s_glance_buffer), "Up @ %02u:%02u", s_the_time.HourSet, s_the_time.MinuteSet); AppGlanceSlice slice = { .expiration_time = (time_t)(uintptr_t)target, @@ -98,29 +99,33 @@ static void glance_reload_callback(AppGlanceReloadSession *session, size_t limit app_glance_add_slice(session, slice); } -// returns (passes through) the negative value from wakeup_schedule -// when an error is encountered -static int8_t schedule_main_alarm() { - time_t now = time(NULL); - struct tm now_tm = *localtime(&now); - struct tm target_tm = now_tm; - target_tm.tm_hour = s_the_time.HourWIP; - target_tm.tm_min = s_the_time.MinuteWIP; - target_tm.tm_sec = 0; - time_t target = mktime(&target_tm); - if (target <= now) { - target += SECONDS_PER_DAY; - } +static bool schedule_alarm(bool is_fallback) { + time_t target; + WakeupId status; wakeup_cancel_all(); - WakeupId status = wakeup_schedule(target, 0, true); + + if (is_fallback) { + target = time(NULL) + 15; + status = wakeup_schedule_simple_robust(target, 15, true); + } else { + time_t now = time(NULL); + struct tm target_tm = *localtime(&now); + target_tm.tm_hour = s_the_time.HourWIP; + target_tm.tm_min = s_the_time.MinuteWIP; + target_tm.tm_sec = 0; + target = mktime(&target_tm); + if (target <= now) { + target += SECONDS_PER_DAY; + } + status = wakeup_schedule_simple_robust(target, SECONDS_PER_MINUTE, is_fallback); + } if (status < 0) { - APP_LOG(APP_LOG_LEVEL_DEBUG, "SCHEDULING ERROR: %d", status); - return status; + return false; // TODO show failure window - user must acknowledge this window to close it } s_the_time.HourSet = s_the_time.HourWIP; s_the_time.MinuteSet = s_the_time.MinuteWIP; app_glance_reload(glance_reload_callback, (void *)(uintptr_t)target); - return 0; + return true; } static void setter_seconds_handler(struct tm *tick_time, TimeUnits units_changed) { @@ -145,7 +150,7 @@ static void setter_seconds_handler(struct tm *tick_time, TimeUnits units_changed uint8_t minutes = (diff % 3600) / 60; uint8_t seconds = diff % 60; - static char s_countdown_buffer[9]; + static char s_countdown_buffer[12]; snprintf(s_countdown_buffer, sizeof(s_countdown_buffer), "%02u:%02u:%02u", hours, minutes, seconds); text_layer_set_text(s_counter_txt_layer, s_countdown_buffer); } @@ -156,7 +161,7 @@ static void update_countdown_display() { } static void update_setter_display() { - static char s_setter_buffer[PBL_IF_ROUND_ELSE(8, 12)]; + static char s_setter_buffer[PBL_IF_ROUND_ELSE(10, 14)]; snprintf(s_setter_buffer, sizeof(s_setter_buffer), PBL_IF_ROUND_ELSE("%02u : %02u", "%02u : %02u"), s_the_time.HourWIP, s_the_time.MinuteWIP); text_layer_set_text(s_setter_txt_layer, s_setter_buffer); } @@ -207,8 +212,8 @@ static void setter_down_click_handler(void *recognizer, void *ctx) { static void setter_select_click_handler(void *recognizer, void *ctx) { if (s_highlight_pos == 2) { - int8_t status = schedule_main_alarm(); - if (status < 0) { + bool success = schedule_alarm(false); + if (!success) { return; // early return to avoid indicating alarm was set when it was not } @@ -344,23 +349,10 @@ static void setter_window_unload() { fonts_unload_custom_font(s_font_lecoish_big); } -static void schedule_fallback_alarm() { - wakeup_cancel_all(); - time_t target_time = time(NULL); - WakeupId status; - while (true) { - target_time += 15; - status = wakeup_schedule(target_time, 0, true); - if (status >= 0) { - break; - } - } -} - static void alarm_seconds_handler(struct tm *tick_time, TimeUnits units_changed) { // re-schedule fallback alarm every 15 seconds if (tick_time->tm_sec % 15 == 0) { - schedule_fallback_alarm(); + schedule_alarm(true); // output is not captured, as it is not actionable } // update stopwatch @@ -562,7 +554,7 @@ static void alarm_window_load() { text_layer_set_font(s_side_c_txt_layer, s_font_mansalva_small); layer_add_child(window_layer, text_layer_get_layer(s_side_c_txt_layer)); //// display 2 side lengths to the user - static char side_a_buffer[8], side_b_buffer[8], side_c_buffer[8], angle_A_buffer[8], angle_B_buffer[8], angle_C_buffer[8]; + static char side_a_buffer[8], side_b_buffer[8], side_c_buffer[8]; uint8_t ignored_side = rand() % 3; uint8_t sin_index = rand() % 2; uint8_t cos_index = rand() % 2; @@ -684,12 +676,13 @@ static void alarm_window_load() { text_layer_set_text(s_counter_txt_layer, "00:00"); layer_add_child(window_layer, text_layer_get_layer(s_counter_txt_layer)); - schedule_fallback_alarm(); + schedule_alarm(true); // output is not captured, as it is not actionable tick_timer_service_subscribe(SECOND_UNIT, alarm_seconds_handler); } static void alarm_window_unload() { text_layer_destroy(s_counter_txt_layer); + window_destroy(s_drop_window); text_layer_destroy(s_side_a_txt_layer); text_layer_destroy(s_side_b_txt_layer); text_layer_destroy(s_side_c_txt_layer); @@ -756,19 +749,11 @@ static void alarm_select_hold_handler(void *recognizer, void *ctx) { } wakeup_cancel_all(); - int8_t status = schedule_main_alarm(); - while (status < 0) { - s_the_time.MinuteWIP++; - if (s_the_time.MinuteWIP >= 60) { - s_the_time.MinuteWIP = 0; - s_the_time.HourWIP++; - if (s_the_time.HourWIP >= 24) { - s_the_time.HourWIP = 0; - } - } - status = schedule_main_alarm(); + bool success = schedule_alarm(false); + if (success) { + // TODO confirmation window + window_stack_pop_all(true); } - window_stack_pop_all(true); // TODO pop-up telling user the answer & their previous 5 times } static void alarm_click_config_provider(void *ctx) {