#include "palette_manip.h" #include // window statics static Window *s_main_window; static BitmapLayer *s_time_mg_layers[4]; static BitmapLayer *s_time_fg_layers[4]; static const uint8_t s_x_r = (PBL_DISPLAY_WIDTH / 2) + 2; static const uint8_t s_x_l = (s_x_r - 2 * (69 / 2)) - 5; static const GRect s_time_grects[4] = {GRect(s_x_l, 2, 69, 110), GRect(s_x_r, 2, 69, 110), GRect(s_x_l, 116, 69, 110), GRect(s_x_r, 116, 69, 110)}; // persist statics&defines static GColor8 s_led_color; static GColor8 s_bg_color; static GColor8 s_mg_color; static GColor8 s_fg_color; #define storage_key_led_color 0 #define storage_key_bg_color 1 #define storage_key_mg_color 2 #define storage_key_fg_color 3 // font static GBitmap *s_font_bitmaps[10]; static void main_window_load() { Layer *window_layer = window_get_root_layer(s_main_window); if (persist_exists(storage_key_led_color)) { light_set_color_rgb888(persist_read_int(storage_key_led_color)); } else { light_set_color(GColorWhite); } if (persist_exists(storage_key_bg_color)) { window_set_background_color(s_main_window, (GColor8){.argb = persist_read_int(storage_key_bg_color)}); } else { window_set_background_color(s_main_window, GColorBlue); } if (persist_exists(storage_key_mg_color)) { s_time_mg_layers[0] = bitmap_layer_create(s_time_grects[0]); s_time_mg_layers[1] = bitmap_layer_create(s_time_grects[1]); s_time_mg_layers[2] = bitmap_layer_create(s_time_grects[2]); s_time_mg_layers[3] = bitmap_layer_create(s_time_grects[3]); bitmap_layer_set_compositing_mode(s_time_mg_layers[0], GCompOpSet); bitmap_layer_set_compositing_mode(s_time_mg_layers[1], GCompOpSet); bitmap_layer_set_compositing_mode(s_time_mg_layers[2], GCompOpSet); bitmap_layer_set_compositing_mode(s_time_mg_layers[3], GCompOpSet); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[0])); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[1])); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[2])); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_mg_layers[3])); } if (persist_exists(storage_key_fg_color)) { replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[0], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[1], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[2], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[3], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[4], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[5], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[6], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[7], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[8], NULL); replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[9], NULL); } layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[0])); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[1])); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[2])); layer_add_child(window_layer, bitmap_layer_get_layer(s_time_fg_layers[3])); } static void main_window_unload() { if (s_time_mg_layers[0]) { bitmap_layer_destroy(s_time_mg_layers[0]); bitmap_layer_destroy(s_time_mg_layers[1]); bitmap_layer_destroy(s_time_mg_layers[2]); bitmap_layer_destroy(s_time_mg_layers[3]); } } static void update_minute_1() { time_t temp = time(NULL); struct tm *tick_time = localtime(&temp); uint8_t hour = (uint8_t)tick_time->tm_hour; if (!clock_is_24h_style()) { hour = hour % 12; if (hour == 0) { hour = 12; } } uint8_t minute = (uint8_t)tick_time->tm_min; static uint8_t s_time_digits[4]; s_time_digits[0] = hour / 10; s_time_digits[1] = hour % 10; s_time_digits[2] = minute / 10; s_time_digits[3] = minute % 10; bitmap_layer_set_bitmap(s_time_fg_layers[0], s_font_bitmaps[s_time_digits[0]]); bitmap_layer_set_bitmap(s_time_fg_layers[1], s_font_bitmaps[s_time_digits[1]]); bitmap_layer_set_bitmap(s_time_fg_layers[2], s_font_bitmaps[s_time_digits[2]]); bitmap_layer_set_bitmap(s_time_fg_layers[3], s_font_bitmaps[s_time_digits[3]]); } static void minute_handler(struct tm *tick_time, TimeUnits units_changed) { update_minute_1(); } static void soft_reload(bool first_load) { if (!first_load) { window_stack_remove(s_main_window, false); window_destroy(s_main_window); } s_main_window = window_create(); window_set_window_handlers( s_main_window, (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); window_stack_push(s_main_window, false); } static void init() { // initialize font array s_font_bitmaps[0] = gbitmap_create_with_resource(RESOURCE_ID_0); s_font_bitmaps[1] = gbitmap_create_with_resource(RESOURCE_ID_1); s_font_bitmaps[2] = gbitmap_create_with_resource(RESOURCE_ID_2); s_font_bitmaps[3] = gbitmap_create_with_resource(RESOURCE_ID_3); s_font_bitmaps[4] = gbitmap_create_with_resource(RESOURCE_ID_4); s_font_bitmaps[5] = gbitmap_create_with_resource(RESOURCE_ID_5); s_font_bitmaps[6] = gbitmap_create_with_resource(RESOURCE_ID_6); s_font_bitmaps[7] = gbitmap_create_with_resource(RESOURCE_ID_7); s_font_bitmaps[8] = gbitmap_create_with_resource(RESOURCE_ID_8); s_font_bitmaps[9] = gbitmap_create_with_resource(RESOURCE_ID_9); // do everything else s_time_fg_layers[0] = bitmap_layer_create(s_time_grects[0]); s_time_fg_layers[1] = bitmap_layer_create(s_time_grects[1]); s_time_fg_layers[2] = bitmap_layer_create(s_time_grects[2]); s_time_fg_layers[3] = bitmap_layer_create(s_time_grects[3]); bitmap_layer_set_compositing_mode(s_time_fg_layers[0], GCompOpSet); bitmap_layer_set_compositing_mode(s_time_fg_layers[1], GCompOpSet); bitmap_layer_set_compositing_mode(s_time_fg_layers[2], GCompOpSet); bitmap_layer_set_compositing_mode(s_time_fg_layers[3], GCompOpSet); update_minute_1(); soft_reload(true); tick_timer_service_subscribe(MINUTE_UNIT, minute_handler); } static void deinit() { tick_timer_service_unsubscribe(); bitmap_layer_destroy(s_time_fg_layers[0]); bitmap_layer_destroy(s_time_fg_layers[1]); bitmap_layer_destroy(s_time_fg_layers[2]); bitmap_layer_destroy(s_time_fg_layers[3]); gbitmap_destroy(s_font_bitmaps[0]); gbitmap_destroy(s_font_bitmaps[1]); gbitmap_destroy(s_font_bitmaps[2]); gbitmap_destroy(s_font_bitmaps[3]); gbitmap_destroy(s_font_bitmaps[4]); gbitmap_destroy(s_font_bitmaps[5]); gbitmap_destroy(s_font_bitmaps[6]); gbitmap_destroy(s_font_bitmaps[7]); gbitmap_destroy(s_font_bitmaps[8]); gbitmap_destroy(s_font_bitmaps[9]); window_destroy(s_main_window); } int main(void) { init(); app_event_loop(); deinit(); }