static void handle_minute_tick(struct tm* tick_time, TimeUnits units_changed) { // Display Time static char s_time_text[] = "00:00"; if (hours_24) { strftime(s_time_text, sizeof(s_time_text), "%H:%M", tick_time); } else { strftime(s_time_text, sizeof(s_time_text), "%H:%p", tick_time); } text_layer_set_text(s_time_layer, s_time_text); // Display Date static char s_date_text[] = "Jan 10"; strftime(s_date_text, sizeof(s_date_text), "%b %d", tick_time); text_layer_set_text(s_date_layer, s_date_text); // Display Steps static char steps_text[] = "00000 steps"; HealthMetric metric = HealthMetricStepCount; time_t start = time_start_of_today(); time_t end = time(NULL); HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask & HealthServiceAccessibilityMaskAvailable) { snprintf(steps_text, sizeof(steps_text), "%d steps", (int)health_service_sum_today(metric)); } else { snprintf(steps_text, sizeof(steps_text), "%d steps", 0); } text_layer_set_text(s_steps_layer, steps_text); }
int health_get_metric_sum(HealthMetric metric) { HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, time_start_of_today(), time(NULL)); if (mask == HealthServiceAccessibilityMaskAvailable) { return (int) health_service_sum_today(metric); } else { LOG_DEBUG("Data unavailable!"); return 0; } }
static void update_time() { // Get a tm structure time_t temp = time(NULL); ts=*localtime(&temp); minutes=ts.tm_min; layer_mark_dirty(s_canvas_layer); hours=ts.tm_hour; // Write the current hours and minutes into a buffer static char s_buffer[6]; if(minutes<10) { snprintf(s_buffer,6, "%d:0%d", hours,minutes); } else { snprintf(s_buffer,6, "%d:%d", hours,minutes); } // Display this time on the TextLayer text_layer_set_text(s_time_layer, s_buffer); #if defined(PBL_HEALTH) // Use the step count metric HealthMetric metric = HealthMetricStepCount; // Create timestamps for midnight (the start time) and now (the end time) time_t start = time_start_of_today(); time_t end = time(NULL); // Check step data is available HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); bool any_data_available = mask & HealthServiceAccessibilityMaskAvailable; //steps= (int)health_service_sum_today(metric); steps+=600; printf("%d",steps); #else // Health data is not available here bool any_data_available = false; #endif /* minutes=(minutes+13)%60; layer_mark_dirty(s_canvas_layer); static char s_buffer[3]; snprintf(s_buffer,3, "%d", minutes); // Display this time on the TextLayer text_layer_set_text(s_time_layer, s_buffer); */ }
void getHealthData() { // Use the sleep count metric (sleep seconds) HealthMetric metric = HealthMetricSleepSeconds; // Create timestamps for now (the end time) and midnight (the start time) time_t end = time(NULL); time_t start = time_start_of_today(); // Check the metric has data available for today HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask == HealthServiceAccessibilityMaskAvailable) { // Data is available! APP_LOG(APP_LOG_LEVEL_INFO, "Sleep seconds today: %d", (int)health_service_sum_today(metric)); today.sleepSeconds = (int)health_service_sum_today(metric); } else { // No data recorded yet today APP_LOG(APP_LOG_LEVEL_ERROR, "Data unavailable!"); } metric = HealthMetricStepCount; mask = health_service_metric_accessible(metric, start, end); if(mask == HealthServiceAccessibilityMaskAvailable) { // Data is available! APP_LOG(APP_LOG_LEVEL_INFO, "Step count today: %d", (int)health_service_sum_today(metric)); today.steps = (int)health_service_sum_today(metric); } else { // No data recorded yet today APP_LOG(APP_LOG_LEVEL_ERROR, "Data unavailable!"); } }
//Health static void update_health() { HealthMetric metric = HealthMetricStepCount; time_t start = time_start_of_today(); time_t end = time(NULL); // Check the metric has data available for today HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask & HealthServiceAccessibilityMaskAvailable) { // Data is available! } else { // No data recorded yet today } }
static void update_step_count() { HealthMetric metric = HealthMetricStepCount; time_t start = time_start_of_today(); time_t end = time(NULL); HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if (mask & HealthServiceAccessibilityMaskAvailable) { int step_count = (int) health_service_sum_today(metric); static char buf[16]; snprintf(buf, sizeof(buf), "\U0001F49C %d", step_count); text_layer_set_text(s_steps_layer, buf); } else { APP_LOG(APP_LOG_LEVEL_ERROR, "Data unavailable"); } }
static int getTotalStepsToday() { HealthMetric metric = HealthMetricStepCount; time_t end = time(NULL); time_t start = time_start_of_today(); // Check the metric has data available for today HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask & HealthServiceAccessibilityMaskAvailable) { int totalSteps = (int)health_service_sum_today(metric); return totalSteps; } else { return 0; } }
static int getSleepSeconds() { HealthMetric metric = HealthMetricSleepSeconds; time_t start = time_start_of_today(); time_t end = time(NULL); // Check the metric has data available for today HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask & HealthServiceAccessibilityMaskAvailable) { // Data is available! int sleeps = (int)health_service_sum_today(metric); return sleeps; } else { // No data recorded yet today APP_LOG(APP_LOG_LEVEL_ERROR, "Data unavailable!"); return 0; } }
static void menu_select_callback(MenuLayer *menu_layer, MenuIndex *cell_index, void *data) { // step_goal = cell_index->row; setStepGoal(cell_index->row); persist_write_int(GOAL, cell_index->row); AppWorkerMessage packet = { .data0 = cell_index->row }; app_worker_send_message(GOAL, &packet); menu_layer_reload_data(main_menu); layer_mark_dirty(info_bar); } static int get_step_count() { HealthServiceAccessibilityMask request = health_service_metric_accessible(HealthMetricStepCount, time_start_of_today(), time(NULL)); if(request == HealthServiceAccessibilityMaskAvailable) { return (int)health_service_sum_today(HealthMetricStepCount); } else { return 0; } }
static void update_step_counter() { HealthMetric metric = HealthMetricStepCount; time_t start = time_start_of_today(); time_t end = time(NULL); // Check the metric has data available for today HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask & HealthServiceAccessibilityMaskAvailable) { static char steps_count_char[] = "00000000000"; snprintf(steps_count_char, sizeof(steps_count_char), "%d", (int)health_service_sum_today(metric)); // Data is available! APP_LOG(APP_LOG_LEVEL_INFO, "Steps today: %d", (int)health_service_sum_today(metric)); APP_LOG(APP_LOG_LEVEL_INFO, steps_count_char); text_layer_set_text(s_step_count_layer, steps_count_char); } else { // No data recorded yet today APP_LOG(APP_LOG_LEVEL_ERROR, "Data unavailable!"); } }
uint32_t health_sum_timeframe(HealthMetric metric, time_t time_start, time_t time_end) { APP_LOG(APP_LOG_LEVEL_INFO, "Heap: %i", heap_bytes_free()); uint32_t output = 0; APP_LOG(APP_LOG_LEVEL_INFO, "Metric: %i", metric); if (health_service_metric_accessible(metric, time_start, time_end) == HealthServiceAccessibilityMaskAvailable) { HealthMinuteData * minute_data = malloc(sizeof(HealthMinuteData) * UPDATE_INTERVAL); time_t time_start_calc = time_start; while (time_start_calc < time_end && time_start_calc < time(NULL)) { time_t time_start_temp = time_start_calc; time_t time_end_temp = time_start_calc + UPDATE_INTERVAL*60; if (time_end_temp > time_end) { time_end_temp = time_end; } if (time_end_temp > time(NULL)) { time_end_temp = time(NULL); } APP_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "start %lu", time_start_temp); APP_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "end %lu", time_end_temp); uint32_t minutes_gotten = health_service_get_minute_history(minute_data, UPDATE_INTERVAL, &time_start_temp, &time_end_temp); APP_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "-> start %lu", time_start_temp); APP_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "-> end %lu", time_end_temp); APP_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "mins gotten %lu", minutes_gotten); for (uint32_t i = 0; i < minutes_gotten; i++) { if (!minute_data[i].is_invalid) { output += minute_data[i].steps; } } APP_LOG(APP_LOG_LEVEL_DEBUG_VERBOSE, "output now %lu", output); time_start_calc += UPDATE_INTERVAL*60; } APP_LOG(APP_LOG_LEVEL_DEBUG, "OUTPUT: %lu", output); free(minute_data); } return output; }
void health_handler(HealthEventType event, void *context) { // Which type of event occured? switch(event) { case HealthEventSignificantUpdate: APP_LOG(APP_LOG_LEVEL_INFO, "New HealthService HealthEventSignificantUpdate event"); // break; //fallthrough case HealthEventMovementUpdate: APP_LOG(APP_LOG_LEVEL_INFO, "New HealthService HealthEventMovementUpdate event"); HealthMetric metric = HealthMetricStepCount; time_t start = time_start_of_today(); time_t end = time(NULL); // Check the metric has data available for today HealthServiceAccessibilityMask mask = health_service_metric_accessible(metric, start, end); if(mask & HealthServiceAccessibilityMaskAvailable) { // Data is available! APP_LOG(APP_LOG_LEVEL_INFO, "Steps today: %d", (int)health_service_sum_today(metric)); health.steps = (int)health_service_sum_today(metric); } else { // No data recorded yet today APP_LOG(APP_LOG_LEVEL_ERROR, "Data unavailable!"); } break; case HealthEventSleepUpdate: APP_LOG(APP_LOG_LEVEL_INFO, "New HealthService HealthEventSleepUpdate event"); break; } }
static void canvas_update_proc(Layer *this_layer, GContext *ctx) { GRect bounds = layer_get_bounds(this_layer); uint16_t ms_start = time_ms(NULL, NULL); graphics_context_set_antialiased(ctx, true); if (!isBtConnected) { graphics_context_set_fill_color(ctx, GColorDukeBlue); graphics_fill_rect(ctx, bounds, 0, GCornerNone); } else { graphics_context_set_fill_color(ctx, GColorBlack); graphics_fill_rect(ctx, bounds, 0, GCornerNone); } graphics_context_set_fill_color(ctx, GColorBlack); uint16_t ms_fill = time_ms(NULL, NULL); graphics_context_set_stroke_color(ctx, COLOR_FALLBACK(GColorRed, GColorWhite)); #if defined(PBL_PLATFORM_APLITE) || defined(PBL_PLATFORM_DIORITE) graphics_context_set_stroke_width(ctx, 1); #else graphics_context_set_stroke_width(ctx, 3); #endif graphics_draw_line(ctx, GPoint(bounds.size.w/2, 0), GPoint(bounds.size.w/2, bounds.size.h)); graphics_context_set_stroke_color(ctx, GColorWhite); int hour_loc = bounds.size.h/2; if(ctick_time->tm_min < 20) draw_hour(this_layer, ctx, -1, tz2, hour_loc, FONT_KEY_ROBOTO_BOLD_SUBSET_49, 49); draw_hour(this_layer, ctx, 0, tz2, hour_loc, FONT_KEY_ROBOTO_BOLD_SUBSET_49, 49); draw_hour(this_layer, ctx, 1, tz2, hour_loc, FONT_KEY_ROBOTO_BOLD_SUBSET_49, 49); if(ctick_time->tm_min > 50) draw_hour(this_layer, ctx, 2, tz2, hour_loc, FONT_KEY_ROBOTO_BOLD_SUBSET_49, 49); graphics_context_set_stroke_width(ctx, 1); uint16_t ms_hour = time_ms(NULL, NULL); draw_minutes(this_layer, ctx, bounds.size.h/2+49/2+5); uint16_t ms_day = time_ms(NULL, NULL); int day_loc = bounds.size.h/2-49/2-12; draw_day(this_layer, ctx, -1, day_loc); draw_day(this_layer, ctx, 0, day_loc); draw_day(this_layer, ctx, 1, day_loc); draw_day(this_layer, ctx, 2, day_loc); draw_day_ticks(this_layer, ctx, bounds.size.h/2-49/2-12); if (temp != INVALID_TEMP) { draw_temp(this_layer, ctx, bounds.size.h/2+75); } getWeather(); getForecast(); uint16_t ms_end = time_ms(NULL, NULL); draw_bat(this_layer, ctx, 0); #if defined(PBL_HEALTH) // Check step data is available HealthServiceAccessibilityMask mask = health_service_metric_accessible(HealthMetricStepCount, time_start_of_today(), cur_time); if(mask & HealthServiceAccessibilityMaskAvailable) { // Data is available! int total_steps = (int)health_service_sum_today(HealthMetricStepCount); draw_step(this_layer, ctx, 22, total_steps); } time_t end_time = time(NULL); time_t start_time = end_time - 600; HealthServiceAccessibilityMask hr = health_service_metric_accessible(HealthMetricHeartRateBPM, start_time, end_time); if (hr & HealthServiceAccessibilityMaskAvailable) { uint32_t bpm = health_service_peek_current_value(HealthMetricHeartRateBPM); APP_LOG(APP_LOG_LEVEL_INFO, "HR: %d", (int)bpm); draw_bpm(this_layer, ctx, 9, bpm); } #endif static int repaints = 0; ++repaints; ms_fill = ms_fill < ms_start ? ms_fill + 1000 : ms_fill; ms_hour = ms_hour < ms_start ? ms_hour + 1000 : ms_hour; ms_day = ms_day < ms_start ? ms_day + 1000 : ms_day; ms_end = ms_end < ms_start ? ms_end + 1000 : ms_end; static uint16_t tt_max = 0; if ((ms_end-ms_start) > tt_max) tt_max = ms_end-ms_start; }
// Is step data available? bool step_data_is_available() { return HealthServiceAccessibilityMaskAvailable & health_service_metric_accessible(HealthMetricStepCount, time_start_of_today(), time(NULL)); }