void handle_timer(AppContextRef ctx, AppTimerHandle handle, uint32_t cookie) { (void)ctx; (void)handle; if (cookie == COOKIE_MY_TIMER) { if(init_anim == ANIM_START) { init_anim = ANIM_HOURS; timer_handle = app_timer_send_event(ctx, 50 /* milliseconds */, COOKIE_MY_TIMER); } else if(init_anim==ANIM_HOURS) { layer_mark_dirty(&hour_display_layer); timer_handle = app_timer_send_event(ctx, 50 /* milliseconds */, COOKIE_MY_TIMER); } else if(init_anim==ANIM_MINUTES) { layer_mark_dirty(&minute_display_layer); timer_handle = app_timer_send_event(ctx, 50 /* milliseconds */, COOKIE_MY_TIMER); } else if(init_anim==ANIM_SECONDS) { layer_mark_dirty(&second_display_layer); timer_handle = app_timer_send_event(ctx, 50 /* milliseconds */, COOKIE_MY_TIMER); } } handle_calendar_timer(ctx, handle, cookie); }
/* * Timer handling. Includes a hold off for a period of time if there is resource contention */ void handle_calendar_timer(void *cookie) { // Show the alert and let the world know if ((int)cookie >= ALERT_EVENT && (int)cookie <= ALERT_EVENT + MAX_EVENTS) { int num = (int)cookie - ALERT_EVENT; if (timer_rec[num].active == false) return; // Already had the data for this event deleted - cannot show it. timer_rec[num].active = false; for (int i = num + 1; i < max_entries; i++) { if (timer_rec[i].active == true) { handle_calendar_timer((void *)100 + i); vibes_short_pulse(); light_enable_interaction(); return; } } //draw_date(); return; } if ((int)cookie >= 100 && (int)cookie <= 100 + MAX_EVENTS) { int num = (int)cookie - 100; if (timer_rec[num].active == false) return; // Already had the data for this event Event event = events[num]; // Compute the event start time as a figure in ms int time_position = 9; if (event.start_date[5] != '/') time_position = 6; int hour = a_to_i(&event.start_date[time_position],2); int minute_position = time_position + 3; if (event.start_date[time_position + 1] == ':') minute_position = time_position + 2; int minute = a_to_i(&event.start_date[minute_position],2); uint32_t event_in_ms = (hour * 3600 + minute * 60) * 1000; // Get now as ms time_t rawtime; time(&rawtime); struct tm *time = localtime(&rawtime); uint32_t now_in_ms = (time->tm_hour * 3600 + time->tm_min * 60 + time->tm_sec) * 1000; // Work out the alert interval int32_t alert_event = event_in_ms - now_in_ms; // If this is negative then we are after the alert period if (alert_event >= 0) { set_relative_desc(num, alert_event); display_event_text(timer_rec[num].event_desc, timer_rec[num].relative_desc); if (alert_event == 0) { timer_rec[num].handle = app_timer_register(30000, handle_calendar_timer, (void *)ALERT_EVENT + num); vibes_double_pulse(); light_enable_interaction(); } else if (alert_event > 0) { timer_rec[num].handle = app_timer_register(60000 - time->tm_sec * 1000, handle_calendar_timer, (void *)100 + num); } } return; } // Server requests if ((int)cookie != REQUEST_CALENDAR_KEY) return; // If we're going to make a call to the phone, then a dictionary is a good idea. DictionaryIterator *iter; app_message_outbox_begin(&iter); // We didn't get a dictionary - so go away and wait until resources are available if (!iter) { // Can't get an dictionary then come back in a second app_timer_register(1000, handle_calendar_timer, (void *)cookie); return; } // Make the appropriate call to the server if ((int)cookie == REQUEST_CALENDAR_KEY) { calendar_request(iter); app_timer_register(REQUEST_CALENDAR_INTERVAL_MS, handle_calendar_timer, (void *)cookie); } }