johtull

Johtull's Binary Clock Source

May 18th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. #include <pebble.h>
  2. //custom font: http://pastebin.com/agK5KeDn
  3. static Window *s_main_window;
  4. static TextLayer *s_time_hours;
  5. static TextLayer *s_time_minutes;
  6. static GFont s_time_font;
  7.  
  8. static char *tochar(int i, char *p) {
  9.     if (i / 10 == 0) {
  10.         *p++ = i + '0';
  11.         *p = '\0';
  12.         return p;
  13.     }
  14.     p = tochar(i / 10, p);
  15.     *p++ = i % 10 + '0';
  16.     *p = '\0';
  17.     return p;
  18. }
  19.  
  20. static void decimal_binary(char* result) {
  21.   int n = atoi(result);
  22.   int rem, i=1, binary=0;
  23.   while (n!=0) {
  24.       rem=n%2;
  25.       n/=2;
  26.       binary+=rem*i;
  27.       i*=10;
  28.   }
  29.   tochar(binary, result);
  30. }
  31.  
  32. static void update_time() {
  33.   time_t temp = time(NULL);
  34.   struct tm *tick_time = localtime(&temp);
  35.  
  36.   static char hours[] = "000000";
  37.   static char minutes[] = "000000";
  38.  
  39.   strftime(hours, sizeof("00"), "%H", tick_time);
  40.   strftime(minutes, sizeof("00"), "%M", tick_time);
  41.  
  42.   decimal_binary(hours);
  43.   decimal_binary(minutes);
  44.  
  45.   text_layer_set_text(s_time_hours, hours);
  46.   text_layer_set_text(s_time_minutes, minutes);
  47. }
  48.  
  49.  
  50.  
  51. //144×168
  52. static void main_window_load(Window *window) {
  53.   s_time_hours = text_layer_create(GRect(0, 46, 144, 40));
  54.   text_layer_set_background_color(s_time_hours, GColorClear);
  55.   text_layer_set_text_color(s_time_hours, GColorBlack);
  56.   text_layer_set_text(s_time_hours, "0");
  57.   s_time_minutes = text_layer_create(GRect(0, 86, 144, 40));
  58.   text_layer_set_background_color(s_time_minutes, GColorClear);
  59.   text_layer_set_text_color(s_time_minutes, GColorBlack);
  60.   text_layer_set_text(s_time_minutes, "1");
  61.  
  62.  
  63.   s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_BINARY_BLOCKS_25));
  64.  
  65.   text_layer_set_font(s_time_hours, s_time_font);
  66.   text_layer_set_text_alignment(s_time_hours, GTextAlignmentCenter);
  67.   text_layer_set_font(s_time_minutes, s_time_font);
  68.   text_layer_set_text_alignment(s_time_minutes, GTextAlignmentCenter);
  69.  
  70.   layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_hours));
  71.   layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_minutes));
  72.  
  73.   update_time();
  74. }
  75.  
  76. static void main_window_unload(Window *window) {
  77.   text_layer_destroy(s_time_hours);
  78.   text_layer_destroy(s_time_minutes);
  79.   fonts_unload_custom_font(s_time_font);
  80. }
  81.  
  82. static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  83.   update_time();
  84. }
  85.  
  86. static void init() {
  87.   s_main_window = window_create();
  88.  
  89.   window_set_window_handlers(s_main_window, (WindowHandlers) {
  90.     .load = main_window_load,
  91.     .unload = main_window_unload
  92.   });
  93.  
  94.   window_stack_push(s_main_window, true);
  95.  
  96.   tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
  97. }
  98.  
  99. static void deinit() {
  100.   window_destroy(s_main_window);
  101. }
  102.  
  103. int main(void) {
  104.   init();
  105.   app_event_loop();
  106.   deinit();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment