Repository navigation
Expand file tree
/
Copy pathactivity.c
More file actions
362 lines (301 loc) · 12.7 KB
/
Copy pathactivity.c
File metadata and controls
362 lines (301 loc) · 12.7 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "activity.h"
#include <memory.h>
#include "bitmap.h"
#include "programme.h"
static const uint32_t MARKER_DEGREES = 2;
static const int16_t MARKER_SIZE = 16;
static const int16_t PHASE_HEIGHT = 18;
static const uint32_t TIMER_TIMEOUT_MS = 500;
#ifdef PBL_ROUND
static const int16_t PADDING_SIZE = 30;
static const int16_t RADIAL_WIDTH = 10;
#else
static const int16_t PADDING_SIZE = 5;
static const int16_t RADIAL_WIDTH = 10;
#endif
struct ActivityWindow {
time_t elapsed;
time_t started_at;
bool active;
AppTimer* timer;
const Programme* programme;
Window* window;
Layer* gfx;
TextLayer* phase;
StatusBarLayer* status_bar;
TextLayer* time_remaining;
#ifndef PBL_ROUND
ActionBarLayer* action_bar;
#endif
char phase_buffer[10];
char time_remaining_buffer[24];
ActivityCallbacks callbacks;
};
typedef struct {
const GRect bounds;
GContext* ctx;
const time_t total_duration;
} StateIteratorData;
static GColor state_colour(ProgrammeState state) {
switch (state) {
case PROGRAMME_STATE_WARM_UP:
case PROGRAMME_STATE_WARM_DOWN:
return GColorLightGray;
case PROGRAMME_STATE_WALK:
return GColorDarkGray;
case PROGRAMME_STATE_RUN:
// The other colours in here are fine on the B&W Pebble screens, but the
// green gets turned to white, which isn't really useful. We'll do what we
// did with the number selector and instead use black to highlight.
return COLOR_FALLBACK(GColorBrightGreen, GColorBlack);
default:
LOG_ERROR("unexpected programme state: %d", (int)state);
return GColorRed;
}
}
static int32_t calculate_angle(time_t at, time_t total_duration) {
return (TRIG_MAX_RATIO * at) / total_duration;
}
static void on_state(time_t at,
time_t phase_duration,
ProgrammeState state,
void* userdata) {
StateIteratorData* data = (StateIteratorData*)userdata;
graphics_context_set_fill_color(data->ctx, state_colour(state));
graphics_fill_radial(
data->ctx, data->bounds, GOvalScaleModeFitCircle, RADIAL_WIDTH,
calculate_angle(at, data->total_duration),
calculate_angle(at + phase_duration, data->total_duration));
}
static void on_update_proc(Layer* layer, GContext* ctx) {
ActivityWindow* activity =
(ActivityWindow*)window_get_user_data(layer_get_window(layer));
GRect bounds = layer_get_bounds(layer);
uint32_t marker_angle;
static const uint32_t marker_angle_delta = DEG_TO_TRIGANGLE(MARKER_DEGREES);
StateIteratorData userdata = {
.bounds = bounds,
.ctx = ctx,
.total_duration = programme_duration(activity->programme),
};
// We'll draw the circle that shows the activity in stages by iterating over
// the phases in the programme. Each phase will draw an arc section, and if we
// do the maths correctly, everything will just look like a fancy
// multicoloured circle.
programme_iterate_states(activity->programme, on_state, &userdata);
bounds.size.w += MARKER_SIZE - RADIAL_WIDTH;
bounds.size.h += MARKER_SIZE - RADIAL_WIDTH;
bounds.origin.x -= (MARKER_SIZE - RADIAL_WIDTH) / 2;
bounds.origin.y -= (MARKER_SIZE - RADIAL_WIDTH) / 2;
// Draw the zero point in white, just to make it obvious.
graphics_context_set_fill_color(ctx, GColorWhite);
graphics_fill_radial(ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE,
TRIG_MAX_ANGLE - marker_angle_delta, TRIG_MAX_ANGLE);
graphics_fill_radial(ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE,
DEG_TO_TRIGANGLE(0), DEG_TO_TRIGANGLE(2));
// Calculate and draw the marker showing our actual progression to date.
graphics_context_set_fill_color(ctx, GColorRed);
marker_angle = calculate_angle(activity->elapsed, userdata.total_duration);
// This gets a bit ugly: the radial function can't handle values less than 0
// or greater than TRIG_MAX_ANGLE, so we have to implement our own wrapping.
// Basically, if we're going to cross 0 or TRIG_MAX_ANGLE, we have to draw two
// radials instead, just as we did above for the zero point.
if (marker_angle < marker_angle_delta / 2) {
graphics_fill_radial(
ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE,
(TRIG_MAX_ANGLE + marker_angle) - (marker_angle_delta / 2),
TRIG_MAX_ANGLE);
graphics_fill_radial(ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE, 0,
marker_angle + marker_angle_delta / 2);
} else if (marker_angle > (TRIG_MAX_ANGLE - marker_angle_delta / 2)) {
graphics_fill_radial(ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE,
marker_angle, TRIG_MAX_ANGLE);
graphics_fill_radial(
ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE, 0,
(marker_angle + marker_angle_delta / 2) - TRIG_MAX_ANGLE);
} else {
graphics_fill_radial(ctx, bounds, GOvalScaleModeFitCircle, MARKER_SIZE,
marker_angle - marker_angle_delta / 2,
marker_angle + marker_angle_delta / 2);
}
}
static void update_text_labels(ActivityWindow* activity) {
if (activity->active) {
time_t phase_remaining =
programme_phase_remaining_at(activity->programme, activity->elapsed);
if (phase_remaining / 60 > 9) {
snprintf(activity->time_remaining_buffer,
sizeof(activity->time_remaining_buffer), "%2lld:%02lld",
phase_remaining / 60, phase_remaining % 60);
} else {
snprintf(activity->time_remaining_buffer,
sizeof(activity->time_remaining_buffer), "%1lld:%02lld",
phase_remaining / 60, phase_remaining % 60);
if (phase_remaining == 0) {
vibes_double_pulse();
}
}
activity
->time_remaining_buffer[sizeof(activity->time_remaining_buffer) - 1] =
'\0';
strncpy(activity->phase_buffer,
programme_state_string(
programme_state_at(activity->programme, activity->elapsed)),
sizeof(activity->phase_buffer));
activity->phase_buffer[sizeof(activity->phase_buffer) - 1] = '\0';
}
}
static void on_tick(void* userdata) {
ActivityWindow* activity = (ActivityWindow*)userdata;
activity->elapsed = time(NULL) - activity->started_at;
if (activity->elapsed >= programme_duration(activity->programme)) {
activity_window_set_active(activity, false);
} else {
// We have to chain timers because the TimerService doesn't allow for
// userdata to be provided. This is going to use more battery, but provides
// more encapsulated code.
activity->timer = app_timer_register(TIMER_TIMEOUT_MS, on_tick, activity);
}
layer_mark_dirty(activity->gfx);
update_text_labels(activity);
}
static void on_appear(Window* window) {
ActivityWindow* activity = (ActivityWindow*)window_get_user_data(window);
activity->elapsed = 0;
activity_window_set_active(activity, true);
update_text_labels(activity);
}
static void on_disappear(Window* window) {
ActivityWindow* activity = (ActivityWindow*)window_get_user_data(window);
activity_window_set_active(activity, false);
}
static void on_button_back(ClickRecognizerRef ref, void* ctx) {
ActivityWindow* activity = (ActivityWindow*)ctx;
(activity->callbacks.on_back)(activity->callbacks.userdata);
}
static void on_button_select(ClickRecognizerRef ref, void* ctx) {
ActivityWindow* activity = (ActivityWindow*)ctx;
activity_window_set_active(activity, !activity->active);
layer_mark_dirty(activity->gfx);
update_text_labels(activity);
}
static void click_config_provider(void* ctx) {
window_single_click_subscribe(BUTTON_ID_BACK, on_button_back);
window_single_click_subscribe(BUTTON_ID_SELECT, on_button_select);
}
static void on_load(Window* window) {
ActivityWindow* activity = (ActivityWindow*)window_get_user_data(window);
Layer* root = window_get_root_layer(window);
#ifdef PBL_ROUND
// To centre a circle within the round watch screen, we have to ignore the
// status and action bars, and instead use extra padding to avoid overlaps.
GRect bounds = layer_get_bounds(root);
#else
GRect bounds = calculate_bounds_with_status_action_bars(root);
#endif
#ifndef PBL_ROUND
activity->action_bar = action_bar_layer_create();
action_bar_layer_set_context(activity->action_bar, activity);
action_bar_layer_set_click_config_provider(activity->action_bar,
click_config_provider);
action_bar_layer_add_to_window(activity->action_bar, activity->window);
#else
window_set_click_config_provider_with_context(
activity->window, click_config_provider, activity);
#endif
activity->status_bar = status_bar_layer_create();
layer_add_child(root, status_bar_layer_get_layer(activity->status_bar));
// By working from the outside in, we can incrementally reduce the size of the
// bounds. First up, we'll just move in the padding size for the graphics
// layer on which we'll draw the circular view of the activity.
bounds.size.w -= PADDING_SIZE * 2;
bounds.size.h -= PADDING_SIZE * 2;
bounds.origin.x += PADDING_SIZE;
bounds.origin.y += PADDING_SIZE;
activity->gfx = layer_create(bounds);
layer_set_update_proc(activity->gfx, on_update_proc);
layer_add_child(root, activity->gfx);
// Now we want to constrain the text layers to the inside of the circle. If we
// were doing this properly, there'd be a square root involved somewhere. But
// since not all Pebbles have a FPU and I don't care for pixel precision,
// we'll just put some fudge in and it'll all be fine. Mmmm. Fudge.
const uint16_t circle_radius = bounds.size.w - RADIAL_WIDTH * 5;
bounds.origin.x += bounds.size.w / 2 - circle_radius / 2;
bounds.origin.y += bounds.size.h / 2 - circle_radius / 2;
bounds.size.w = circle_radius;
bounds.size.h = circle_radius;
activity->time_remaining = text_layer_create(bounds);
text_layer_set_text_alignment(activity->time_remaining, GTextAlignmentCenter);
text_layer_set_font(activity->time_remaining,
fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text(activity->time_remaining,
activity->time_remaining_buffer);
layer_add_child(root, text_layer_get_layer(activity->time_remaining));
// Finally, put the phase label below the time remaining.
bounds.origin.y += bounds.size.h - PHASE_HEIGHT;
bounds.size.h = PHASE_HEIGHT;
activity->phase = text_layer_create(bounds);
text_layer_set_text_alignment(activity->phase, GTextAlignmentCenter);
text_layer_set_font(activity->phase,
fonts_get_system_font(FONT_KEY_GOTHIC_14));
text_layer_set_text(activity->phase, activity->phase_buffer);
layer_add_child(root, text_layer_get_layer(activity->phase));
}
static void on_unload(Window* window) {
ActivityWindow* activity = (ActivityWindow*)window_get_user_data(window);
#ifndef PBL_ROUND
action_bar_layer_destroy(activity->action_bar);
#endif
layer_destroy(activity->gfx);
text_layer_destroy(activity->phase);
status_bar_layer_destroy(activity->status_bar);
text_layer_destroy(activity->time_remaining);
}
ActivityWindow* activity_window_create(ActivityCallbacks callbacks) {
ActivityWindow* activity = malloc(sizeof(ActivityWindow));
activity->elapsed = 0;
activity->active = false;
activity->programme = NULL;
activity->callbacks = callbacks;
activity->window = window_create();
window_set_user_data(activity->window, activity);
window_set_window_handlers(activity->window, (WindowHandlers){
.load = on_load,
.unload = on_unload,
.appear = on_appear,
.disappear = on_disappear,
});
return activity;
}
void activity_window_destroy(ActivityWindow* activity) {
window_destroy(activity->window);
free(activity);
}
Window* activity_window_get_window(ActivityWindow* activity) {
return activity->window;
}
void activity_window_set_active(ActivityWindow* activity, bool active) {
if (active) {
#ifndef PBL_ROUND
action_bar_layer_set_icon(activity->action_bar, BUTTON_ID_SELECT,
image_pause);
#endif
activity->active = true;
activity->started_at = time(NULL) - activity->elapsed;
activity->timer = app_timer_register(TIMER_TIMEOUT_MS, on_tick, activity);
} else {
#ifndef PBL_ROUND
action_bar_layer_set_icon(activity->action_bar, BUTTON_ID_SELECT,
image_play);
#endif
activity->active = false;
if (activity->timer) {
app_timer_cancel(activity->timer);
activity->timer = NULL;
}
}
}
void activity_window_set_programme(ActivityWindow* activity,
const Programme* programme) {
activity->programme = programme;
}