Tutorials from Pebble developer website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
8.0 KiB

  1. #include <pebble.h>
  2. #define KEY_TEMPERATURE 0
  3. #define KEY_CONDITIONS 1
  4. // Function signatures
  5. static void update_time();
  6. static void tick_handler(struct tm *tick_time, TimeUnits units_changed);
  7. static void main_window_load(Window *window);
  8. static void main_window_unload(Window *window);
  9. static void init();
  10. static void deinit();
  11. static void inbox_received_callback(DictionaryIterator *iterator, void *context);
  12. static void inbox_dropped_callback(AppMessageResult reason, void *context);
  13. static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context);
  14. static void outbox_sent_callback(DictionaryIterator *iterator, void *context);
  15. // Global variables
  16. static Window *s_main_window;
  17. static TextLayer *s_time_layer;
  18. static TextLayer *s_weather_layer;
  19. static TextLayer *s_daymonth_layer;
  20. //static TextLayer *s_year_layer;
  21. static GFont s_time_font;
  22. static GFont s_weather_font;
  23. static GFont s_date_font;
  24. static BitmapLayer *s_background_layer;
  25. static GBitmap *s_background_bitmap;
  26. int main(void) {
  27. init();
  28. app_event_loop();
  29. deinit();
  30. }
  31. static void update_time() {
  32. // Get a tm structure
  33. time_t temp = time(NULL);
  34. struct tm *tick_time = localtime(&temp);
  35. // Create a long-lived buffer
  36. static char buffer[] = "00:00";
  37. // Write the current hours and minutes into the buffer
  38. if (clock_is_24h_style()) {
  39. // Use 24 hour format
  40. strftime(buffer, sizeof("00:00"), "%H:%M", tick_time);
  41. } else {
  42. // Use 12 hour format
  43. strftime(buffer, sizeof("00:00"), "%I:%M", tick_time);
  44. }
  45. // Display this time on the TextLayer
  46. text_layer_set_text(s_time_layer, buffer);
  47. }
  48. static void update_date() {
  49. // Get a tm structure
  50. time_t temp = time(NULL);
  51. struct tm *tick_time = localtime(&temp);
  52. // Create a long-lived buffer
  53. static char buffer_daymonth[] = "00.000";
  54. // Write the current hours and minutes into the buffer
  55. strftime(buffer_daymonth, sizeof(buffer_daymonth), "%e.%b", tick_time);
  56. // Display this time on the TextLayer
  57. text_layer_set_text(s_daymonth_layer, buffer_daymonth);
  58. }
  59. static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  60. update_time();
  61. if (units_changed & DAY_UNIT) {
  62. update_date();
  63. }
  64. // Get weather update every 30 minutes
  65. if (tick_time->tm_min % 30 == 0) {
  66. // Begin dictionary
  67. DictionaryIterator *iter;
  68. app_message_outbox_begin(&iter);
  69. // Add a key-value pair
  70. dict_write_uint8(iter, 0, 0);
  71. // Send the message!
  72. app_message_outbox_send();
  73. }
  74. }
  75. static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
  76. // Store incoming information
  77. static char temperature_buffer[8];
  78. static char conditions_buffer[32];
  79. static char weather_layer_buffer[32];
  80. // Read first item
  81. Tuple *t = dict_read_first(iterator);
  82. // For all items
  83. while(t != NULL) {
  84. // Which key was received?
  85. switch(t->key) {
  86. case KEY_TEMPERATURE:
  87. snprintf(temperature_buffer, sizeof(temperature_buffer), "%d°C", (int)t->value->int32);
  88. break;
  89. case KEY_CONDITIONS:
  90. snprintf(conditions_buffer, sizeof(conditions_buffer), "%s", t->value->cstring);
  91. break;
  92. default:
  93. APP_LOG(APP_LOG_LEVEL_ERROR, "Key %d not recognized!", (int)t->key);
  94. break;
  95. }
  96. // Look for next item
  97. t = dict_read_next(iterator);
  98. }
  99. // Assemble full string and display
  100. snprintf(weather_layer_buffer, sizeof(weather_layer_buffer), "%s, %s", temperature_buffer, conditions_buffer);
  101. text_layer_set_text(s_weather_layer, weather_layer_buffer);
  102. }
  103. static void inbox_dropped_callback(AppMessageResult reason, void *context) {
  104. APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped!");
  105. }
  106. static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) {
  107. APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed!");
  108. }
  109. static void outbox_sent_callback(DictionaryIterator *iterator, void *context) {
  110. APP_LOG(APP_LOG_LEVEL_INFO, "Outbox send success!");
  111. }
  112. static void main_window_load(Window *window) {
  113. // Create GBitmap, then set to created BitmapLayer
  114. s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_TUTORIAL_BACKGROUND);
  115. s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168));
  116. bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
  117. // Create time TextLayer
  118. s_time_layer = text_layer_create(GRect(5, 52, 139, 50));
  119. text_layer_set_background_color(s_time_layer, GColorClear);
  120. text_layer_set_text_color(s_time_layer, GColorBlack);
  121. // Create temperature Layer
  122. s_weather_layer = text_layer_create(GRect(0, 130, 144, 25));
  123. text_layer_set_background_color(s_weather_layer, GColorClear);
  124. text_layer_set_text_color(s_weather_layer, GColorWhite);
  125. text_layer_set_text(s_weather_layer, "Loading...");
  126. // Create day-month TextLayer
  127. s_daymonth_layer = text_layer_create(GRect(5, 12, 139, 30));
  128. text_layer_set_background_color(s_daymonth_layer, GColorBlack);
  129. text_layer_set_text_color(s_daymonth_layer, GColorWhite);
  130. // Create year TextLayer
  131. // s_year_layer = text_layer_create(GRect(5, 123, 139, 30));
  132. // text_layer_set_background_color(s_year_layer, GColorBlack);
  133. // text_layer_set_text_color(s_year_layer, GColorWhite);
  134. // Create GFont
  135. s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_48));
  136. s_weather_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_16));
  137. s_date_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_24));
  138. // Apply to TextLayer
  139. text_layer_set_font(s_time_layer, s_time_font);
  140. text_layer_set_font(s_weather_layer, s_weather_font);
  141. text_layer_set_font(s_daymonth_layer, s_date_font);
  142. // text_layer_set_font(s_year_layer, s_date_font);
  143. // Improve the layout to be more like a watchface
  144. text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  145. text_layer_set_text_alignment(s_weather_layer, GTextAlignmentCenter);
  146. text_layer_set_text_alignment(s_daymonth_layer, GTextAlignmentCenter);
  147. // text_layer_set_text_alignment(s_year_layer, GTextAlignmentCenter);
  148. // Add it as a child layer to the Window's root layer
  149. layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer));
  150. layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));
  151. layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_weather_layer));
  152. layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_daymonth_layer));
  153. // layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_year_layer));
  154. }
  155. static void main_window_unload(Window *window) {
  156. // Destroy TextLayer
  157. text_layer_destroy(s_time_layer);
  158. text_layer_destroy(s_weather_layer);
  159. text_layer_destroy(s_daymonth_layer);
  160. // text_layer_destroy(s_year_layer);
  161. // Unload GFont
  162. fonts_unload_custom_font(s_time_font);
  163. fonts_unload_custom_font(s_weather_font);
  164. fonts_unload_custom_font(s_date_font);
  165. // Destroy GBitmap
  166. gbitmap_destroy(s_background_bitmap);
  167. // Destroy BitmapLayer
  168. bitmap_layer_destroy(s_background_layer);
  169. }
  170. static void init() {
  171. // Create main Window element and assign to pointer
  172. s_main_window = window_create();
  173. // Set handlers to manage the elements inside the Window
  174. window_set_window_handlers(s_main_window, (WindowHandlers){
  175. .load = main_window_load,
  176. .unload = main_window_unload
  177. });
  178. // Show the Window on the watch, with animated=true
  179. window_stack_push(s_main_window, true);
  180. // Register with TickTimerService
  181. tick_timer_service_subscribe((MINUTE_UNIT | DAY_UNIT), tick_handler);
  182. // Register AppMessage callbacks
  183. app_message_register_inbox_received(inbox_received_callback);
  184. app_message_register_inbox_dropped(inbox_dropped_callback);
  185. app_message_register_outbox_failed(outbox_failed_callback);
  186. app_message_register_outbox_sent(outbox_sent_callback);
  187. // Open AppMessage
  188. app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
  189. update_time();
  190. update_date();
  191. }
  192. static void deinit() {
  193. // Destroy Window
  194. window_destroy(s_main_window);
  195. }