An ode to Windows 3.1 Hot Dog Stand - Pebble watchface
at master 122 lines 4.6 kB view raw
1/* 2 * Copyright (c) 2015 joshua stein <jcs@jcs.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include <pebble.h> 29 30static Window *s_main_window; 31static TextLayer *s_time_layer; 32static TextLayer *s_date_layer; 33static BitmapLayer *s_background_layer; 34static GBitmap *s_background_bitmap; 35 36static void update_time() { 37 static char buffer[] = " : "; 38 static char dbuffer[] = " / / "; 39 40 time_t temp = time(NULL); 41 struct tm *tick_time = localtime(&temp); 42 43 if (clock_is_24h_style() == true) { 44 strftime(buffer, sizeof(buffer), "%H:%M", tick_time); 45 text_layer_set_text(s_time_layer, buffer); 46 } 47 else { 48 strftime(buffer, sizeof(buffer), "%I:%M %p", tick_time); 49 text_layer_set_text(s_time_layer, buffer); 50 } 51 52 strftime(dbuffer, sizeof(dbuffer), "%m/%d/%y", tick_time); 53 text_layer_set_text(s_date_layer, dbuffer); 54} 55 56static void main_window_load(Window *window) { 57 s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND); 58 s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168)); 59 bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap); 60 layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer)); 61 62 s_time_layer = text_layer_create(GRect(0, 65, 144, 50)); 63 text_layer_set_background_color(s_time_layer, GColorClear); 64 text_layer_set_text_color(s_time_layer, GColorBlack); 65 if (clock_is_24h_style() == true) { 66 layer_set_frame(text_layer_get_layer(s_time_layer), GRect(0, 70, 144, 50)); 67 text_layer_set_text(s_time_layer, " : "); 68 text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_34_MEDIUM_NUMBERS)); 69 } 70 else { 71 text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD)); 72 text_layer_set_text(s_time_layer, " : "); 73 } 74 text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter); 75 layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer)); 76 77 s_date_layer = text_layer_create(GRect(0, 95, 144, 50)); 78 if (clock_is_24h_style() == true) 79 layer_set_frame(text_layer_get_layer(s_date_layer), GRect(0, 100, 144, 50)); 80 81 text_layer_set_background_color(s_date_layer, GColorClear); 82 text_layer_set_text_color(s_date_layer, GColorBlack); 83 text_layer_set_text(s_date_layer, " / / "); 84 text_layer_set_font(s_date_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); 85 text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter); 86 layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_date_layer)); 87 88 update_time(); 89} 90 91static void main_window_unload(Window *window) { 92 text_layer_destroy(s_time_layer); 93 gbitmap_destroy(s_background_bitmap); 94 bitmap_layer_destroy(s_background_layer); 95} 96 97static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { 98 update_time(); 99} 100 101static void init() { 102 s_main_window = window_create(); 103 104 window_set_window_handlers(s_main_window, (WindowHandlers) { 105 .load = main_window_load, 106 .unload = main_window_unload 107 }); 108 109 window_stack_push(s_main_window, true); 110 111 tick_timer_service_subscribe(MINUTE_UNIT, tick_handler); 112} 113 114static void deinit() { 115 window_destroy(s_main_window); 116} 117 118int main(void) { 119 init(); 120 app_event_loop(); 121 deinit(); 122}