summaryrefslogtreecommitdiff
path: root/watchapp/src/watchapp.c
blob: 7fc40f4a25cab4599e15146838b89280a412c046 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#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 ResourceId current_battery_id = INVALID_RESOURCE;
static GBitmap *current_battery_image;
static BitmapLayer *battery_layer;

static void handle_battery_update(BatteryChargeState charge)
{
	ResourceId target_battery_id = INVALID_RESOURCE;
	GBitmap *new_image;
	if (charge.is_charging)
		target_battery_id = RESOURCE_ID_ICON_BATTERY_CHARGING;
	else if (charge.is_plugged)
		target_battery_id = RESOURCE_ID_ICON_BATTERY_CHARGED;
	else if (charge.charge_percent > 70)
		target_battery_id = RESOURCE_ID_ICON_BATTERY_FULL;
	else if (charge.charge_percent > 20)
		target_battery_id = RESOURCE_ID_ICON_BATTERY_MID;
	else
		target_battery_id = RESOURCE_ID_ICON_BATTERY_CRITICAL;
	if (current_battery_id == target_battery_id)
		return;
	new_image = gbitmap_create_with_resource(target_battery_id);
	bitmap_layer_set_bitmap(battery_layer, new_image);
	if (current_battery_id != INVALID_RESOURCE) {
		gbitmap_destroy(current_battery_image);
	}
	current_battery_image = new_image;
	current_battery_id = target_battery_id;
}

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";
	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", step_total_this_period);
	else
		snprintf(step_text, sizeof(step_text), "ZzZz");
	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(16, 0, 65, 16));
	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);
	
	// Now prepare the status icon layers
	battery_layer = bitmap_layer_create(GRect(120,0,24,16));
	bitmap_layer_set_background_color(battery_layer, GColorClear);
	handle_battery_update(battery_state_service_peek());
	battery_state_service_subscribe(handle_battery_update);

	// 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);
	layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(battery_layer));
}

static void window_unload(Window *window)
{
	battery_state_service_unsubscribe();
	tick_timer_service_unsubscribe();
	text_layer_destroy(time_layer);
	text_layer_destroy(date_layer);
	text_layer_destroy(step_layer);
	bitmap_layer_destroy(battery_layer);
	if (current_battery_id != INVALID_RESOURCE)
		gbitmap_destroy(current_battery_image);
	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();
}