Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pebble.h>
- static Window *s_window;
- static Layer *s_window_layer;
- static TextLayer *s_bg1,*s_bg2,*s_time_layer,*s_date_layer,*s_step_layer;
- static char s_current_time_buffer[8],s_current_steps_buffer[16],s_current_date_buffer[32];
- static int s_step_count = 0;
- // Is step data available?
- bool step_data_is_available() {
- return HealthServiceAccessibilityMaskAvailable &
- health_service_metric_accessible(HealthMetricStepCount,
- time_start_of_today(), time(NULL));
- }
- // Todays current step count
- static void get_step_count() {
- s_step_count = (int)health_service_sum_today(HealthMetricStepCount);
- }
- static void tick_handler(struct tm *tick_time, TimeUnits changed) {
- //Getting the time and formatting it
- strftime(s_current_time_buffer, sizeof(s_current_time_buffer),
- clock_is_24h_style() ? "%H:%M" : "%l:%M", tick_time);
- strftime(s_current_date_buffer, sizeof(s_current_date_buffer),"%B %d", tick_time);
- text_layer_set_text(s_time_layer, s_current_time_buffer);
- text_layer_set_text(s_date_layer, s_current_date_buffer);
- }
- static void display_step_count() {
- int thousands = s_step_count / 1000;
- int hundreds = s_step_count % 1000;
- // if more than 1000 steps, the will be showed as 1'000, if less it will be the plain number
- if(thousands > 0) {
- snprintf(s_current_steps_buffer, sizeof(s_current_steps_buffer),"%d'%03d", thousands, hundreds);
- } else {
- snprintf(s_current_steps_buffer, sizeof(s_current_steps_buffer),"%d", hundreds);
- }
- text_layer_set_text(s_step_layer, s_current_steps_buffer);
- }
- static void health_handler(HealthEventType event, void *context) {
- if(event != HealthEventSleepUpdate) {
- get_step_count();
- display_step_count();
- }
- }
- static void window_load(Window *window) {
- GRect window_bounds = layer_get_bounds(s_window_layer);
- // Background layers, used to show the 3 different colors. The 3rd one is not needed as the s_window background is black
- s_bg1 = text_layer_create(
- GRect(0, 0, window_bounds.size.w, 56));
- text_layer_set_background_color(s_bg1, GColorBlack);
- s_bg2 = text_layer_create(
- GRect(0, 56, window_bounds.size.w, 56));
- text_layer_set_background_color(s_bg2, GColorWhite);
- // Information layers
- s_date_layer = text_layer_create(
- GRect(0, 9, window_bounds.size.w, 38));
- text_layer_set_text_color(s_date_layer, GColorWhite);
- text_layer_set_background_color(s_date_layer, GColorClear);
- text_layer_set_font(s_date_layer,
- fonts_get_system_font(FONT_KEY_GOTHIC_28));
- text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
- s_time_layer = text_layer_create(
- GRect(0, 60, window_bounds.size.w, 38));
- text_layer_set_text_color(s_time_layer, GColorDarkGray);
- text_layer_set_background_color(s_time_layer, GColorClear);
- text_layer_set_font(s_time_layer,
- fonts_get_system_font(FONT_KEY_LECO_38_BOLD_NUMBERS));
- text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
- s_step_layer = text_layer_create(
- GRect(0,121, window_bounds.size.w, 38));
- text_layer_set_text_color(s_step_layer, GColorWhite);
- text_layer_set_background_color(s_step_layer, GColorClear);
- text_layer_set_font(s_step_layer,
- fonts_get_system_font(FONT_KEY_GOTHIC_28));
- text_layer_set_text_alignment(s_step_layer, GTextAlignmentCenter);
- // Adding the childs to the main layer
- layer_add_child(s_window_layer, text_layer_get_layer(s_bg1));
- layer_add_child(s_window_layer, text_layer_get_layer(s_bg2));
- layer_add_child(s_window_layer, text_layer_get_layer(s_date_layer));
- layer_add_child(s_window_layer, text_layer_get_layer(s_time_layer));
- layer_add_child(s_window_layer, text_layer_get_layer(s_step_layer));
- // Subscribe to health events if we can
- if(step_data_is_available()) {
- health_service_events_subscribe(health_handler, NULL);
- }
- }
- static void window_unload(Window *window) {
- layer_destroy(text_layer_get_layer(s_bg1));
- layer_destroy(text_layer_get_layer(s_bg2));
- layer_destroy(text_layer_get_layer(s_date_layer));
- layer_destroy(text_layer_get_layer(s_time_layer));
- layer_destroy(text_layer_get_layer(s_step_layer));
- }
- void init() {
- s_window = window_create();
- s_window_layer = window_get_root_layer(s_window);
- window_set_background_color(s_window, GColorBlack);
- window_set_window_handlers(s_window, (WindowHandlers) {
- .load = window_load,
- .unload = window_unload
- });
- window_stack_push(s_window, true);
- tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
- }
- void deinit() {
- window_destroy(s_window);
- }
- int main() {
- init();
- app_event_loop();
- deinit();
- }
Advertisement
Add Comment
Please, Sign In to add comment