Advertisement
Guest User

Untitled

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