Skip to content

Instantly share code, notes, and snippets.

@adamgreig
Last active December 17, 2015 14:19
Show Gist options
  • Save adamgreig/5623314 to your computer and use it in GitHub Desktop.
Save adamgreig/5623314 to your computer and use it in GitHub Desktop.
Simple "hours remaining" countdown clock for Pebble. Counts down until the deadline for my Masters project!
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"
#define MY_UUID { 0x8B, 0xA6, 0x5F, 0x1D, 0xB1, 0x08, 0x44, 0xAA, 0xA8, 0x17, 0x17, 0xFE, 0xCD, 0x3A, 0x89, 0xDA }
PBL_APP_INFO(MY_UUID,
"IIB Countdown", "Adam Greig",
1, 0, /* App version */
DEFAULT_MENU_ICON,
APP_INFO_WATCH_FACE);
Window window;
TextLayer hours_text;
TextLayer static_text;
char buf[4];
int last = 0;
void fill_buf(int number, char* buf) {
char hundreds = number / 100 + '0';
char tens = number % 100 / 10 + '0';
char ones = number % 10 + '0';
if(hundreds != '0') {
buf[0] = hundreds; buf[1] = tens; buf[2] = ones; buf[3] = 0;
} else if(tens != '0') {
buf[0] = tens; buf[1] = ones; buf[2] = 0;
} else {
buf[0] = ones; buf[1] = 0;
}
}
void update_time(PblTm* t) {
int hours = (148 - t->tm_yday)*24 + (15 - t->tm_hour);
if(hours != last) {
if(hours > 0) {
fill_buf(hours, buf);
text_layer_set_text(&hours_text, buf);
if(hours == 1) {
text_layer_set_text(&static_text, "HOUR\nREMAINS");
}
last = hours;
} else if(hours == 0) {
int mins = 60 - t->tm_min;
fill_buf(mins, buf);
text_layer_set_text(&hours_text, buf);
if(mins > 1) {
text_layer_set_text(&static_text, "MINUTES\nREMAIN");
} else {
text_layer_set_text(&static_text, "MINUTE\nREMAINS");
}
last = mins;
} else {
text_layer_set_text(&hours_text, "DONE");
text_layer_set_text(&static_text, "IT'S ALL\nOVER!");
last = hours;
}
}
}
void handle_init(AppContextRef ctx) {
(void)ctx;
window_init(&window, "IIB Countdown");
window_set_background_color(&window, GColorBlack);
GFont hours_font = fonts_get_system_font(FONT_KEY_GOTHAM_42_BOLD);
text_layer_init(&hours_text, GRect(0, 10, window.layer.frame.size.w, 50));
text_layer_set_background_color(&hours_text, GColorBlack);
text_layer_set_text_color(&hours_text, GColorWhite);
text_layer_set_text_alignment(&hours_text, GTextAlignmentCenter);
text_layer_set_font(&hours_text, hours_font);
layer_add_child(&window.layer, &hours_text.layer);
GFont static_font = fonts_get_system_font(FONT_KEY_GOTHIC_28);
text_layer_init(&static_text, GRect(0, 60, window.layer.frame.size.w,
window.layer.frame.size.h - 60));
text_layer_set_background_color(&static_text, GColorBlack);
text_layer_set_text_color(&static_text, GColorWhite);
text_layer_set_font(&static_text, static_font);
text_layer_set_text_alignment(&static_text, GTextAlignmentCenter);
text_layer_set_text(&static_text, "HOURS\nREMAIN");
layer_add_child(&window.layer, &static_text.layer);
PblTm t;
get_time(&t);
update_time(&t);
window_stack_push(&window, true);
}
static void handle_minute_tick(AppContextRef app_ctx, PebbleTickEvent* e) {
update_time(e->tick_time);
}
void pbl_main(void *params) {
PebbleAppHandlers handlers = {
.init_handler = &handle_init,
.tick_info = {
.tick_handler = &handle_minute_tick,
.tick_units = MINUTE_UNIT
}
};
app_event_loop(params, &handlers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment