Advertisement
Guest User

main.c

a guest
Jun 20th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. #include <pebble.h>
  2.  
  3. static Window *s_main_window;
  4. static TextLayer *s_time_layer, *s_date_layer;
  5. static BitmapLayer *s_background_layer;
  6. static GBitmap *s_background_bitmap;
  7. static GFont s_time_font, s_date_font;
  8.  
  9.  
  10. static void update_time() {
  11.   // Get a tm structure
  12.   time_t temp = time(NULL);
  13.   struct tm *tick_time = localtime(&temp);
  14.  
  15.   // Write the current hours and minutes into a buffer
  16.   static char s_buffer[8];
  17.   strftime(s_buffer, sizeof(s_buffer), clock_is_24h_style() ?
  18.                                           "%H:%M" : "%I:%M", tick_time);
  19.  
  20.   // Display this time on the TextLayer
  21.   text_layer_set_text(s_time_layer, s_buffer);
  22.  
  23.   // Copy date into buffer from tm structure
  24. static char date_buffer[16];
  25. strftime(date_buffer, sizeof(date_buffer), "%a %b %d", tick_time);
  26.  
  27. // Show the date
  28. text_layer_set_text(s_date_layer, date_buffer);
  29.  
  30. }
  31.  
  32. static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  33.   update_time();
  34. }
  35.  
  36. static void main_window_load(Window *window) {
  37.  // Get information about the Window
  38.   Layer *window_layer = window_get_root_layer(window);
  39.   GRect bounds = layer_get_bounds(window_layer);
  40.   // Create GBitmap
  41. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND);
  42.  
  43. // Create BitmapLayer to display the GBitmap
  44. s_background_layer = bitmap_layer_create(bounds);
  45.  
  46. // Set the bitmap onto the layer and add to the window
  47. bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
  48. layer_add_child(window_layer, bitmap_layer_get_layer(s_background_layer));
  49.  
  50. // Create GFont
  51. s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_WIDE_56));
  52. s_date_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_JUSTICE_24));
  53.  
  54.   // Create the TextLayer with specific bounds
  55.   s_time_layer = text_layer_create(
  56.       GRect(0, PBL_IF_ROUND_ELSE(58, 52), bounds.size.w, 56));
  57.  
  58.   // Improve the layout to be more like a watchface
  59.   text_layer_set_background_color(s_time_layer, GColorClear);
  60.   text_layer_set_text_color(s_time_layer, GColorCyan);
  61.   text_layer_set_text(s_time_layer, "9/11");
  62.   text_layer_set_font(s_time_layer, s_time_font);
  63.   text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  64.  
  65.   // Add it as a child layer to the Window's root layer
  66.   layer_add_child(window_layer, text_layer_get_layer(s_time_layer));
  67.  
  68.   // Create date TextLayer
  69. s_date_layer = text_layer_create(GRect(0, 140, 144, 144));
  70. text_layer_set_text_color(s_date_layer, GColorCyan);
  71. text_layer_set_background_color(s_date_layer, GColorClear);
  72. text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
  73. text_layer_set_font(s_date_layer, s_date_font);
  74.  
  75. // Add to Window
  76. layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_date_layer));
  77.  
  78. }
  79.  
  80. static void main_window_unload(Window *window) {
  81.   // Destroy TextLayer
  82.   text_layer_destroy(s_time_layer);
  83.   text_layer_destroy(s_date_layer);
  84.   // Unload GFont
  85. fonts_unload_custom_font(s_time_font);
  86. fonts_unload_custom_font(s_date_font);
  87.   // Destroy GBitmap
  88. gbitmap_destroy(s_background_bitmap);
  89. // Destroy BitmapLayer
  90. bitmap_layer_destroy(s_background_layer);
  91. }
  92.  
  93. static void init() {
  94.  // Create main Window element and assign to pointer
  95.   s_main_window = window_create();
  96.  
  97.   // Set the background color
  98.   window_set_background_color(s_main_window, GColorBlack);
  99.  
  100.   // Set handlers to manage the elements inside the Window
  101.   window_set_window_handlers(s_main_window, (WindowHandlers) {
  102.     .load = main_window_load,
  103.     .unload = main_window_unload
  104.   });
  105.  
  106.   // Show the Window on the watch, with animated=true
  107.   window_stack_push(s_main_window, true);
  108.   // Register with TickTimerService
  109. tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
  110. }
  111.  
  112. static void deinit() {
  113.  // Destroy Window
  114.   window_destroy(s_main_window);
  115. }
  116.  
  117. int main(void) {
  118.   init();
  119.   app_event_loop();
  120.   deinit();
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement