summaryrefslogtreecommitdiff
path: root/watchapp/src/watchapp.c
blob: 63e693f7960bbd458ebe79e822a9aca8283b8800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <pebble.h>
#include "mainmenu.h"
#include "actimon.h"

static Window *window;
static TextLayer *date_layer;
static TextLayer *time_layer;
static Layer *line_layer;
static TextLayer *step_layer;

static void draw_line(Layer *layer, GContext *ctx)
{
	graphics_context_set_stroke_color(ctx, GColorWhite);
	graphics_draw_line(ctx, GPoint(0, 0), GPoint(144-8, 0));
	graphics_draw_line(ctx, GPoint(0, 1), GPoint(144-8, 1));
}

static void handle_second_tick(struct tm* tick_time, TimeUnits units_changed)
{
	static char time_text[] = "00:00";
	static char date_text[] = "September 30    ";
	static char step_text[] = "999999 steps";
	strftime(time_text, sizeof(time_text), "%k:%M", tick_time);
	strftime(date_text, sizeof(date_text), "%B %d", tick_time);
	if (monitor_mode == mmSteps)
		snprintf(step_text, sizeof(step_text), "%lu steps", step_total_this_period);
	else
		snprintf(step_text, sizeof(step_text), "Sleeping");
	text_layer_set_text(time_layer, time_text);
	text_layer_set_text(date_layer, date_text);
	text_layer_set_text(step_layer, step_text);
}

static void select_click_handler(ClickRecognizerRef recognizer, void *context)
{
	push_menu();
}

static void up_click_handler(ClickRecognizerRef recognizer, void *context)
{
}

static void down_click_handler(ClickRecognizerRef recognizer, void *context)
{
}

static void back_click_handler(ClickRecognizerRef recognizer, void *context)
{
}

static void click_config_provider(void *context)
{
  window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
  window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
  window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
  window_single_click_subscribe(BUTTON_ID_BACK, back_click_handler);
}

static void window_load(Window *window)
{
	window_set_fullscreen(window, true);
	window_set_background_color(window, GColorBlack);
	GRect full = layer_get_bounds(window_get_root_layer(window));
	full.size.h += 16; /* Account for status bar no longer shown */
	layer_set_bounds(window_get_root_layer(window), full);
	// Init the text layer used to show the time
	time_layer = text_layer_create(GRect(4, 168-49-8, 144-4 /* width */, 49 /* height */));
	text_layer_set_text_color(time_layer, GColorWhite);
	text_layer_set_background_color(time_layer, GColorClear);
	text_layer_set_font(time_layer, fonts_get_system_font(FONT_KEY_ROBOTO_BOLD_SUBSET_49));
	
	// Init the text layer used to show the date
	date_layer = text_layer_create(GRect(4, 168-49-28-4, 144-4, 28));
	text_layer_set_text_color(date_layer, GColorWhite);
	text_layer_set_background_color(date_layer, GColorClear);
	text_layer_set_font(date_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
	
	// Init the text layer used to show steps
	step_layer = text_layer_create(GRect(4, 56, 144-4, 28));
	text_layer_set_text_color(step_layer, GColorWhite);
	text_layer_set_background_color(step_layer, GColorClear);
	text_layer_set_font(date_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
	
	// Ensures time is displayed immediately (will break if NULL tick event accessed).
	// (This is why it's a good idea to have a separate routine to do the update itself.)
	time_t now = time(NULL);
	struct tm *current_time = localtime(&now);
	handle_second_tick(current_time, SECOND_UNIT);
	tick_timer_service_subscribe(SECOND_UNIT, &handle_second_tick);
	
	// Finally create a simple white line
	line_layer = layer_create(GRect(4, 168-49-1, 144-8, 2));
	layer_set_update_proc(line_layer, draw_line);
	
	// And stuff the layers into the window
	layer_add_child(window_get_root_layer(window), text_layer_get_layer(time_layer));
	layer_add_child(window_get_root_layer(window), text_layer_get_layer(date_layer));
	layer_add_child(window_get_root_layer(window), text_layer_get_layer(step_layer));
	layer_add_child(window_get_root_layer(window), line_layer);
}

static void window_unload(Window *window)
{
	tick_timer_service_unsubscribe();
	text_layer_destroy(time_layer);
	text_layer_destroy(date_layer);
	text_layer_destroy(step_layer);
	layer_destroy(line_layer);
}

static void init(void)
{
  window = window_create();
  window_set_click_config_provider(window, click_config_provider);
  window_set_window_handlers(window, (WindowHandlers) {
    .load = window_load,
    .unload = window_unload,
  });
  const bool animated = true;
  window_stack_push(window, animated);
}

static void deinit(void)
{
  window_destroy(window);
}

int main(void)
{
  init();
  create_menu();
  init_activity_monitor();
  APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window);
  app_event_loop();
  fini_activity_monitor();
  destroy_menu();
  deinit();
}