intuiclock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * Copyright (c) 2015 Tomek Wójcik <tomek@bthlabs.pl>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include <pebble.h>
  23. static Window *s_main_window;
  24. static GBitmap *s_background_bitmap;
  25. static GBitmap *s_battery_bitmap;
  26. static GFont s_text_font;
  27. static GPath *s_minute_hand;
  28. static GPath *s_hour_hand;
  29. static BitmapLayer *s_background_layer;
  30. static TextLayer *s_title_layer;
  31. static Layer *s_hands_layer;
  32. static TextLayer *s_date_layer;
  33. static TextLayer *s_ampm_layer;
  34. static BitmapLayer *s_battery_icon_layer;
  35. static Layer *s_battery_indicator_layer;
  36. static struct tm *t = NULL;
  37. static uint8_t battery_charge = 0;
  38. static bool hide_seconds_hand = false;
  39. static bool hide_battery_indicator = false;
  40. static char date_format[2];
  41. static bool use_wide_layout = false;
  42. static const GPathInfo MINUTE_HAND_POINTS = {
  43. 4,
  44. (GPoint []) {
  45. { 0, 0 },
  46. { -4.5, -26 },
  47. { 0, -38 },
  48. { 4.5, -26 }
  49. }
  50. };
  51. static const GPathInfo HOUR_HAND_POINTS = {
  52. 4,
  53. (GPoint []) {
  54. { 0, 0 },
  55. { -4.5, -22 },
  56. { 0, -29 },
  57. { 4.5, -22 }
  58. }
  59. };
  60. #define AMPM_TEXT_SIZE 3
  61. #define DUMMY_AMPM_TEXT ""
  62. #define DATE_TEXT_SIZE 12
  63. #define DUMMY_DATE_TEXT "23 Jul 1985"
  64. #define KEY_SECONDS_HAND 0
  65. #define KEY_BATTERY_INDICATOR 1
  66. #define KEY_DATE_FORMAT 2
  67. #define KEY_WIDE_LAYOUT 3
  68. static void update_date_text_layer() {
  69. static char date_text[DATE_TEXT_SIZE];
  70. size_t result = 0;
  71. if (strcmp(date_format, "d") == 0) {
  72. result = strftime(date_text, DATE_TEXT_SIZE, "%d/%m/%y", t);
  73. } else if (strcmp(date_format, "m") == 0) {
  74. result = strftime(date_text, DATE_TEXT_SIZE, "%m/%d/%y", t);
  75. } else {
  76. result = strftime(date_text, DATE_TEXT_SIZE, "%e %b %Y", t);
  77. }
  78. if (result != 0) {
  79. text_layer_set_text(s_date_layer, date_text);
  80. } else {
  81. text_layer_set_text(s_date_layer, DUMMY_DATE_TEXT);
  82. }
  83. }
  84. static void update_ampm_text_layer() {
  85. static char ampm_text[AMPM_TEXT_SIZE];
  86. if (clock_is_24h_style()) {
  87. text_layer_set_text(s_ampm_layer, DUMMY_AMPM_TEXT);
  88. } else {
  89. size_t result = strftime(ampm_text, AMPM_TEXT_SIZE, "%p", t);
  90. if (result != 0) {
  91. text_layer_set_text(s_ampm_layer, ampm_text);
  92. } else {
  93. text_layer_set_text(s_ampm_layer, DUMMY_AMPM_TEXT);
  94. }
  95. }
  96. text_layer_set_text(s_ampm_layer, ampm_text);
  97. }
  98. static void tick_handler(struct tm *tick_time, TimeUnits changed_units) {
  99. // Getting the current time.
  100. time_t now = time(NULL);
  101. t = localtime(&now);
  102. // Updating views.
  103. layer_mark_dirty(s_hands_layer);
  104. update_date_text_layer();
  105. update_ampm_text_layer();
  106. }
  107. static void s_hands_layer_update(Layer *layer, GContext *ctx) {
  108. GRect bounds = layer_get_bounds(layer);
  109. GPoint center = grect_center_point(&bounds);
  110. // Drawing the minute hand.
  111. graphics_context_set_fill_color(ctx, GColorBlack);
  112. graphics_context_set_stroke_color(ctx, GColorBlack);
  113. gpath_move_to(s_minute_hand, (GPoint){39.5, 39});
  114. gpath_rotate_to(s_minute_hand, TRIG_MAX_ANGLE * t->tm_min / 60);
  115. gpath_draw_outline(ctx, s_minute_hand);
  116. gpath_draw_filled(ctx, s_minute_hand);
  117. // Drawing the hour hand.
  118. graphics_context_set_fill_color(ctx, GColorBlack);
  119. graphics_context_set_stroke_color(ctx, GColorBlack);
  120. gpath_move_to(s_hour_hand, (GPoint){39.5, 39});
  121. gpath_rotate_to(s_hour_hand, (TRIG_MAX_ANGLE * (((t->tm_hour % 12) * 6) + (t->tm_min / 10))) / (12 * 6));
  122. gpath_draw_outline(ctx, s_hour_hand);
  123. gpath_draw_filled(ctx, s_hour_hand);
  124. // Drawing the second hand.
  125. if (hide_seconds_hand == false) {
  126. graphics_context_set_antialiased(ctx, false);
  127. int32_t second_angle = TRIG_MAX_ANGLE * t->tm_sec / 60;
  128. int16_t second_hand_length = (bounds.size.w / 2) - 1;
  129. GPoint second_hand = {
  130. .x = (int16_t)(sin_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.x,
  131. .y = (int16_t)(-cos_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.y,
  132. };
  133. graphics_context_set_stroke_color(ctx, GColorPictonBlue);
  134. graphics_draw_line(ctx, second_hand, center);
  135. graphics_context_set_antialiased(ctx, true);
  136. }
  137. }
  138. static void s_battery_indicator_layer_update(Layer *layer, GContext *ctx) {
  139. GRect bounds = layer_get_bounds(layer);
  140. graphics_context_set_fill_color(ctx, GColorGreen);
  141. if (battery_charge <= 10) {
  142. graphics_context_set_fill_color(ctx, GColorRed);
  143. } else if (battery_charge <= 60) {
  144. graphics_context_set_fill_color(ctx, GColorYellow);
  145. }
  146. graphics_fill_rect(ctx, bounds, 0, GCornerNone);
  147. }
  148. static void battery_state_handler(BatteryChargeState charge) {
  149. if (charge.charge_percent != battery_charge) {
  150. battery_charge = charge.charge_percent;
  151. layer_mark_dirty(s_battery_indicator_layer);
  152. }
  153. }
  154. static void start_time_tracking() {
  155. TimeUnits unit = MINUTE_UNIT;
  156. if (hide_seconds_hand == false) {
  157. unit = SECOND_UNIT;
  158. }
  159. tick_handler(NULL, unit);
  160. tick_timer_service_subscribe(unit, tick_handler);
  161. }
  162. static void start_battery_tracking() {
  163. if (hide_battery_indicator == false) {
  164. BatteryChargeState charge = battery_state_service_peek();
  165. battery_state_handler(charge);
  166. battery_state_service_subscribe(battery_state_handler);
  167. } else {
  168. battery_state_service_unsubscribe();
  169. }
  170. layer_set_hidden(bitmap_layer_get_layer(s_battery_icon_layer), hide_battery_indicator);
  171. layer_set_hidden(s_battery_indicator_layer, hide_battery_indicator);
  172. }
  173. static void update_geometry() {
  174. if (s_background_bitmap) {
  175. gbitmap_destroy(s_background_bitmap);
  176. }
  177. if (use_wide_layout == false) {
  178. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BACKGROUND);
  179. layer_set_frame(text_layer_get_layer(s_title_layer), GRect(36, 2, 46, 18));
  180. layer_set_frame(text_layer_get_layer(s_date_layer), GRect(17, 130, 109, 16));
  181. layer_set_frame(text_layer_get_layer(s_ampm_layer), GRect(107, 26, 16, 16));
  182. layer_set_frame(bitmap_layer_get_layer(s_battery_icon_layer), GRect(20, 26, 16, 7));
  183. layer_set_frame(s_battery_indicator_layer, GRect(21, 27, 13, 5));
  184. } else {
  185. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BACKGROUND_WIDE);
  186. layer_set_frame(text_layer_get_layer(s_title_layer), GRect(24, 2, 71, 18));
  187. layer_set_frame(text_layer_get_layer(s_date_layer), GRect(4, 130, 136, 16));
  188. layer_set_frame(text_layer_get_layer(s_ampm_layer), GRect(122, 25, 16, 16));
  189. layer_set_frame(bitmap_layer_get_layer(s_battery_icon_layer), GRect(7, 26, 16, 7));
  190. layer_set_frame(s_battery_indicator_layer, GRect(8, 27, 13, 5));
  191. }
  192. bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
  193. }
  194. static void inbox_received_handler(DictionaryIterator *iter, void *context) {
  195. // Handling seconds_hand setting.
  196. Tuple *seconds_hand_t = dict_find(iter, KEY_SECONDS_HAND);
  197. if (seconds_hand_t && seconds_hand_t->value->int32 > 0) {
  198. hide_seconds_hand = false;
  199. } else {
  200. hide_seconds_hand = true;
  201. }
  202. persist_write_bool(KEY_SECONDS_HAND, hide_seconds_hand);
  203. start_time_tracking();
  204. // Handling battery_indicator setting.
  205. Tuple *battery_indicator_t = dict_find(iter, KEY_BATTERY_INDICATOR);
  206. if (battery_indicator_t && battery_indicator_t->value->int32 > 0) {
  207. hide_battery_indicator = false;
  208. } else {
  209. hide_battery_indicator = true;
  210. }
  211. persist_write_bool(KEY_BATTERY_INDICATOR, hide_battery_indicator);
  212. start_battery_tracking();
  213. // Handling date_format setting.
  214. Tuple *date_format_t = dict_find(iter, KEY_DATE_FORMAT);
  215. if (date_format_t) {
  216. strcpy(date_format, date_format_t->value->cstring);
  217. }
  218. persist_write_string(KEY_DATE_FORMAT, date_format);
  219. update_date_text_layer();
  220. // Handling wide_layout setting.
  221. Tuple *wide_layout_t = dict_find(iter, KEY_WIDE_LAYOUT);
  222. if (wide_layout_t && wide_layout_t->value->int32 > 0) {
  223. use_wide_layout = true;
  224. } else {
  225. use_wide_layout = false;
  226. }
  227. persist_write_bool(KEY_WIDE_LAYOUT, use_wide_layout);
  228. update_geometry();
  229. }
  230. static void main_window_load(Window *window) {
  231. // Loading resources.
  232. s_battery_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMG_BATTERY);
  233. s_text_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_TOPAZ_16));
  234. // Creating hand paths.
  235. s_minute_hand = gpath_create(&MINUTE_HAND_POINTS);
  236. s_hour_hand = gpath_create(&HOUR_HAND_POINTS);
  237. Layer *window_layer = window_get_root_layer(window);
  238. GRect bounds = layer_get_bounds(window_layer);
  239. // Creating background image layer.
  240. s_background_layer = bitmap_layer_create(bounds);
  241. bitmap_layer_set_compositing_mode(s_background_layer, GCompOpSet);
  242. layer_add_child(window_layer, bitmap_layer_get_layer(s_background_layer));
  243. // Creating title text layer.
  244. s_title_layer = text_layer_create(GRect(0, 0, 71, 18));
  245. text_layer_set_background_color(s_title_layer, GColorClear);
  246. text_layer_set_text(s_title_layer, "Clock");
  247. text_layer_set_font(s_title_layer, s_text_font);
  248. layer_add_child(window_layer, text_layer_get_layer(s_title_layer));
  249. // Creating hands layer.
  250. s_hands_layer = layer_create(GRect(32, 38, 78, 78));
  251. layer_set_update_proc(s_hands_layer, s_hands_layer_update);
  252. layer_add_child(window_layer, s_hands_layer);
  253. // Creating date layer.
  254. s_date_layer = text_layer_create(GRect(0, 0, 136, 16));
  255. text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
  256. text_layer_set_background_color(s_date_layer, GColorClear);
  257. text_layer_set_font(s_date_layer, s_text_font);
  258. layer_add_child(window_layer, text_layer_get_layer(s_date_layer));
  259. // Creating AM/PM layer.
  260. s_ampm_layer = text_layer_create(GRect(0, 0, 16, 16));
  261. text_layer_set_text_alignment(s_ampm_layer, GTextAlignmentCenter);
  262. text_layer_set_background_color(s_ampm_layer, GColorClear);
  263. text_layer_set_text_color(s_ampm_layer, GColorWhite);
  264. text_layer_set_font(s_ampm_layer, s_text_font);
  265. layer_add_child(window_layer, text_layer_get_layer(s_ampm_layer));
  266. // Creating battery icon layer.
  267. s_battery_icon_layer = bitmap_layer_create(GRect(0, 0, 16, 7));
  268. bitmap_layer_set_bitmap(s_battery_icon_layer, s_battery_bitmap);
  269. bitmap_layer_set_compositing_mode(s_battery_icon_layer, GCompOpSet);
  270. layer_add_child(window_layer, bitmap_layer_get_layer(s_battery_icon_layer));
  271. // Creating battery indicator layer.
  272. s_battery_indicator_layer = layer_create(GRect(0, 0, 13, 5));
  273. layer_set_update_proc(s_battery_indicator_layer, s_battery_indicator_layer_update);
  274. layer_add_child(window_layer, s_battery_indicator_layer);
  275. // Update geometry according to wide_layout setting.
  276. use_wide_layout = persist_read_bool(KEY_WIDE_LAYOUT);
  277. update_geometry();
  278. // Read date_format setting from persistent storage.
  279. int result = persist_read_string(KEY_DATE_FORMAT, date_format, 2);
  280. if (result == E_DOES_NOT_EXIST) {
  281. strcpy(date_format, "t");
  282. }
  283. // Starting time tracking.
  284. hide_seconds_hand = persist_read_bool(KEY_SECONDS_HAND);
  285. start_time_tracking();
  286. // Starting battery tracking.
  287. hide_battery_indicator = persist_read_bool(KEY_BATTERY_INDICATOR);
  288. start_battery_tracking();
  289. }
  290. static void main_window_unload(Window *window) {
  291. battery_state_service_unsubscribe();
  292. tick_timer_service_unsubscribe();
  293. // Apparently, freeing this pointer causes the watchface to crash on
  294. // v3.3. Weird.
  295. // if (t) {
  296. // free(t);
  297. // }
  298. layer_destroy(s_battery_indicator_layer);
  299. bitmap_layer_destroy(s_battery_icon_layer);
  300. text_layer_destroy(s_ampm_layer);
  301. text_layer_destroy(s_date_layer);
  302. layer_destroy(s_hands_layer);
  303. text_layer_destroy(s_title_layer);
  304. bitmap_layer_destroy(s_background_layer);
  305. gpath_destroy(s_hour_hand);
  306. gpath_destroy(s_minute_hand);
  307. fonts_unload_custom_font(s_text_font);
  308. gbitmap_destroy(s_battery_bitmap);
  309. gbitmap_destroy(s_background_bitmap);
  310. }
  311. static void init() {
  312. s_main_window = window_create();
  313. window_set_window_handlers(s_main_window, (WindowHandlers) {
  314. .load = main_window_load,
  315. .unload = main_window_unload
  316. });
  317. window_stack_push(s_main_window, true);
  318. app_message_register_inbox_received(inbox_received_handler);
  319. app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
  320. }
  321. static void deinit() {
  322. window_destroy(s_main_window);
  323. }
  324. int main(void) {
  325. init();
  326. app_event_loop();
  327. deinit();
  328. }