/
watchapp.c
427 lines (359 loc) · 14.1 KB
/
watchapp.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <pebble.h>
#define NUM_MENU_SECTIONS 1
#define NUM_FIRST_MENU_ITEMS 3
static Window *window;
static TextLayer *hello_layer;
static char msg[100]; // message to send to phone
static bool celsius = true;
static AppTimer *timer; // used for polling for current temperature
static int refresh_time = 1000; // frequency of polling updates (milliseconds)
static SimpleMenuLayer *s_simple_menu_layer;
static SimpleMenuSection s_menu_sections[NUM_MENU_SECTIONS];
static SimpleMenuItem s_first_menu_items[NUM_FIRST_MENU_ITEMS];
/* Called when a message is successfully transmitted to phone */
void out_sent_handler(DictionaryIterator *sent, void *context) {
// outgoing message was delivered -- do nothing
}
/* Called when a message fails to make it to the phone */
void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) {
text_layer_set_text(hello_layer, "Error out!");
}
int our_isdigit(char c) {
return c >= '0' && c <= '9';
}
/** Converts a string of characters to a double.
* This method was adapted from its original version, which appears online at
* https://code.google.com/p/unladen-swallow/source/browse/branches/release-2009Q1-maint/Python/atof.c
*/
double our_atof(char *s) {
double a = 0.0;
int e = 0;
int c;
int negative = 0;
if (*s == '-') {
negative = 1;
s++;
}
while ((c = *s++) != '\0' && our_isdigit(c)) {
a = a*10.0 + (c - '0');
}
if (c == '.') {
while ((c = *s++) != '\0' && our_isdigit(c)) {
a = a*10.0 + (c - '0');
e = e-1;
}
}
if (c == 'e' || c == 'E') {
int sign = 1;
int i = 0;
c = *s++;
if (c == '+')
c = *s++;
else if (c == '-') {
c = *s++;
sign = -1;
}
while (our_isdigit(c)) {
i = i*10 + (c - '0');
c = *s++;
}
e += i*sign;
}
while (e > 0) {
a *= 10.0;
e--;
}
while (e < 0) {
a *= 0.1;
e++;
}
if (negative) a = -1 * a;
return a;
}
int our_pow(int base, int power) {
int result = 1;
while (power > 0) {
result = result * base;
power--;
}
return result;
}
/* Converts an input double to an array of chars, stored in the input buffer */
void double_to_char_array(double num, char* buffer) {
int dig_left = 0;
// Determine how many digits there are to the left of the decimal point
double num_cpy = num;
while (num_cpy >= 1) {
num_cpy = num_cpy / 10;
dig_left++;
}
// Copy all digits to the left of the decimal place one by one
int num_left_cpy = (int) num;
int digit = 1;
while (digit <= dig_left) {
int ten_pow = our_pow(10, dig_left - digit);
int this_dig = num_left_cpy / ten_pow;
buffer[digit - 1] = this_dig + '0';
num_left_cpy = num_left_cpy % ten_pow;
digit++;
}
buffer[digit - 1] = '.';
digit++;
// Copy all digits to the right of the decimal place
double num_right_cpy = num - ((int) num);
while (digit < 7) {
num_right_cpy = num_right_cpy * 10;
int this_dig = ((int) num_right_cpy) % 10;
buffer[digit - 1] = this_dig + '0';
digit++;
}
}
/* Converts an integer to an array of chars and stores it in the input buffer */
/*void int_to_char_array(int num, char* buffer) {
int num_cpy = num;
// Determine number of digits
int num_digits = 0;
while (num_cpy >= 1) {
num_cpy = num_cpy / 10;
num_digits++;
}
// Copy the digits one by one into the buffer
num_cpy = num;
int digit = 1;
while (digit <= num_digits) {
int ten_pow = our_pow(10, num_digits - digit);
int this_dig = num_cpy / ten_pow;
buffer[digit - 1] = this_dig + '0';
num_cpy = num_cpy % ten_pow;
digit++;
}
// Null-terminate the char array
buffer[digit - 1] = '\0';
}*/
/* Converts a char array with a celsius temperature into a char array with a fahrenheit temperature */
void convert_to_f(char* cel_str) {
char cel_str_copy[7];
strncpy(cel_str_copy, cel_str, 6);
cel_str_copy[6] = '\0';
double cel_temp = our_atof(cel_str_copy);
double f_temp = cel_temp * (9.0 / 5.0) + 32; // Celsius to Fahrenheit conversion
char f_str[100];
double_to_char_array(f_temp, f_str);
for (int i = 0; i < 6; i++) {
cel_str[i] = f_str[i];
}
}
void in_received_handler(DictionaryIterator *received, void *context) {
// Looks for key #0 (current temperature) in the incoming message
Tuple *text_tuple = dict_find(received, 0);
if (text_tuple) {
strncpy(msg, text_tuple->value->cstring, 6);
msg[6] = '\0';
if (!celsius) {
convert_to_f(msg);
strcat(msg, " F");
} else {
strcat(msg, " C");
}
text_layer_set_text(hello_layer, msg);
return;
}
// Looks for key #1 (statistics) in the incoming message
text_tuple = dict_find(received, 1);
if (text_tuple) {
char* p = text_tuple->value->cstring;
strcpy(msg, "Min: ");
if (!celsius) {
convert_to_f(p);
strncat(msg, p, 6);
strcat(msg, " F\nMax: ");
} else {
strncat(msg, p, 6);
strcat(msg, " C\nMax: ");
}
while (*p != ',') {p++;}
p++;
if (!celsius) {
convert_to_f(p);
strncat(msg, p, 6);
strcat(msg, " F\nAvg: ");
} else {
strncat(msg, p, 6);
strcat(msg, " C\nAvg: ");
}
while (*p != ',') {p++;}
p++;
if (!celsius) {
convert_to_f(p);
strncat(msg, p, 6);
strcat(msg, " F\n");
} else {
strncat(msg, p, 6);
strcat(msg, " C\n");
}
text_layer_set_text(hello_layer, msg);
return;
}
// Looks for key #13 (unable to connect to serve) in the incoming message
text_tuple = dict_find(received, 13);
if (text_tuple) {
strcpy(msg, "Unable to connect to server");
text_layer_set_text(hello_layer, msg);
return;
}
text_tuple = dict_find(received, 3);
if (text_tuple) {
strcpy(msg, "Server can't connect to the Arduino");
text_layer_set_text(hello_layer, msg);
return;
}
// Otherwise: couldn't identify message key
strcpy(msg, "Error - unidentified key!");
text_layer_set_text(hello_layer, msg);
}
/* Called when the incoming message is dropped */
void in_dropped_handler(AppMessageResult reason, void *context) {
// incoming message dropped
text_layer_set_text(hello_layer, "Error receiving message.");
}
void refresh_temp_click_handler(ClickRecognizerRef recognizer, void* context) {
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
// sends a request for the current temperature, using key #0
Tuplet value = TupletCString(0, "hello?");
dict_write_tuplet(iter, &value);
app_message_outbox_send();
// Reschedule the timer to poll for the current temp in 1 second
app_timer_reschedule(timer, refresh_time);
}
/* Called when the timer's time elapses - polling for current temperature */
static void timer_callback(void *data) {
refresh_temp_click_handler(NULL, NULL);
timer = app_timer_register(refresh_time, timer_callback, NULL);
}
/* Handles the user clicking the select button to refresh the statistics */
void refresh_stats_click_handler(ClickRecognizerRef recognizer, void* context) {
text_layer_set_text(hello_layer, "Getting data!");
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
// sends a request for the statistics, using key #1
Tuplet value = TupletCString(1, "hello?");
dict_write_tuplet(iter, &value);
app_message_outbox_send();
}
/* Sends a request to change the units between celsius and fahrenheit */
void send_change_units_message() {
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
if (celsius) {
Tuplet value = TupletCString(4, "Use celsius");
dict_write_tuplet(iter, &value);
} else {
Tuplet value = TupletCString(5, "Use Fahrenheit");
dict_write_tuplet(iter, &value);
}
app_message_outbox_send();
}
/* Handles the user pressing the back button (returns the user to the main menu) */
void back_handler(ClickRecognizerRef recognizer, void* context) {
app_timer_cancel(timer); // Stop polling for the current temperature
window_stack_pop(true);
window = window_stack_get_top_window();
}
/* this registers the appropriate function to the appropriate button (current temperature) */
void config_provider_current_temp(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, refresh_temp_click_handler);
window_single_click_subscribe(BUTTON_ID_BACK, back_handler);
}
/* this registers the appropriate function to the appropriate button (statistics)*/
void config_provider_statistics(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, refresh_stats_click_handler);
window_single_click_subscribe(BUTTON_ID_BACK, back_handler);
}
/* Called when statistics is clicked */
static void statistics_callback(int index, void *ctx) {
window = window_create();
layer_add_child(window_get_root_layer(window), text_layer_get_layer(hello_layer));
window_stack_push(window, true);
window_set_click_config_provider(window, config_provider_statistics);
refresh_stats_click_handler(NULL, NULL); // Refresh/initiate stats
}
/* This is called when current temp is selected */
static void current_temp_callback(int index, void *ctx) {
window = window_create();
layer_add_child(window_get_root_layer(window), text_layer_get_layer(hello_layer));
window_stack_push(window, true);
window_set_click_config_provider(window, config_provider_current_temp);
text_layer_set_text(hello_layer, "Retrieving Information...");
refresh_temp_click_handler(NULL, NULL); // Refresh/initiate current temperature
timer = app_timer_register(3000, timer_callback, NULL); // Initiate polling
}
/* This is called when change units is selected */
static void units_callback(int index, void *ctx) {
if (celsius) {
s_first_menu_items[2].subtitle = "Fahrenheit";
celsius = false;
} else {
s_first_menu_items[2].subtitle = "Celsius";
celsius = true;
}
send_change_units_message();
layer_mark_dirty(simple_menu_layer_get_layer(s_simple_menu_layer));
}
/* Create the main menu window */
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_frame(window_layer);
s_first_menu_items[0] = (SimpleMenuItem) {
.title = "Current Temperature",
.callback = current_temp_callback,
};
s_first_menu_items[1] = (SimpleMenuItem) {
.title = "Statistics",
.callback = statistics_callback,
};
s_first_menu_items[2] = (SimpleMenuItem) {
.title = "Change Units",
.subtitle = "Celsius",
.callback = units_callback,
};
s_menu_sections[0] = (SimpleMenuSection) {
.num_items = NUM_FIRST_MENU_ITEMS,
.items = s_first_menu_items,
};
s_simple_menu_layer = simple_menu_layer_create(bounds, window, s_menu_sections, NUM_MENU_SECTIONS, NULL);
hello_layer = text_layer_create((GRect) { .origin = { 0, 35 }, .size = { bounds.size.w, 120 } });
text_layer_set_text_alignment(hello_layer, GTextAlignmentCenter);
layer_add_child(window_layer, simple_menu_layer_get_layer(s_simple_menu_layer));
}
/* Called when the app is exited */
static void window_unload(Window *window) {
text_layer_destroy(hello_layer);
simple_menu_layer_destroy(s_simple_menu_layer);
}
static void init(void) {
window = window_create();
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
// for registering AppMessage handlers
app_message_register_inbox_received(in_received_handler);
app_message_register_inbox_dropped(in_dropped_handler);
app_message_register_outbox_sent(out_sent_handler);
app_message_register_outbox_failed(out_failed_handler);
const uint32_t inbound_size = 64;
const uint32_t outbound_size = 64;
app_message_open(inbound_size, outbound_size);
const bool animated = true;
window_stack_push(window, animated);
}
/* De-initializes the main menu window */
static void deinit(void) {
window_destroy(window);
}
int main(void) {
init();
app_event_loop();
deinit();
}