Reputation: 374
Very basic question that I've been trying to figure out by myself for a couple hours but can't solve it.
So I'm creating my first watchface for Pebble and I want the watch to buzz (vibrate) every hour. You remember some digital watches make a "beep" at o'clock times? Well something like that.
I figure it out the most simple way to do it is:
if (tick_time->tm_min == 0) {
vibes_short_pulse();
}
It works great. Now I have a little bug. During that minute, if the window is re-created (for example, if you go to the timeline or open a watchapp and go back quickly to my watchface) the update_time() is called again so it buzzes again. Not a huge bug but kinda annoying.
I tried to workaround with a boolean flag.
static bool vibed = false;
And then,
if (tick_time->tm_min != 0) {
vibed = false;
}
if (tick_time->tm_min == 0 && vibed == false) {
vibes_short_pulse();
vibed = true;
}
But apparently, everytime the window is re-created the flag goes back to "false" no matter what.
I don't know how to solve it. If you have a hint for me, would be appreciate it. Thanks!
Upvotes: 0
Views: 261
Reputation: 2547
I would just remember the hour of your last buzz. Only buzz if it's different. So:
//initialize to impossible hour so it works the first time
static int lastBuzzHour=-1;
if (tick_time->tm_hour !=lastBuzzHour) {
vibes_short_pulse();
// now that you've buzzed, remember it so you don't retrigger
lastBuzzHour=tick_time->tm_hour;
}
Upvotes: 0
Reputation: 39777
Instead of checking minutes for zero, check for hour change event, e.g.:
void tick_handler(struct tm *tick_time, TimeUnits units_changed)
{
// Some other code that display/process time
// on hour change do a short buzz
if (units_changed & HOUR_UNIT) {
vibes_short_pulse();
}
}
Upvotes: 2