Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pebble.h>
- //custom font: http://pastebin.com/agK5KeDn
- static Window *s_main_window;
- static TextLayer *s_time_hours;
- static TextLayer *s_time_minutes;
- static GFont s_time_font;
- static char *tochar(int i, char *p) {
- if (i / 10 == 0) {
- *p++ = i + '0';
- *p = '\0';
- return p;
- }
- p = tochar(i / 10, p);
- *p++ = i % 10 + '0';
- *p = '\0';
- return p;
- }
- static void decimal_binary(char* result) {
- int n = atoi(result);
- int rem, i=1, binary=0;
- while (n!=0) {
- rem=n%2;
- n/=2;
- binary+=rem*i;
- i*=10;
- }
- tochar(binary, result);
- }
- static void update_time() {
- time_t temp = time(NULL);
- struct tm *tick_time = localtime(&temp);
- static char hours[] = "000000";
- static char minutes[] = "000000";
- strftime(hours, sizeof("00"), "%H", tick_time);
- strftime(minutes, sizeof("00"), "%M", tick_time);
- decimal_binary(hours);
- decimal_binary(minutes);
- text_layer_set_text(s_time_hours, hours);
- text_layer_set_text(s_time_minutes, minutes);
- }
- //144×168
- static void main_window_load(Window *window) {
- s_time_hours = text_layer_create(GRect(0, 46, 144, 40));
- text_layer_set_background_color(s_time_hours, GColorClear);
- text_layer_set_text_color(s_time_hours, GColorBlack);
- text_layer_set_text(s_time_hours, "0");
- s_time_minutes = text_layer_create(GRect(0, 86, 144, 40));
- text_layer_set_background_color(s_time_minutes, GColorClear);
- text_layer_set_text_color(s_time_minutes, GColorBlack);
- text_layer_set_text(s_time_minutes, "1");
- s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_BINARY_BLOCKS_25));
- text_layer_set_font(s_time_hours, s_time_font);
- text_layer_set_text_alignment(s_time_hours, GTextAlignmentCenter);
- text_layer_set_font(s_time_minutes, s_time_font);
- text_layer_set_text_alignment(s_time_minutes, GTextAlignmentCenter);
- layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_hours));
- layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_minutes));
- update_time();
- }
- static void main_window_unload(Window *window) {
- text_layer_destroy(s_time_hours);
- text_layer_destroy(s_time_minutes);
- fonts_unload_custom_font(s_time_font);
- }
- static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
- update_time();
- }
- static void init() {
- 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, true);
- tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
- }
- static void deinit() {
- window_destroy(s_main_window);
- }
- int main(void) {
- init();
- app_event_loop();
- deinit();
- }
Advertisement
Add Comment
Please, Sign In to add comment