-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathobsidian.c
executable file
·416 lines (371 loc) · 14.4 KB
/
obsidian.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
// Copyright 2015-16 Stefan Heule
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "obsidian.h"
////////////////////////////////////////////
//// Default values for the configuration
////////////////////////////////////////////
// defaults are also in src/obsidian.c, src/js/pebble-js-app.js and config/js/preview.js
uint8_t config_color_outer_background = COLOR_FALLBACK(GColorDarkGrayARGB8, GColorBlackARGB8);
uint8_t config_color_inner_background = COLOR_FALLBACK(GColorWhiteARGB8, GColorWhiteARGB8);
uint8_t config_color_minute_hand = COLOR_FALLBACK(GColorBlackARGB8, GColorBlackARGB8);
uint8_t config_color_inner_minute_hand = COLOR_FALLBACK(GColorLightGrayARGB8, GColorBlackARGB8);
uint8_t config_color_hour_hand = COLOR_FALLBACK(GColorJaegerGreenARGB8, GColorBlackARGB8);
uint8_t config_color_inner_hour_hand = COLOR_FALLBACK(GColorLightGrayARGB8, GColorBlackARGB8);
uint8_t config_color_circle = COLOR_FALLBACK(GColorBlackARGB8, GColorBlackARGB8);
uint8_t config_color_ticks = COLOR_FALLBACK(GColorBlackARGB8, GColorBlackARGB8);
uint8_t config_color_day_of_week = COLOR_FALLBACK(GColorJaegerGreenARGB8, GColorBlackARGB8);
uint8_t config_color_date = COLOR_FALLBACK(GColorBlackARGB8, GColorBlackARGB8);
uint8_t config_battery_logo = 1;
uint8_t config_color_battery_logo = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorDarkGrayARGB8, GColorBlackARGB8),
GColorWhiteARGB8);
uint8_t config_color_battery_30 = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorYellowARGB8, GColorBlackARGB8),
GColorWhiteARGB8);
uint8_t config_color_battery_20 = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorOrangeARGB8, GColorBlackARGB8),
GColorWhiteARGB8);
uint8_t config_color_battery_10 = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorRedARGB8, GColorBlackARGB8),
GColorWhiteARGB8);
uint8_t config_color_battery_bg_30 = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorWhiteARGB8, GColorYellowARGB8),
GColorBlackARGB8);
uint8_t config_color_battery_bg_20 = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorWhiteARGB8, GColorOrangeARGB8),
GColorBlackARGB8);
uint8_t config_color_battery_bg_10 = COLOR_FALLBACK(PBL_IF_ROUND_ELSE(GColorWhiteARGB8, GColorRedARGB8),
GColorBlackARGB8);
uint8_t config_color_bluetooth_logo = COLOR_FALLBACK(GColorWhiteARGB8, GColorBlackARGB8);
uint8_t config_color_bluetooth_logo_2 = COLOR_FALLBACK(GColorBlackARGB8, GColorWhiteARGB8);
uint8_t config_bluetooth_logo = true;
uint8_t config_vibrate_disconnect = true;
uint8_t config_vibrate_reconnect = true;
uint8_t config_message_disconnect = true;
uint8_t config_message_reconnect = true;
uint8_t config_minute_ticks = 1;
uint8_t config_hour_ticks = 1;
uint8_t config_color_weather = COLOR_FALLBACK(GColorBlackARGB8, GColorBlackARGB8);
uint16_t config_weather_refresh = 30;
uint16_t config_weather_expiration = 3*60;
uint8_t config_square = false;
uint8_t config_seconds = 0;
uint8_t config_color_seconds = COLOR_FALLBACK(GColorJaegerGreenARGB8, GColorBlackARGB8);
uint8_t config_date_format = 0;
////////////////////////////////////////////
//// Global variables
////////////////////////////////////////////
/** A pointer to our window, for later deallocation. */
Window *window;
/** All layers */
Layer *layer_background;
/** Buffers for strings */
char buffer_1[30];
char buffer_2[30];
/** The center of the watch */
GPoint center;
/** The height and width of the watch */
int16_t height;
int16_t width;
#ifdef OBSIDIAN_SHOW_NUMBERS
/** Open Sans font. */
GFont font_open_sans;
#endif
/** Fonts. */
#ifdef PBL_COLOR
FFont* font_main;
FFont* font_weather;
#else
GFont font_main;
GFont font_main_big;
GFont font_weather;
#endif
/** Is the bluetooth popup current supposed to be shown? */
bool show_bluetooth_popup;
/** The timer for the bluetooth popup */
AppTimer *timer_bluetooth_popup;
/** The current weather information. */
Weather weather;
/** Is the JS runtime ready? */
bool js_ready;
/** A timer used to schedule weather updates. */
AppTimer * weather_request_timer;
////////////////////////////////////////////
//// Implementation
////////////////////////////////////////////
/*
#define LOG(fmt, args...) \
do { \
char buffer[80]; \
snprintf(buffer, ARRAY_LENGTH(buffer), fmt, ## args); \
graphics_context_set_text_color(ctx, COLOR_ACCENT); \
graphics_context_set_fill_color(ctx, GColorWhite); \
graphics_fill_rect(ctx, GRect(0, 0, 144, 60), 0, GCornerNone); \
graphics_draw_text(ctx, buffer, font_system_18px_bold, GRect(5, 0, 144-2*5, 50), GTextOverflowModeWordWrap, GTextAlignmentCenter, NULL); \
} while (0)
#define LOG2(fmt, args...) \
do { \
char buffer[80]; \
snprintf(buffer, ARRAY_LENGTH(buffer), fmt, ## args); \
graphics_context_set_text_color(ctx, COLOR_ACCENT); \
graphics_draw_text(ctx, buffer, font_system_18px_bold, GRect(5, 21, 144-2*5, 50), GTextOverflowModeWordWrap, GTextAlignmentCenter, NULL); \
} while (0)
#define LOG3(fmt, args...) \
do { \
char buffer[80]; \
snprintf(buffer, ARRAY_LENGTH(buffer), fmt, ## args); \
graphics_context_set_text_color(ctx, COLOR_ACCENT); \
graphics_draw_text(ctx, buffer, font_system_18px_bold, GRect(5, 21+21, 144-2*5, 50), GTextOverflowModeWordWrap, GTextAlignmentCenter, NULL); \
} while (0)
*/
/**
* Handler for time ticks.
*/
void handle_second_tick(struct tm *tick_time, TimeUnits units_changed) {
if (config_seconds == 0 || (tick_time->tm_sec == 0) || (tick_time->tm_sec % config_seconds == 0)) {
layer_mark_dirty(layer_background);
}
#ifdef DEBUG_ITER_COUNTER
debug_iter += 1;
#endif
}
void timer_callback_bluetooth_popup(void *data) {
show_bluetooth_popup = false;
timer_bluetooth_popup = NULL;
layer_mark_dirty(layer_background);
APP_LOG(APP_LOG_LEVEL_DEBUG, "bluetooth change callback");
}
void handle_bluetooth(bool connected) {
// redraw background (to turn on/off the logo)
layer_mark_dirty(layer_background);
APP_LOG(APP_LOG_LEVEL_DEBUG, "bluetooth change");
bool show_popup = false;
bool vibrate = false;
if ((config_message_reconnect && connected) || (config_message_disconnect && !connected)) {
show_popup = true;
}
if ((config_vibrate_reconnect && connected) || (config_vibrate_disconnect && !connected)) {
vibrate = true;
}
// vibrate
if (vibrate && !quiet_time_is_active()) {
vibes_double_pulse();
}
// turn light on
if (show_popup && !quiet_time_is_active()) {
light_enable_interaction();
}
// show popup
if (show_popup) {
show_bluetooth_popup = true;
if (timer_bluetooth_popup) {
app_timer_reschedule(timer_bluetooth_popup, OBSIDIAN_BLUETOOTH_POPUP_MS);
} else {
timer_bluetooth_popup = app_timer_register(OBSIDIAN_BLUETOOTH_POPUP_MS, timer_callback_bluetooth_popup,
NULL);
}
}
}
/**
* Window load callback.
*/
void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
// create layer
layer_background = layer_create(bounds);
layer_set_update_proc(layer_background, background_update_proc);
layer_add_child(window_layer, layer_background);
// load fonts
#ifdef PBL_COLOR
font_main = ffont_create_from_resource(RESOURCE_ID_MAIN_FFONT);
font_weather = ffont_create_from_resource(RESOURCE_ID_WEATHER_FFONT);
#else
font_main = fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD);
font_main_big = fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD);
font_weather = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_NUPE_23));
#endif
// initialize
show_bluetooth_popup = false;
}
/**
* Window unload callback.
*/
void window_unload(Window *window) {
layer_destroy(layer_background);
#ifdef OBSIDIAN_SHOW_NUMBERS
fonts_unload_custom_font(font_open_sans);
#endif
#ifdef PBL_COLOR
ffont_destroy(font_main);
ffont_destroy(font_weather);
#else
fonts_unload_custom_font(font_weather);
#endif
}
void subscribe_tick(bool also_unsubscribe) {
if (also_unsubscribe) {
tick_timer_service_unsubscribe();
}
TimeUnits unit = MINUTE_UNIT;
if (config_seconds != 0) {
unit = SECOND_UNIT;
}
#ifdef DEBUG_ITER_COUNTER
unit = SECOND_UNIT;
#endif
tick_timer_service_subscribe(unit, handle_second_tick);
}
/**
* Initialization.
*/
void init() {
read_config_all();
// some alternative themes (for screenshots)
#if defined(SCREENSHOT_ALT_THEME_1) && defined(PBL_COLOR)
uint8_t accent_col = GColorRedARGB8;
#endif
#if defined(SCREENSHOT_ALT_THEME_2) && defined(PBL_COLOR)
uint8_t accent_col = GColorBlueARGB8;
#endif
#if (defined(SCREENSHOT_ALT_THEME_1) || defined(SCREENSHOT_ALT_THEME_2)) && defined(PBL_COLOR)
config_color_day_of_week = accent_col;
config_color_hour_hand = accent_col;
#endif
#if defined(SCREENSHOT_ALT_THEME_3) && defined(PBL_COLOR)
config_hour_ticks = 3;
config_minute_ticks = 3;
config_color_outer_background = GColorPurpleARGB8;
config_color_inner_background = GColorPurpleARGB8;
config_color_minute_hand = GColorBlackARGB8;
config_color_hour_hand = GColorBlackARGB8;
config_color_circle = GColorBlackARGB8;
config_color_ticks = GColorBlackARGB8;
config_color_day_of_week = GColorPurpleARGB8;
config_color_date = GColorPurpleARGB8;
config_battery_logo = 3;
config_color_inner_minute_hand = GColorBlackARGB8;
config_color_inner_hour_hand = GColorBlackARGB8;
#endif
#if defined(SCREENSHOT_ALT_THEME_4) && defined(PBL_COLOR)
config_color_outer_background = GColorBlackARGB8;
config_color_inner_background = GColorBlackARGB8;
config_color_minute_hand = GColorWhiteARGB8;
config_color_hour_hand = GColorBabyBlueEyesARGB8;
config_color_circle = GColorBlackARGB8;
config_color_ticks = GColorBlackARGB8;
config_color_day_of_week = GColorBabyBlueEyesARGB8;
config_color_date = GColorWhiteARGB8;
config_battery_logo = 3;
config_color_inner_minute_hand = GColorLightGrayARGB8;
config_color_inner_hour_hand = config_color_inner_minute_hand;
#endif
#if defined(SCREENSHOT_ALT_THEME_5) && defined(PBL_COLOR)
uint8_t col1 = GColorChromeYellowARGB8;
uint8_t col2 = GColorVividCeruleanARGB8;
config_hour_ticks = 3;
config_minute_ticks = 3;
config_color_outer_background = col2;
config_color_inner_background = col2;
config_color_minute_hand = col1;
config_color_hour_hand = col1;
config_color_circle = col2;
config_color_ticks = col1;
config_color_day_of_week = col2;
config_color_date = col2;
config_battery_logo = 3;
config_color_inner_minute_hand = col1;
config_color_inner_hour_hand = config_color_inner_minute_hand;
#endif
#if defined(SCREENSHOT_ALT_THEME_6) && defined(PBL_COLOR)
config_hour_ticks = 1;
config_minute_ticks = 2;
config_color_outer_background = GColorWhiteARGB8;
config_color_inner_background = GColorWhiteARGB8;
config_color_minute_hand = GColorRedARGB8;
config_color_hour_hand = GColorBlackARGB8;
config_color_circle = GColorBlackARGB8;
config_color_ticks = GColorBlackARGB8;
config_color_day_of_week = GColorWhiteARGB8;
config_color_date = GColorWhiteARGB8;
config_battery_logo = 3;
config_color_inner_minute_hand = GColorRedARGB8;
config_color_inner_hour_hand = GColorBlackARGB8;
#endif
#if defined(SCREENSHOT_ALT_THEME_7) && defined(PBL_COLOR)
config_hour_ticks = 1;
config_minute_ticks = 2;
config_color_outer_background = GColorWhiteARGB8;
config_color_inner_background = GColorWhiteARGB8;
config_color_minute_hand = GColorBlackARGB8;
config_color_hour_hand = GColorBlackARGB8;
config_color_circle = GColorBlackARGB8;
config_color_ticks = GColorBlackARGB8;
config_color_day_of_week = GColorWhiteARGB8;
config_color_date = GColorBlackARGB8;
config_battery_logo = 3;
config_color_inner_minute_hand = GColorBlackARGB8;
config_color_inner_hour_hand = GColorBlackARGB8;
#endif
#if defined(SCREENSHOT_ALT_THEME_8) && defined(PBL_COLOR)
config_hour_ticks = 1;
config_minute_ticks = 2;
config_color_outer_background = GColorWhiteARGB8;
config_color_inner_background = GColorIcterineARGB8;
config_color_minute_hand = GColorBlueARGB8;
config_color_hour_hand = GColorBlackARGB8;
config_color_circle = GColorBlackARGB8;
config_color_ticks = GColorBlackARGB8;
config_color_day_of_week = GColorIcterineARGB8;
config_color_date = GColorBlueARGB8;
config_battery_logo = 3;
config_color_inner_minute_hand = GColorBlueARGB8;
config_color_inner_hour_hand = GColorBlackARGB8;
#endif
#if (defined(SCREENSHOT_ALT_THEME_1) || \
defined(SCREENSHOT_ALT_THEME_2) || \
defined(SCREENSHOT_ALT_THEME_3) || \
defined(SCREENSHOT_ALT_THEME_4) || \
defined(SCREENSHOT_ALT_THEME_5) || \
defined(SCREENSHOT_ALT_THEME_6) || \
defined(SCREENSHOT_ALT_THEME_7) || \
defined(SCREENSHOT_ALT_THEME_8)) && defined(PBL_COLOR)
config_color_weather = config_color_date;
#endif
#ifdef DEBUG_NO_BATTERY_ICON
config_battery_logo = 3;
#endif
window = window_create();
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
window_stack_push(window, true);
subscribe_tick(false);
bluetooth_connection_service_subscribe(handle_bluetooth);
app_message_open(OBSIDIAN_INBOX_SIZE, OBSIDIAN_OUTBOX_SIZE);
app_message_register_inbox_received(inbox_received_handler);
}
/**
* De-initialisation.
*/
void deinit() {
tick_timer_service_unsubscribe();
battery_state_service_unsubscribe();
bluetooth_connection_service_unsubscribe();
window_destroy(window);
app_message_deregister_callbacks();
}
/**
* Main entry point.
*/
int main() {
init();
app_event_loop();
deinit();
}