Last active
August 29, 2015 14:14
-
-
Save davidstosik/b829146049cee9b818bd to your computer and use it in GitHub Desktop.
Fixes to github.com/bemn67/MinimalTwist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pebble.h> | |
#include <time.h> | |
Window *my_window; | |
TextLayer *min_layer; | |
static GFont s_time_font; | |
static BitmapLayer *s_background_layer; | |
static GBitmap *s_background_bitmap; | |
static void update_time() { | |
// Get a tm structure | |
time_t temp = time(NULL); | |
struct tm *tick_time = localtime(&temp); | |
// Create a long-lived buffer | |
static char min_buffer[] = "00"; | |
strftime(min_buffer, sizeof("00"), "%M", tick_time); | |
text_layer_set_text(min_layer, min_buffer); | |
// updated_hours will be FALSE when watchface starts, then set to TRUE forever. | |
static bool updated_hours = S_FALSE; | |
if (!updated_hours || tick_time->tm_min == 0) { | |
updated_hours = S_TRUE; | |
int resource = 0; | |
// tick_time can tell us want hour it is between 0 and 23, as an int. | |
// modulo (%) brings it back to a 0-11 interval. | |
switch(tick_time->tm_hour % 12) { | |
case 1: resource = RESOURCE_ID_F1; break; | |
case 2: resource = RESOURCE_ID_F2; break; | |
case 3: resource = RESOURCE_ID_F3; break; | |
case 4: resource = RESOURCE_ID_F4; break; | |
case 5: resource = RESOURCE_ID_F5; break; | |
case 6: resource = RESOURCE_ID_F6; break; | |
case 7: resource = RESOURCE_ID_F7; break; | |
case 8: resource = RESOURCE_ID_F8; break; | |
case 9: resource = RESOURCE_ID_F9; break; | |
case 10: resource = RESOURCE_ID_F10; break; | |
case 11: resource = RESOURCE_ID_F11; break; | |
case 0: resource = RESOURCE_ID_F12; break; | |
default: resource = 0; // Should never happen. | |
} | |
// Create GBitMap from right resource and apply it to background layer | |
gbitmap_destroy(s_background_bitmap); // first free it to keep our memory clean | |
s_background_bitmap = gbitmap_create_with_resource(resource); | |
bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap); | |
} | |
} | |
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { | |
update_time(); | |
} | |
static void main_window_load(Window *window) { | |
// Create and add background layer | |
s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168)); | |
layer_add_child(window_get_root_layer(my_window), bitmap_layer_get_layer(s_background_layer)); | |
// Create time TextLayer | |
min_layer = text_layer_create(GRect(0, 55, 144, 50)); | |
text_layer_set_background_color(min_layer, GColorClear); | |
text_layer_set_text_color(min_layer, GColorWhite); | |
// Create GFont | |
s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_BAKENEKO_45)); | |
// Apply to TextLayer | |
text_layer_set_font(min_layer, s_time_font); | |
text_layer_set_text_alignment(min_layer, GTextAlignmentCenter); | |
// Add it as a child layer to the Window's root layer | |
layer_add_child(window_get_root_layer(window), text_layer_get_layer(min_layer)); | |
// Make sure the time is displayed from the start | |
update_time(); | |
} | |
static void main_window_unload(Window *window) { | |
// Destroy TextLayer | |
text_layer_destroy(min_layer); | |
// Destroy Bitmap Layer | |
bitmap_layer_destroy(s_background_layer); | |
// Unload GFont | |
fonts_unload_custom_font(s_time_font); | |
// Destroy Bitmaps | |
gbitmap_destroy(s_background_bitmap); | |
} | |
static void init() { | |
// Create main Window element and assign to pointer | |
my_window = window_create(); | |
// Set handlers to manage the elements inside the Window | |
window_set_window_handlers(my_window, (WindowHandlers) { | |
.load = main_window_load, | |
.unload = main_window_unload | |
}); | |
// Show the Window on the watch, with animated=true | |
window_stack_push(my_window, true); | |
// Register with TickTimerService | |
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler); | |
} | |
static void deinit() { | |
// Destroy Window | |
window_destroy(my_window); | |
// Destroy TextLayer | |
// text_layer_destroy(s_time_layer); | |
} | |
int main(void) { | |
init(); | |
app_event_loop(); | |
deinit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment