4

I'm learning watchface development. I have been following the Pebble guide closely, so 80% of my code is the same as their sample code. I'm probably missing something very small, but my face does not seem to be correctly subscribed to the time service.

What am I doing wrong?

In init(), I have:

tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
tick_timer_service_subscribe(DAY_UNIT, tick_handler);

Here's tick_handler:

static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  update_time();
}

Here's update_time:

static void update_time() {
  time_t temp = time(NULL); 
  struct tm *tick_time = localtime(&temp);

  static char time_buffer[] = "00:00";
  static char date_buffer[] = "00/00/00";

  if (clock_is_24h_style() == true) {
    strftime(time_buffer, sizeof(time_buffer), "%H:%M", tick_time);
  } else {
    strftime(time_buffer, sizeof(time_buffer), "%I:%M", tick_time);
  } 
  text_layer_set_text(s_time_layer, time_buffer);

  strftime(date_buffer, sizeof(date_buffer), "%D", tick_time);
  text_layer_set_text(s_date_layer, date_buffer);
}

The face only updates the time when it first loads (by calling update_time).

Antrikshy
  • 2,918
  • 4
  • 31
  • 65

1 Answers1

9

TimeUnits is a bit mask. You set a mask and then call tick_timer_service_subscribe once. Your second call using DAY_UNITS is changing your subscription. To subscribe to both units, you bitwise-or your mask bits:

tick_timer_service_subscribe(MINUTE_UNIT | DAY_UNIT, tick_handler);

Notice how your tick handler has a TimeUnits argument. That argument tells you which unit triggered the handler. In your case, you always want to update the time and it appears DAY_UNIT is redundant. But you could do this:

static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
    if( (units_changed & MINUTE_UNIT) != 0 ) {
        /* Minutes changed */
    }

    if( (units_changed & DAY_UNIT) != 0 ) {
        /* Days changed */
    }
}
indiv
  • 17,306
  • 6
  • 61
  • 82
  • Thank you for the great explanation. My watchface actually shows the time now. I was wondering why they had parameters in `tick_handler`. I'm guessing the subscribe function expects them (like callbacks in JS) so I can't just remove them. I'm still curious why that struct ™ is there when it's possible to simply use `localtime` to generate one when needed. – Antrikshy Nov 20 '14 at 06:19
  • Gread explanation @indiv! Antrikshy: Tick_timer will call you back precisely when the seconds or minute change. This is better than setting a timer that wakes you up every second (or every minute) and would not be precisely synchronized with actual time change. – sarfata Nov 20 '14 at 22:46