RootOfTheNull

Pebble "Berlin Clock"

Jul 18th, 2015
253
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <pebble.h>
  2. #define row_four_blocks 4
  3. #define row_eleven_blocks 11
  4. #define outline_four_row_width 30
  5. #define outline_four_row_next 31
  6. #define outline_four_row_height 17
  7. #define outline_four_row_offset 10
  8. #define ball_radius 21
  9. #define ball_x 70
  10. #define ball_y 41
  11. #define outline_eleven_row_width 11
  12. #define outline_eleven_row_height 12
  13. #define outline_eleven_row_next 12
  14. #define outline_first_row_y 67
  15. #define outline_second_row_y 91
  16. #define outline_third_row_y 114
  17. #define outline_fourth_row_y 134
  18.  
  19. #define fill_four_row_offset 12
  20. #define fill_first_row_y 69
  21. #define fill_second_row_y 93
  22. #define fill_third_row_y 116
  23. #define fill_fourth_row_y 136
  24. #define fill_four_row_width 27
  25. #define fill_four_row_height 13
  26. #define fill_eleven_row_width 8
  27. #define fill_eleven_row_height 8
  28. #define ball_fill_radius 18
  29.  
  30.  
  31. static Window *s_main_window;
  32. static Layer  *s_outline_layer;
  33. static Layer  *s_fill_layer;
  34.  
  35. struct BerlinClock {
  36.  
  37.   // Might be a good idea to try and object-orient this thing.
  38.  
  39.   // bool initializied;
  40.  
  41.   GRect first_row[row_four_blocks];
  42.   GRect second_row[row_four_blocks];
  43.   GRect third_row[row_eleven_blocks];
  44.   GRect fourth_row[row_four_blocks];
  45.  
  46.  
  47. };
  48.  
  49. struct BerlinClock watch;
  50.  
  51.  
  52. int odd( int number ){
  53.  
  54.   return (number%2);
  55. }
  56.  
  57. static void outline_update_procedure( Layer *this_layer, GContext *context ){
  58.  
  59.   graphics_context_set_stroke_color( context, GColorWhite );
  60.   graphics_context_set_fill_color( context, GColorWhite );
  61.  
  62.   for ( int i = 0; i < row_four_blocks; i++ ){
  63.     graphics_draw_rect( context, GRect( i*outline_four_row_width + outline_four_row_offset, outline_first_row_y, outline_four_row_next, outline_four_row_height ) );
  64.     graphics_draw_rect( context, GRect( i*outline_four_row_width + outline_four_row_offset, outline_second_row_y, outline_four_row_next, outline_four_row_height ) );
  65.     graphics_draw_rect( context, GRect( i*outline_four_row_width + outline_four_row_offset, outline_fourth_row_y, outline_four_row_next, outline_four_row_height ) );
  66.  
  67.   }
  68.  
  69.   for ( int i = 0; i < row_eleven_blocks; i++ ){
  70.     graphics_draw_rect( context, GRect( i*outline_eleven_row_width + outline_four_row_offset, outline_third_row_y, outline_eleven_row_next, outline_eleven_row_height ) );
  71.   }
  72.  
  73.   graphics_draw_circle( context, GPoint( ball_x, ball_y ), ball_radius );
  74. }
  75.  
  76. static void fill_update_procedure( Layer *this_layer, GContext *context ){
  77.  
  78.   time_t temp = time(NULL);
  79.   struct tm *ttime = localtime( &temp );
  80.  
  81.   if ( odd( ttime->tm_sec ) ){
  82.  
  83.     graphics_context_set_fill_color( context, GColorWhite );
  84.     graphics_fill_circle( context, GPoint( ball_x, ball_y ), ball_fill_radius );
  85.   }else{
  86.     graphics_context_set_fill_color( context, GColorBlack );
  87.     graphics_fill_circle( context, GPoint( ball_x, ball_y ), ball_fill_radius );
  88.   }
  89.  
  90.   int first_row = (int)(ttime->tm_hour / 5);
  91.   int second_row = (ttime->tm_hour % 5);
  92.  
  93.   int third_row = (int)(ttime->tm_min / 5);
  94.   int fourth_row = (ttime->tm_min % 5);
  95.  
  96.   graphics_context_set_fill_color( context, GColorWhite );
  97.  
  98.   for ( int i = 0; i < first_row; i++ ){
  99.     graphics_fill_rect( context, watch.first_row[i], 0, GCornerNone );
  100.   }
  101.  
  102.   for ( int i = 0; i < second_row; i++ ){
  103.     graphics_fill_rect( context, watch.second_row[i], 0, GCornerNone );
  104.   }
  105.  
  106.   for ( int i = 0; i < third_row; i++ ){
  107.     graphics_fill_rect( context, watch.third_row[i], 0, GCornerNone );
  108.   }
  109.  
  110.   for ( int i = 0; i < fourth_row; i++ ){
  111.     graphics_fill_rect( context, watch.fourth_row[i], 0, GCornerNone );
  112.   }
  113.  
  114. }
  115.  
  116. static void tick_handler( struct tm *tick_time, TimeUnits units_changed ){
  117.  
  118.   layer_mark_dirty( s_fill_layer );
  119.  
  120.   /*  
  121.  
  122.     Marks the complete layer as "dirty", awaiting to be asked by the
  123.     system to redraw itself. Typically, this function is called whenever state
  124.     has changed that affects what the layer is displaying. The layer's
  125.     .update_proc will not be called before this function returns, but will be
  126.     called asynchronously, shortly.  
  127.    
  128.   */
  129.  
  130. }
  131.  
  132.  
  133.  
  134. static void main_window_load( Window *window ){
  135.  
  136.   Layer *window_layer = window_get_root_layer( window );
  137.   GRect window_bounds = layer_get_bounds( window_layer );
  138.  
  139.   s_outline_layer = layer_create( GRect( 0, 0, window_bounds.size.w, window_bounds.size.h ) );
  140.   s_fill_layer = layer_create( GRect( 0, 0, window_bounds.size.w, window_bounds.size.h ) );
  141.   layer_add_child( window_layer, s_outline_layer );
  142.   layer_add_child( s_outline_layer, s_fill_layer );
  143.  
  144.   layer_set_update_proc( s_outline_layer, outline_update_procedure );
  145.   layer_set_update_proc( s_fill_layer, fill_update_procedure );
  146.  
  147.   for ( int i = 0; i < row_four_blocks; i++ ){
  148.     watch.first_row[i] = GRect( i*outline_four_row_width + fill_four_row_offset, fill_first_row_y, fill_four_row_width, fill_four_row_height );
  149.     watch.second_row[i] = GRect( i*outline_four_row_width + fill_four_row_offset, fill_second_row_y, fill_four_row_width, fill_four_row_height );
  150.     watch.fourth_row[i] = GRect( i*outline_four_row_width + fill_four_row_offset, fill_fourth_row_y, fill_four_row_width, fill_four_row_height );    
  151.   }
  152.   for ( int i = 0; i < row_eleven_blocks; i++ ){
  153.     watch.third_row[i] = GRect( i*outline_eleven_row_width + fill_four_row_offset, fill_third_row_y, fill_eleven_row_width, fill_eleven_row_height );
  154.   }
  155.  
  156. }
  157.  
  158. static void main_window_unload( Window *window ){
  159.  
  160.   layer_destroy( s_outline_layer );
  161.   layer_destroy( s_fill_layer );
  162.  
  163. }
  164.  
  165. static void init(){
  166.  
  167.   s_main_window = window_create();
  168.   window_set_background_color( s_main_window, GColorBlack );
  169.   window_set_window_handlers( s_main_window, (WindowHandlers){
  170.  
  171.     .load = main_window_load,
  172.     .unload = main_window_unload,
  173.   } );
  174.  
  175.   window_stack_push( s_main_window, true );
  176.  
  177.   tick_timer_service_subscribe( SECOND_UNIT, tick_handler );
  178.  
  179. }
  180.  
  181. static void deinit(){
  182.  
  183.   window_destroy( s_main_window );
  184.  
  185. }
  186.  
  187. int main( void ){
  188.  
  189.   init();
  190.   app_event_loop();
  191.   deinit();
  192. }
RAW Paste Data