Guest User

Untitled

a guest
Sep 19th, 2017
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #include <pebble.h>
  2.  
  3. static Window *s_window;
  4. static Layer *s_window_layer;
  5. static TextLayer *s_bg1,*s_bg2,*s_time_layer,*s_date_layer,*s_step_layer;
  6. static char s_current_time_buffer[8],s_current_steps_buffer[16],s_current_date_buffer[32];
  7. static int s_step_count = 0;
  8.  
  9. // Is step data available?
  10. bool step_data_is_available() {
  11.   return HealthServiceAccessibilityMaskAvailable &
  12.     health_service_metric_accessible(HealthMetricStepCount,
  13.       time_start_of_today(), time(NULL));
  14. }
  15.  
  16. // Todays current step count
  17. static void get_step_count() {
  18.   s_step_count = (int)health_service_sum_today(HealthMetricStepCount);
  19. }
  20.  
  21. static void tick_handler(struct tm *tick_time, TimeUnits changed) {
  22.   //Getting the time and formatting it
  23.   strftime(s_current_time_buffer, sizeof(s_current_time_buffer),
  24.            clock_is_24h_style() ? "%H:%M" : "%l:%M", tick_time);
  25.  
  26.   strftime(s_current_date_buffer, sizeof(s_current_date_buffer),"%B %d", tick_time);
  27.  
  28.   text_layer_set_text(s_time_layer, s_current_time_buffer);
  29.   text_layer_set_text(s_date_layer, s_current_date_buffer);
  30. }
  31.  
  32.  
  33. static void display_step_count() {
  34.   int thousands = s_step_count / 1000;
  35.   int hundreds = s_step_count % 1000;
  36.  
  37.   // if more than 1000 steps, the will be showed as 1'000, if less it will be the plain number
  38.   if(thousands > 0) {
  39.     snprintf(s_current_steps_buffer, sizeof(s_current_steps_buffer),"%d'%03d", thousands, hundreds);
  40.   } else {
  41.     snprintf(s_current_steps_buffer, sizeof(s_current_steps_buffer),"%d", hundreds);
  42.   }
  43.   text_layer_set_text(s_step_layer, s_current_steps_buffer);
  44. }
  45.  
  46. static void health_handler(HealthEventType event, void *context) {
  47.   if(event != HealthEventSleepUpdate) {
  48.     get_step_count();
  49.     display_step_count();
  50.   }
  51. }
  52.  
  53. static void window_load(Window *window) {
  54.   GRect window_bounds = layer_get_bounds(s_window_layer);
  55.  
  56.   // Background layers, used to show the 3 different colors. The 3rd one is not needed as the s_window background is black
  57.   s_bg1 = text_layer_create(
  58.       GRect(0, 0, window_bounds.size.w, 56));
  59.   text_layer_set_background_color(s_bg1, GColorBlack);
  60.  
  61.   s_bg2 = text_layer_create(
  62.       GRect(0, 56, window_bounds.size.w, 56));
  63.   text_layer_set_background_color(s_bg2, GColorWhite);
  64.  
  65.   // Information layers
  66.   s_date_layer = text_layer_create(
  67.       GRect(0, 9, window_bounds.size.w, 38));
  68.   text_layer_set_text_color(s_date_layer, GColorWhite);
  69.   text_layer_set_background_color(s_date_layer, GColorClear);
  70.   text_layer_set_font(s_date_layer,
  71.                       fonts_get_system_font(FONT_KEY_GOTHIC_28));
  72.   text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
  73.  
  74.   s_time_layer = text_layer_create(
  75.       GRect(0, 60, window_bounds.size.w, 38));
  76.   text_layer_set_text_color(s_time_layer, GColorDarkGray);
  77.   text_layer_set_background_color(s_time_layer, GColorClear);
  78.   text_layer_set_font(s_time_layer,
  79.                       fonts_get_system_font(FONT_KEY_LECO_38_BOLD_NUMBERS));
  80.   text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  81.  
  82.   s_step_layer = text_layer_create(
  83.       GRect(0,121, window_bounds.size.w, 38));
  84.   text_layer_set_text_color(s_step_layer, GColorWhite);
  85.   text_layer_set_background_color(s_step_layer, GColorClear);
  86.   text_layer_set_font(s_step_layer,
  87.                       fonts_get_system_font(FONT_KEY_GOTHIC_28));
  88.   text_layer_set_text_alignment(s_step_layer, GTextAlignmentCenter);
  89.  
  90.   // Adding the childs to the main layer
  91.   layer_add_child(s_window_layer, text_layer_get_layer(s_bg1));
  92.   layer_add_child(s_window_layer, text_layer_get_layer(s_bg2));
  93.   layer_add_child(s_window_layer, text_layer_get_layer(s_date_layer));
  94.   layer_add_child(s_window_layer, text_layer_get_layer(s_time_layer));
  95.   layer_add_child(s_window_layer, text_layer_get_layer(s_step_layer));
  96.  
  97.   // Subscribe to health events if we can
  98.   if(step_data_is_available()) {
  99.     health_service_events_subscribe(health_handler, NULL);
  100.   }
  101. }
  102.  
  103. static void window_unload(Window *window) {
  104.   layer_destroy(text_layer_get_layer(s_bg1));
  105.   layer_destroy(text_layer_get_layer(s_bg2));
  106.   layer_destroy(text_layer_get_layer(s_date_layer));
  107.   layer_destroy(text_layer_get_layer(s_time_layer));
  108.   layer_destroy(text_layer_get_layer(s_step_layer));
  109. }
  110.  
  111. void init() {
  112.   s_window = window_create();
  113.   s_window_layer = window_get_root_layer(s_window);
  114.   window_set_background_color(s_window, GColorBlack);
  115.  
  116.   window_set_window_handlers(s_window, (WindowHandlers) {
  117.     .load = window_load,
  118.     .unload = window_unload
  119.   });
  120.  
  121.   window_stack_push(s_window, true);
  122.  
  123.   tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
  124. }
  125.  
  126. void deinit() {
  127.   window_destroy(s_window);
  128. }
  129.  
  130. int main() {
  131.   init();
  132.   app_event_loop();
  133.   deinit();
  134. }
Advertisement
Add Comment
Please, Sign In to add comment