Skip to content

Instantly share code, notes, and snippets.

@matthewtole
Last active December 19, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthewtole/5942985 to your computer and use it in GitHub Desktop.
Save matthewtole/5942985 to your computer and use it in GitHub Desktop.
Basic countdown app for the Pebble smartwatch.
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"
#define MY_UUID { 0xD6, 0xEB, 0x90, 0xCA, 0x3F, 0x77, 0x43, 0xB7, 0xAD, 0x5A, 0x79, 0xDE, 0x29, 0xF8, 0x68, 0x7C }
PBL_APP_INFO(MY_UUID, "Simple Countdown", "Matthew Tole", 1, 0, DEFAULT_MENU_ICON, APP_INFO_STANDARD_APP);
Window window;
TextLayer text_layer;
AppTimerHandle timer_handle;
#define COOKIE_COUNTDOWN 1
int time_remaining = 5 * 60;
/**
* Set a timer event for 1 second from now.
*/
void set_timer(AppContextRef ctx) {
timer_handle = app_timer_send_event(ctx, 1000, COOKIE_COUNTDOWN);
}
/**
* Update the text layer with the time remaining.
*/
void update_countdown_text() {
static char countdown_text[] = "00:00";
snprintf(countdown_text, sizeof(countdown_text), "%02d:%02d", (time_remaining / 60), (time_remaining % 60));
text_layer_set_text(&text_layer, countdown_text);
}
/**
* Event handler for all timer events.
* If the timer cookie is the countdown, decrement the time remaining, update
* the text and set the timer again.
*/
void handle_timer(AppContextRef ctx, AppTimerHandle handle, uint32_t cookie) {
if (cookie == COOKIE_COUNTDOWN) {
time_remaining -= 1;
update_countdown_text();
if (time_remaining > 0) {
set_timer(ctx);
}
}
}
/**
* Handler for the app init event.
* Create the window and text layer, and then start the countdown.
*/
void handle_init(AppContextRef ctx) {
window_init(&window, "Countdown Window");
window_stack_push(&window, true);
text_layer_init(&text_layer, GRect(0, 46, 144, 60));
text_layer_set_text_color(&text_layer, GColorBlack);
text_layer_set_background_color(&text_layer, GColorClear);
text_layer_set_font(&text_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
text_layer_set_text_alignment(&text_layer, GTextAlignmentCenter);
layer_add_child(&window.layer, &text_layer.layer);
update_countdown_text();
set_timer(ctx);
}
/**
* Pebble app entry point.
*/
void pbl_main(void *params) {
PebbleAppHandlers handlers = {
.init_handler = &handle_init,
.timer_handler = &handle_timer
};
app_event_loop(params, &handlers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment