Source

3 Timezones / cloud / th10.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
/** \file
 * TH10 -- Torgoen T10 analog style
 *
 */
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"

#define CONFIG_SECOND


#define UUID { 0xDD, 0xD6, 0x07, 0x2A, 0x2D, 0x0B, 0x4D, 0x79, 0x99, 0x6D, 0xDC, 0x69, 0xD4, 0x62, 0xB8, 0x5B }

PBL_APP_INFO(
        UUID,
        "TH10",
        "hudson",
        1, 0, // Version
        INVALID_RESOURCE, //RESOURCE_ID_IMAGE_MENU_ICON,
        APP_INFO_WATCH_FACE
);


static Window window;
static Layer hand_layer;
static Layer bg_layer;
#ifdef CONFIG_SECOND
static Layer sec_layer;
#endif

static PblTm now;
static GFont font_time;
static GFont font_date;

static int use_24hour;


// Dimensions of the watch face
#define W 144
#define H 168
#define R (W/2 - 2)

// Hour hand
static GPath hour_path;
static GPoint hour_points[] = {
        {  -8, -20 },
        { -10, -40 },
        {   0, -60 },
        { +10, -40 },
        {  +8, -20 },
        {  -8, -20 },
};

// Minute hand
static GPath minute_path;
static GPoint minute_points[] = {
        { -5, -20 },
        { -7, -60 },
        {  0, -76 },
        { +7, -60 },
        { +5, -20 },
        { -5, -20 },
};

#ifdef CONFIG_SECOND
// Second hand
static GPath sec_path;
static GPoint sec_points[] = {
        { -3, -20 },
        { -3, -60 },
        {  0, -76 },
        { +3, -60 },
        { +3, -20 },
        { -3, -20 },
};
#endif


// Hour hand ticks around the circle (slightly shorter)
static GPath hour_tick_path;
static GPoint hour_tick_points[] = {
        { -3, 70 },
        { +3, 70 },
        { +3, 84 },
        { -3, 84 },
};

// Non-hour major ticks around the circle
static GPath major_tick_path;
static GPoint major_tick_points[] = {
        { -3, 60 },
        { +3, 60 },
        { +3, 84 },
        { -3, 84 },
};

// Non-major ticks around the circle; will be drawn as lines
static GPath minor_tick_path;
static GPoint minor_tick_points[] = {
        { 0, 76 },
        { 0, 84 },
};


static void
hand_layer_update(
        Layer * const me,
        GContext * ctx
)
{
        (void) me;

        // Draw the minute hand outline in black and filled with white
        int minute_angle = ((now.tm_min * 60 + now.tm_sec) * TRIG_MAX_ANGLE) / 3600;

        gpath_rotate_to(&minute_path, minute_angle);
        graphics_context_set_stroke_color(ctx, GColorBlack);
        gpath_draw_outline(ctx, &minute_path);
        graphics_context_set_fill_color(ctx, GColorWhite);
        gpath_draw_filled(ctx, &minute_path);

        // Draw the hour hand outline in black and filled with white
        // above the minute hand
        int hour_angle;
        if (use_24hour)
        {
                hour_angle = ((now.tm_hour * 60 + now.tm_min) * TRIG_MAX_ANGLE) / (60 * 24);
        } else {
                int hour = now.tm_hour;
                if (hour > 12)
                        hour -= 12;
                hour_angle = ((hour * 60 + now.tm_min) * TRIG_MAX_ANGLE) / (60 * 12);
        }

        gpath_rotate_to(&hour_path, hour_angle);
        graphics_context_set_stroke_color(ctx, GColorBlack);
        gpath_draw_outline(ctx, &hour_path);
        graphics_context_set_fill_color(ctx, GColorWhite);
        gpath_draw_filled(ctx, &hour_path);

}


#ifdef CONFIG_SECOND
static void
sec_layer_update(
        Layer * const me,
        GContext * ctx
)
{
        (void) me;

        // Draw the second hand outline in black and filled with white
        int sec_angle = (now.tm_sec * TRIG_MAX_ANGLE) / 60;

        gpath_rotate_to(&sec_path, sec_angle);
        graphics_context_set_stroke_color(ctx, GColorBlack);
        gpath_draw_outline(ctx, &sec_path);
        graphics_context_set_fill_color(ctx, GColorWhite);
        gpath_draw_filled(ctx, &sec_path);
}
#endif


/** Called once per minute */
static void
handle_tick(
        AppContextRef ctx,
        PebbleTickEvent * const event
)
{
        (void) ctx;

        // If the day of month changes, for a redraw of the background
        if (now.tm_mday != event->tick_time->tm_mday)
                layer_mark_dirty(&bg_layer);

        layer_mark_dirty(&hand_layer);

        now = *event->tick_time;
}


/** Draw the initial background image */
static void
bg_layer_update(
        Layer * const me,
        GContext * ctx
)
{
        (void) me;

        graphics_context_set_stroke_color(ctx, GColorWhite);
        graphics_context_set_fill_color(ctx, GColorWhite);

        // Draw the center circle
        graphics_draw_circle(ctx, GPoint(W/2,H/2), 3);

        // Draw the outside marks
        for (int min = 0 ; min < 60 ; min++)
        {
                const int angle = (min * TRIG_MAX_ANGLE) / 60;
                if ((min % 15) == 0)
                {
                        gpath_rotate_to(&hour_tick_path, angle);
                        gpath_draw_filled(ctx, &hour_tick_path);
                } else
                if ((min % 5) == 0)
                {
                        gpath_rotate_to(&major_tick_path, angle);
                        gpath_draw_filled(ctx, &major_tick_path);
                } else {
                        gpath_rotate_to(&minor_tick_path, angle);
                        gpath_draw_outline(ctx, &minor_tick_path);
                }
        }

        // And the large labels
        graphics_context_set_text_color(ctx, GColorWhite);
        graphics_text_draw(ctx,
                use_24hour ? "24" : "12",
                font_time,
                GRect(W/2-30,4,60,50),
                GTextOverflowModeTrailingEllipsis,
                GTextAlignmentCenter,
                NULL
        );

        graphics_text_draw(ctx,
                use_24hour ? "06" : "3",
                font_time,
                GRect(W/2,H/2-26,70,50),
                GTextOverflowModeTrailingEllipsis,
                GTextAlignmentRight,
                NULL
        );

        graphics_text_draw(ctx,
                use_24hour ? "12" : "6",
                font_time,
                GRect(W/2-30,110,60,50),
                GTextOverflowModeTrailingEllipsis,
                GTextAlignmentCenter,
                NULL
        );

        graphics_text_draw(ctx,
                use_24hour ? "18" : "9",
                font_time,
                GRect(W/2-70,H/2-26,60,50),
                GTextOverflowModeTrailingEllipsis,
                GTextAlignmentLeft,
                NULL
        );

        // Draw a small box with the current date
        int mday = now.tm_mday;
        char mday_str[3] = {
                '0' + (mday / 10),
                '0' + (mday % 10),
                '\0'
        };

#if 0
        graphics_context_set_fill_color(ctx, GColorWhite);
        const int date_x = W/2+25;
        const int date_y = H/2+25;

        graphics_fill_rect(
                ctx,
                GRect(date_x, date_y, 20, 18),
                2,
                GCornersAll
        );

        graphics_context_set_text_color(ctx, GColorBlack);
        graphics_text_draw(
                ctx,
                mday_str,
                font_date,
                GRect(date_x, date_y-1, 20, 18),
                GTextOverflowModeTrailingEllipsis,
                GTextAlignmentCenter,
                NULL
        );
#else
        // lower right corner? Looks ok, but not classic
        const int date_x = W - 20;
        const int date_y = H - 18;

        graphics_text_draw(
                ctx,
                mday_str,
                font_date,
                GRect(date_x, date_y-1, 20, 18),
                GTextOverflowModeTrailingEllipsis,
                GTextAlignmentCenter,
                NULL
        );
#endif
}

#define GPATH_INIT(PATH, POINTS) \
        gpath_init( \
                PATH, \
                &(GPathInfo) { \
                        sizeof(POINTS) / sizeof(*POINTS), \
                        POINTS, \
                } \
        )

static void
handle_init(
        AppContextRef ctx
)
{
        (void) ctx;

        GPATH_INIT(&hour_path, hour_points);
        gpath_move_to(&hour_path, GPoint(W/2,H/2));

        GPATH_INIT(&minute_path, minute_points);
        gpath_move_to(&minute_path, GPoint(W/2,H/2));

#ifdef CONFIG_SECOND
        GPATH_INIT(&sec_path, sec_points);
        gpath_move_to(&sec_path, GPoint(W/2,H/2));
#endif

        GPATH_INIT(&major_tick_path, major_tick_points);
        gpath_move_to(&major_tick_path, GPoint(W/2,H/2));

        GPATH_INIT(&hour_tick_path, hour_tick_points);
        gpath_move_to(&hour_tick_path, GPoint(W/2,H/2));

        GPATH_INIT(&minor_tick_path, minor_tick_points);
        gpath_move_to(&minor_tick_path, GPoint(W/2,H/2));

        get_time(&now);
        use_24hour = clock_is_24h_style();

        window_init(&window, "Main");
        window_stack_push(&window, true);
        window_set_background_color(&window, GColorBlack);

        resource_init_current_app(&APP_RESOURCES);

        //font_time = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_GILLSANS_40));
        //font_date = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_GILLSANS_16));
		font_time = fonts_get_system_font(FONT_KEY_GOTHAM_42_BOLD);
		font_date = fonts_get_system_font(FONT_KEY_GOTHIC_14);
	
        layer_init(&bg_layer, GRect(0, 0, W, H));
        layer_add_child(&window.layer, &bg_layer);
        bg_layer.update_proc = bg_layer_update;
        layer_mark_dirty(&bg_layer);

        layer_init(&hand_layer, GRect(0, 0, W, H));
        layer_add_child(&window.layer, &hand_layer);
        hand_layer.update_proc = hand_layer_update;
        layer_mark_dirty(&hand_layer);

#ifdef CONFIG_SECOND
        layer_init(&sec_layer, GRect(0, 0, W, H));
        layer_add_child(&window.layer, &sec_layer);
        sec_layer.update_proc = sec_layer_update;
        layer_mark_dirty(&sec_layer);
#endif
}


static void
handle_deinit(
        AppContextRef ctx
)
{
        (void) ctx;

        //fonts_unload_custom_font(font_time);
        //fonts_unload_custom_font(font_date);
}


void
pbl_main(
        void * const params
)
{
        PebbleAppHandlers handlers = {
                .init_handler   = &handle_init,
                .deinit_handler = &handle_deinit,
                .tick_info      = {
                        .tick_handler = &handle_tick,
#ifdef CONFIG_SECOND
                        .tick_units = SECOND_UNIT,
#else
                        .tick_units = SECOND_MINUTE,
#endif
                },
        };

        app_event_loop(params, &handlers);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.