Created
December 19, 2015 21:52
-
-
Save exiva/3353a2ca84368566b937 to your computer and use it in GitHub Desktop.
This file contains 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> | |
static Window *window; | |
//Setup Layers. | |
static Layer *year_layer; | |
static Layer *time_layer; | |
static Layer *date_layer; | |
static Layer *top_random; | |
static Layer *bottom_random; | |
static GSize textSize; | |
#define FONT_PADDING 6 | |
#define NUMBER_GAP 10 | |
static int getRandNumber() { | |
return rand() % 9; | |
} | |
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { | |
if (units_changed & MINUTE_UNIT) { | |
layer_mark_dirty(time_layer); | |
} | |
if (units_changed & DAY_UNIT) { | |
layer_mark_dirty(date_layer); | |
} | |
if (units_changed & YEAR_UNIT) { | |
layer_mark_dirty(year_layer); | |
} | |
} | |
static void update_time(Layer *layer, GContext *ctx) { | |
APP_LOG(APP_LOG_LEVEL_INFO, "Drawing Time Layer."); | |
GRect bounds = layer_get_bounds(layer); | |
graphics_context_set_text_color(ctx, GColorWhite); | |
graphics_context_set_fill_color(ctx, PBL_IF_COLOR_ELSE(GColorOrange, GColorLightGray)); | |
graphics_fill_rect(ctx, GRect(0, 0, bounds.size.w, bounds.size.h-1),0,GCornerNone); | |
time_t temp = time(NULL); | |
struct tm *tick_time = localtime(&temp); | |
static char s_buffer[5][5]; | |
strftime(s_buffer[0], sizeof(s_buffer[0]), clock_is_24h_style() ? "%H%M" : "%I%M", tick_time); | |
int n = 0; | |
for (size_t i = 1; i < 5; i++) { | |
s_buffer[i][0] = s_buffer[0][n]; | |
s_buffer[i][1] = '\n'; | |
graphics_draw_text(ctx, s_buffer[i], fonts_get_system_font(FONT_KEY_LECO_42_NUMBERS), | |
GRect((NUMBER_GAP * n) + (textSize.w * n) + FONT_PADDING, -7, textSize.w, bounds.size.h), | |
GTextOverflowModeFill, GTextAlignmentCenter, NULL); | |
n++; | |
} | |
} | |
static void update_date(Layer *layer, GContext *ctx) { | |
APP_LOG(APP_LOG_LEVEL_INFO, "Drawing Date Layer."); | |
GRect bounds = layer_get_bounds(layer); | |
graphics_context_set_text_color(ctx, GColorWhite); | |
time_t temp = time(NULL); | |
struct tm *tick_time = localtime(&temp); | |
static char s_buffer[5][5]; | |
strftime(s_buffer[0], sizeof(s_buffer), "%m%d", tick_time); | |
int n = 0; | |
for (size_t i = 1; i < 5; i++) { | |
s_buffer[i][0] = s_buffer[0][n]; | |
s_buffer[i][1] = '\n'; | |
graphics_draw_text(ctx, s_buffer[i], fonts_get_system_font(FONT_KEY_LECO_42_NUMBERS), | |
GRect((NUMBER_GAP * n) + (textSize.w * n) + FONT_PADDING, 0, textSize.w, bounds.size.h), | |
GTextOverflowModeFill, GTextAlignmentCenter, NULL); | |
n++; | |
} | |
} | |
static void update_year(Layer *layer, GContext *ctx) { | |
APP_LOG(APP_LOG_LEVEL_INFO, "Drawing Year Layer."); | |
GRect bounds = layer_get_bounds(layer); | |
graphics_context_set_text_color(ctx, GColorWhite); | |
time_t temp = time(NULL); | |
struct tm *tick_time = localtime(&temp); | |
static char s_buffer[5][5]; | |
strftime(s_buffer[0], sizeof(s_buffer[0]), "%Y", tick_time); | |
int n = 0; | |
for (size_t i = 1; i < 5; i++) { | |
s_buffer[i][0] = s_buffer[0][n]; | |
s_buffer[i][1] = '\n'; | |
graphics_draw_text(ctx, s_buffer[i], fonts_get_system_font(FONT_KEY_LECO_42_NUMBERS), | |
GRect((NUMBER_GAP * n) + (textSize.w * n) + FONT_PADDING, 0, textSize.w, bounds.size.h), | |
GTextOverflowModeFill, GTextAlignmentCenter, NULL); | |
n++; | |
} | |
} | |
static void generate_random(Layer *layer, GContext *ctx) { | |
APP_LOG(APP_LOG_LEVEL_INFO, "Drawing Random Number Layers."); | |
GRect bounds = layer_get_bounds(layer); | |
graphics_context_set_text_color(ctx, PBL_IF_COLOR_ELSE(GColorDarkGray, GColorWhite)); | |
char tmp[2]; | |
for (size_t i = 0; i < 4; i++) { | |
snprintf(tmp, 2, "%d", getRandNumber()); | |
graphics_draw_text(ctx, tmp, fonts_get_system_font(FONT_KEY_LECO_42_NUMBERS), | |
GRect((NUMBER_GAP * i) + (textSize.w * i) + FONT_PADDING, 0, textSize.w, bounds.size.h), | |
GTextOverflowModeFill, GTextAlignmentCenter, NULL); | |
} | |
} | |
static void window_load(Window *window) { | |
textSize = graphics_text_layout_get_content_size("0", | |
fonts_get_system_font(FONT_KEY_LECO_42_NUMBERS), | |
GRect(0, 0, 100, 42), GTextOverflowModeFill, | |
GTextAlignmentCenter); | |
Layer *window_layer = window_get_root_layer(window); | |
GRect bounds = layer_get_bounds(window_layer); | |
window_set_background_color(window, GColorBlack); | |
//draw random numbers | |
//these are static for the life of the run. Because fuck it. who cares. | |
top_random = layer_create(GRect(0, -32, bounds.size.w, textSize.h)); | |
layer_add_child(window_layer, top_random); | |
layer_set_update_proc(top_random, generate_random); | |
bottom_random = layer_create(GRect(0, 144, bounds.size.w, textSize.h)); | |
layer_add_child(window_layer, bottom_random); | |
layer_set_update_proc(bottom_random, generate_random); | |
year_layer = layer_create(GRect(0, 12, bounds.size.w, textSize.h)); | |
layer_add_child(window_layer, year_layer); | |
layer_set_update_proc(year_layer, update_year); | |
time_layer = layer_create(GRect(0, 63, bounds.size.w, textSize.h)); | |
layer_add_child(window_layer,time_layer); | |
layer_set_update_proc(time_layer, update_time); | |
date_layer = layer_create(GRect(0, 100, bounds.size.w, textSize.h)); | |
layer_add_child(window_layer, date_layer); | |
layer_set_update_proc(date_layer, update_date); | |
} | |
static void window_unload(Window *window) { | |
layer_destroy(top_random); | |
layer_destroy(bottom_random); | |
layer_destroy(year_layer); | |
layer_destroy(time_layer); | |
layer_destroy(date_layer); | |
} | |
static void init(void) { | |
window = window_create(); | |
window_set_window_handlers(window, (WindowHandlers) { | |
.load = window_load, | |
.unload = window_unload, | |
}); | |
srand(time(NULL)); //seed random number generator | |
//subscribe to multiple tick events to update layers accordingly. | |
tick_timer_service_subscribe(MINUTE_UNIT | DAY_UNIT | YEAR_UNIT, tick_handler); | |
window_stack_push(window, false); | |
} | |
static void deinit(void) { | |
tick_timer_service_unsubscribe(); | |
window_destroy(window); | |
} | |
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