-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindigo.c
248 lines (206 loc) · 7.26 KB
/
indigo.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
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"
#include "http.h"
/* POST variables */
#define PAGE 1
#define BUTTON 2
#define URL "http://[webserver]/pebble.php"
/* If compiling this for iOS, set ANDROID to be false. */
#define ANDROID false
#if ANDROID
#define MY_UUID { 0x91, 0x41, 0xB6, 0x28, 0xBC, 0x89, 0x49, 0x8E, 0xB1, 0x47, 0x10, 0x34, 0xBF, 0xBE, 0x12, 0x98 }
#else
#define MY_UUID HTTP_UUID
#endif
#define HTTP_COOKIE 4887
PBL_APP_INFO(MY_UUID, "Indigo", "Bernie Nazari", 1, 0, DEFAULT_MENU_ICON, APP_INFO_STANDARD_APP);
void handle_init(AppContextRef ctx);
void http_success(int32_t request_id, int http_status, DictionaryIterator* received, void* context);
void http_failure(int32_t request_id, int http_status, void* context);
void window_appear(Window* me);
void httpebble_error(int error_code);
Window window;
TextLayer layer_text1;
TextLayer layer_text2;
TextLayer layer_text3;
TextLayer layer_text4;
char current_page[4] = "0";
void up_single_click_handler(ClickRecognizerRef recognizer, Window *window) {
DictionaryIterator* dict;
HTTPResult result = http_out_get(URL, HTTP_COOKIE, &dict);
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
dict_write_cstring(dict, PAGE, current_page);
dict_write_cstring(dict, BUTTON, "1");
result = http_out_send();
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
}
void select_single_click_handler(ClickRecognizerRef recognizer, Window *window) {
DictionaryIterator* dict;
HTTPResult result = http_out_get(URL, HTTP_COOKIE, &dict);
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
dict_write_cstring(dict, PAGE, current_page);
dict_write_cstring(dict, BUTTON, "2");
result = http_out_send();
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
}
void down_single_click_handler(ClickRecognizerRef recognizer, Window *window) {
DictionaryIterator* dict;
HTTPResult result = http_out_get(URL, HTTP_COOKIE, &dict);
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
dict_write_cstring(dict, PAGE, current_page);
dict_write_cstring(dict, BUTTON, "3");
result = http_out_send();
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
}
void click_config_provider(ClickConfig **config, Window *window) {
config[BUTTON_ID_SELECT]->click.handler = (ClickHandler) select_single_click_handler;
config[BUTTON_ID_UP]->click.handler = (ClickHandler) up_single_click_handler;
config[BUTTON_ID_DOWN]->click.handler = (ClickHandler) down_single_click_handler;
}
void pbl_main(void *params) {
PebbleAppHandlers handlers = {
.init_handler = &handle_init,
.messaging_info = {
.buffer_sizes = {
.inbound = 256,
.outbound = 256,
}
}
};
app_event_loop(params, &handlers);
}
void http_success(int32_t request_id, int http_status, DictionaryIterator* received, void* context) {
if (request_id != HTTP_COOKIE) {
return;
}
Tuple* tuple1 = dict_find(received, 0);
text_layer_set_text(&layer_text1, tuple1->value->cstring);
Tuple* tuple2 = dict_find(received, 1);
text_layer_set_text(&layer_text2, tuple2->value->cstring);
Tuple* tuple3 = dict_find(received, 2);
strcpy(current_page, tuple3->value->cstring);
text_layer_set_text(&layer_text3, "+");
text_layer_set_text(&layer_text4, "_");
}
void http_failure(int32_t request_id, int http_status, void* context) {
httpebble_error(http_status >= 1000 ? http_status - 1000 : http_status);
}
void window_appear(Window* me) {
DictionaryIterator* dict;
HTTPResult result = http_out_get(URL, HTTP_COOKIE, &dict);
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
result = http_out_send();
if (result != HTTP_OK) {
httpebble_error(result);
return;
}
}
void handle_init(AppContextRef ctx) {
http_set_app_id(76782702);
http_register_callbacks((HTTPCallbacks) {
.success = http_success,
.failure = http_failure
}, NULL);
window_init(&window, "Indigo Control");
window_stack_push(&window, true);
window_set_window_handlers(&window, (WindowHandlers){
.appear = window_appear
});
text_layer_init(&layer_text1, GRect(0, 0, 144, 54));
text_layer_set_text_color(&layer_text1, GColorClear);
text_layer_set_background_color(&layer_text1, GColorBlack);
text_layer_set_font(&layer_text1, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
text_layer_set_text_alignment(&layer_text1, GTextAlignmentCenter);
layer_add_child(&window.layer, &layer_text1.layer);
text_layer_init(&layer_text2, GRect(0, 80, 100, 32));
text_layer_set_text_color(&layer_text2, GColorBlack);
text_layer_set_background_color(&layer_text2, GColorClear);
text_layer_set_font(&layer_text2, fonts_get_system_font(FONT_KEY_BITHAM_30_BLACK));
text_layer_set_text_alignment(&layer_text2, GTextAlignmentCenter);
layer_add_child(&window.layer, &layer_text2.layer);
text_layer_init(&layer_text3, GRect(115, 60, 30, 30));
text_layer_set_text_color(&layer_text3, GColorBlack);
text_layer_set_background_color(&layer_text3, GColorClear);
text_layer_set_font(&layer_text3, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text_alignment(&layer_text3, GTextAlignmentCenter);
layer_add_child(&window.layer, &layer_text3.layer);
text_layer_init(&layer_text4, GRect(115, 105, 30, 30));
text_layer_set_text_color(&layer_text4, GColorBlack);
text_layer_set_background_color(&layer_text4, GColorClear);
text_layer_set_font(&layer_text4, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text_alignment(&layer_text4, GTextAlignmentCenter);
window_set_click_config_provider(&window, (ClickConfigProvider) click_config_provider);
}
void httpebble_error(int error_code) {
static char error_message[] = "UNKNOWN_HTTP_ERRROR_CODE_GENERATED";
switch (error_code) {
case HTTP_SEND_TIMEOUT:
strcpy(error_message, "HTTP_SEND_TIMEOUT");
break;
case HTTP_SEND_REJECTED:
strcpy(error_message, "HTTP_SEND_REJECTED");
break;
case HTTP_NOT_CONNECTED:
strcpy(error_message, "HTTP_NOT_CONNECTED");
break;
case HTTP_BRIDGE_NOT_RUNNING:
strcpy(error_message, "HTTP_BRIDGE_NOT_RUNNING");
break;
case HTTP_INVALID_ARGS:
strcpy(error_message, "HTTP_INVALID_ARGS");
break;
case HTTP_BUSY:
strcpy(error_message, "HTTP_BUSY");
break;
case HTTP_BUFFER_OVERFLOW:
strcpy(error_message, "HTTP_BUFFER_OVERFLOW");
break;
case HTTP_ALREADY_RELEASED:
strcpy(error_message, "HTTP_ALREADY_RELEASED");
break;
case HTTP_CALLBACK_ALREADY_REGISTERED:
strcpy(error_message, "HTTP_CALLBACK_ALREADY_REGISTERED");
break;
case HTTP_CALLBACK_NOT_REGISTERED:
strcpy(error_message, "HTTP_CALLBACK_NOT_REGISTERED");
break;
case HTTP_NOT_ENOUGH_STORAGE:
strcpy(error_message, "HTTP_NOT_ENOUGH_STORAGE");
break;
case HTTP_INVALID_DICT_ARGS:
strcpy(error_message, "HTTP_INVALID_DICT_ARGS");
break;
case HTTP_INTERNAL_INCONSISTENCY:
strcpy(error_message, "HTTP_INTERNAL_INCONSISTENCY");
break;
case HTTP_INVALID_BRIDGE_RESPONSE:
strcpy(error_message, "HTTP_INVALID_BRIDGE_RESPONSE");
break;
default: {
strcpy(error_message, "HTTP_ERROR_UNKNOWN");
}
}
text_layer_set_text(&layer_text1, error_message);
}