Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Animation source:
- * http://bestanimations.com/Science/Physics/Physics2.html
- */
- #include <pebble.h>
- static Window *s_main_window;
- static TextLayer *s_time_layer, *s_date_layer;
- static GBitmap *s_bitmap = NULL;
- static GFont s_time_font, s_date_font;
- static BitmapLayer *s_bitmap_layer;
- static GBitmapSequence *s_sequence = NULL;
- static void load_sequence();
- static void timer_handler(void *context) {
- uint32_t next_delay;
- // Advance to the next APNG frame
- if(gbitmap_sequence_update_bitmap_next_frame(s_sequence, s_bitmap, &next_delay)) {
- bitmap_layer_set_bitmap(s_bitmap_layer, s_bitmap);
- layer_mark_dirty(bitmap_layer_get_layer(s_bitmap_layer));
- // Timer for that delay
- app_timer_register(next_delay, timer_handler, NULL);
- } else {
- // Start again
- load_sequence();
- }
- }
- static void load_sequence() {
- // Free old data
- if(s_sequence) {
- gbitmap_sequence_destroy(s_sequence);
- s_sequence = NULL;
- }
- if(s_bitmap) {
- gbitmap_destroy(s_bitmap);
- s_bitmap = NULL;
- }
- // Create sequence
- s_sequence = gbitmap_sequence_create_with_resource(RESOURCE_ID_ANIMATION);
- // Create GBitmap
- s_bitmap = gbitmap_create_blank(gbitmap_sequence_get_bitmap_size(s_sequence), GBitmapFormat8Bit);
- // Begin animation
- app_timer_register(1, timer_handler, NULL);
- }
- static void update_time() {
- time_t temp = time(NULL);
- struct tm *tick_time = localtime(&temp);
- // Create a long-lived buffer, and show the time
- static char buffer[] = "00:00";
- if(clock_is_24h_style()) {
- strftime(buffer, sizeof("00:00"), "%H:%M", tick_time);
- } else {
- strftime(buffer, sizeof("00:00"), "%I:%M", tick_time);
- }
- text_layer_set_text(s_time_layer, buffer);
- // Show the date
- static char date_buffer[16];
- strftime(date_buffer, sizeof(date_buffer), "%a %d %b", tick_time);
- text_layer_set_text(s_date_layer, date_buffer);
- }
- static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
- update_time();
- }
- static void main_window_load(Window *window) {
- Layer *window_layer = window_get_root_layer(window);
- GRect bounds = layer_get_bounds(window_layer);
- s_bitmap_layer = bitmap_layer_create(bounds);
- layer_add_child(window_layer, bitmap_layer_get_layer(s_bitmap_layer));
- load_sequence();
- // Create GFonts
- s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_53));
- s_date_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_24));
- // Create time TextLayer
- s_time_layer = text_layer_create(GRect(5, 10, 139, 50));
- text_layer_set_background_color(s_time_layer, GColorClear);
- text_layer_set_text_color(s_time_layer, GColorFromRGB(255,255,255));
- text_layer_set_text(s_time_layer, "00:00");
- text_layer_set_font(s_time_layer, s_time_font);
- text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
- layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));
- // Create date TextLayer
- s_date_layer = text_layer_create(GRect(0, 56, 144, 30));
- text_layer_set_text_color(s_date_layer, GColorWhite);
- text_layer_set_background_color(s_date_layer, GColorClear);
- text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
- text_layer_set_text(s_date_layer, "Jul 26");
- text_layer_set_font(s_date_layer, s_date_font);
- layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_date_layer));
- // Initialize the display
- update_time();
- }
- static void main_window_unload(Window *window) {
- bitmap_layer_destroy(s_bitmap_layer);
- fonts_unload_custom_font(s_time_font);
- fonts_unload_custom_font(s_date_font);
- text_layer_destroy(s_time_layer);
- text_layer_destroy(s_date_layer);
- }
- static void init() {
- s_main_window = window_create();
- window_set_background_color(s_main_window, GColorBlack);
- #if defined(PBL_SDK_2)
- window_set_fullscreen(s_main_window, true);
- #endif
- window_set_window_handlers(s_main_window, (WindowHandlers) {
- .load = main_window_load,
- .unload = main_window_unload,
- });
- window_stack_push(s_main_window, true);
- // Register with Event Services
- tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
- }
- static void deinit() {
- window_destroy(s_main_window);
- }
- int main() {
- init();
- app_event_loop();
- deinit();
- }
Advertisement
Add Comment
Please, Sign In to add comment