Advertisement
Guest User

Untitled

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