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
).
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 */
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With