Fix potential double swaps; general cleanup

This commit is contained in:
RandyTheSilly committed 2026-10-31 12:11:32 -04:00
1 parent 3591933b5b
commit bf879d5d4c
1 file changed
+49 -116
+49 -116
View File
@@ -1,9 +1,6 @@
#include "palette_manip.h"
#include <pebble.h>
// DEBUG memory usage layer
// static TextLayer *s_memory_layer;
// declare general statics
static Window *s_main_window;
static GFont s_custom_font;
@@ -129,69 +126,6 @@ static void destroy_animation_handler(Animation *animation, bool finished, void
animation_destroy(animation);
}
static void update_minute_1() {
// get a tm structure
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
// write the current hours and minutes into a buffer & format
static char s_time_buffer[8];
strftime(s_time_buffer, sizeof(s_time_buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", tick_time);
// write the current date into a buffer & format
static char s_date_buffer[16];
#if PBL_DISPLAY_WIDTH <= 144
strftime(s_date_buffer, sizeof(s_date_buffer), "%m%d", tick_time);
#else
strftime(s_date_buffer, sizeof(s_date_buffer), "%m.%d", tick_time);
#endif
// update layers
text_layer_set_text(s_time_layer, s_time_buffer);
text_layer_set_text(s_date_layer, s_date_buffer);
// create date/bt centering animations
//// count "1"s in s_time_buffer
static uint8_t ones;
uint8_t i, new_ones;
for (i = 0, new_ones = 0; s_time_buffer[i]; i++) {
new_ones += (s_time_buffer[i] == '1');
}
if (new_ones != ones) { // only animate if necessary
ones = new_ones;
//// calculate new positions
s_date_grect_b = GRect(s_ones_offset[ones], s_date_pos_y, s_date_bt_width, s_date_height);
s_bt_grect_b = GRect(s_bt_pos_x - s_ones_offset[ones], s_bt_pos_y, s_date_bt_width, s_bt_height);
if (s_first_tick_occurred) { // no need to animate before first tick, as positions are set in init() without animating
//// configure animations
////// date
PropertyAnimation *date_center_prop = property_animation_create_layer_frame(text_layer_get_layer(s_date_layer), &s_date_grect_a, &s_date_grect_b);
Animation *date_center_anim = property_animation_get_animation(date_center_prop);
animation_set_curve(date_center_anim, AnimationCurveEaseOut);
animation_set_duration(date_center_anim, s_animation_duration);
////// bt
PropertyAnimation *s_bt_center_prop = property_animation_create_layer_frame(bitmap_layer_get_layer(s_bt_layer), &s_bt_grect_a, &s_bt_grect_b);
Animation *bt_center_anim = property_animation_get_animation(s_bt_center_prop);
animation_set_curve(bt_center_anim, AnimationCurveEaseOut);
animation_set_duration(bt_center_anim, s_animation_duration);
////// create spawn animation (animate centering of both elements at the same time)
Animation *centering_spawn_anim = animation_spawn_create(date_center_anim, bt_center_anim, NULL);
////// set the handler to destroy the spawn animation (and its children) when finished
animation_set_handlers(centering_spawn_anim, (AnimationHandlers){.stopped = destroy_animation_handler}, NULL);
////// run centering spawn animation
animation_schedule(centering_spawn_anim);
}
////// update current date/bt positions
s_date_grect_a = s_date_grect_b;
s_bt_grect_a = s_bt_grect_b;
}
// DEBUG memory usage layer
// static char s_memory_buffer[32];
// snprintf(s_memory_buffer, sizeof(s_memory_buffer), "%d/%d", (int)heap_bytes_used(), (int)heap_bytes_free() + (int)heap_bytes_used());
// text_layer_set_text(s_memory_layer, s_memory_buffer);
}
static void schedule_guy_animation(Animation *raise_tmpl, Animation *out_tmpl, Animation *in_tmpl, AnimationStoppedHandler out_handler) {
Animation *raise = animation_clone(raise_tmpl);
Animation *out = animation_clone(out_tmpl);
@@ -241,12 +175,58 @@ static void update_minute_60_out_handler(Animation *animation, bool finished, vo
}
static void minute_handler(struct tm *tick_time, TimeUnits units_changed) {
update_minute_1();
// time/date updates
static char s_time_buffer[8];
strftime(s_time_buffer, sizeof(s_time_buffer), clock_is_24h_style() ? "%H:%M" : "%I:%M", tick_time);
static char s_date_buffer[16];
#if PBL_DISPLAY_WIDTH <= 144
strftime(s_date_buffer, sizeof(s_date_buffer), "%m%d", tick_time);
#else
strftime(s_date_buffer, sizeof(s_date_buffer), "%m.%d", tick_time);
#endif
text_layer_set_text(s_time_layer, s_time_buffer);
text_layer_set_text(s_date_layer, s_date_buffer);
// create date/bt centering animations
//// count "1"s in s_time_buffer
static uint8_t ones;
uint8_t i, new_ones;
for (i = 0, new_ones = 0; s_time_buffer[i]; i++) {
new_ones += (s_time_buffer[i] == '1');
}
if (new_ones != ones) { // only animate if necessary
ones = new_ones;
//// calculate new positions
s_date_grect_b = GRect(s_ones_offset[ones], s_date_pos_y, s_date_bt_width, s_date_height);
s_bt_grect_b = GRect(s_bt_pos_x - s_ones_offset[ones], s_bt_pos_y, s_date_bt_width, s_bt_height);
if (s_first_tick_occurred) { // no need to animate before first tick, as positions are set in init() without animating
//// configure animations
////// date
PropertyAnimation *date_center_prop = property_animation_create_layer_frame(text_layer_get_layer(s_date_layer), &s_date_grect_a, &s_date_grect_b);
Animation *date_center_anim = property_animation_get_animation(date_center_prop);
animation_set_curve(date_center_anim, AnimationCurveEaseOut);
animation_set_duration(date_center_anim, s_animation_duration);
////// bt
PropertyAnimation *s_bt_center_prop = property_animation_create_layer_frame(bitmap_layer_get_layer(s_bt_layer), &s_bt_grect_a, &s_bt_grect_b);
Animation *bt_center_anim = property_animation_get_animation(s_bt_center_prop);
animation_set_curve(bt_center_anim, AnimationCurveEaseOut);
animation_set_duration(bt_center_anim, s_animation_duration);
////// create spawn animation (animate centering of both elements at the same time)
Animation *centering_spawn_anim = animation_spawn_create(date_center_anim, bt_center_anim, NULL);
////// set the handler to destroy the spawn animation (and its children) when finished
animation_set_handlers(centering_spawn_anim, (AnimationHandlers){.stopped = destroy_animation_handler}, NULL);
////// run centering spawn animation
animation_schedule(centering_spawn_anim);
}
////// update current date/bt positions
s_date_grect_a = s_date_grect_b;
s_bt_grect_a = s_bt_grect_b;
}
// animate swaps on half-hour boundaries
if (!s_first_tick_occurred) {
s_first_tick_occurred = true;
return; // do not animate part swaps on first tick
}
if (tick_time->tm_min % 60 == 0) {
schedule_guy_animation(s_head_raise_template, s_head_out_template, s_head_in_template, update_minute_60_out_handler);
} else if (tick_time->tm_min % 30 == 0) {
@@ -284,9 +264,6 @@ static void main_window_load(Window *window) {
layer_add_child(window_layer, text_layer_get_layer(s_date_layer));
layer_add_child(window_layer, bitmap_layer_get_layer(s_bt_layer));
// DEBUG memory usage layer
// layer_add_child(window_layer, text_layer_get_layer(s_memory_layer));
// create animation templates
//// butt raise
PropertyAnimation *butt_raise_prop = property_animation_create_layer_frame(bitmap_layer_get_layer(s_guy_butt_layer), &s_guy_butt_grect_a, &s_guy_butt_grect_b);
@@ -327,7 +304,7 @@ static void main_window_unload(Window *window) {
bitmap_layer_destroy(s_guy_butt_layer);
layer_destroy(s_time_bar_layer);
// Destroy animation templates
// destroy animation templates
animation_destroy(s_butt_raise_template);
animation_destroy(s_butt_out_template);
animation_destroy(s_head_raise_template);
@@ -342,24 +319,6 @@ static void bluetooth_callback(bool connected) {
}
}
// DEBUG buttons
// static void up_single_click_handler(ClickRecognizerRef recognizer, void *context) {
// Window *window = (Window *)context;
// schedule_guy_animation(s_head_raise_template, s_head_out_template, s_head_in_template, update_minute_60_out_handler);
//}
// DEBUG buttons
// static void down_single_click_handler(ClickRecognizerRef recognizer, void *context) {
// Window *window = (Window *)context;
// schedule_guy_animation(s_butt_raise_template, s_butt_out_template, s_butt_in_template, update_minute_30_out_handler);
//}
// DEBUG buttons
// static void click_config_provider(Window *window) {
// window_single_click_subscribe(BUTTON_ID_UP, up_single_click_handler);
// window_single_click_subscribe(BUTTON_ID_DOWN, down_single_click_handler);
//}
// set up the app on launch (don't put app logic in here);
static void init() {
// seed RNG
@@ -450,16 +409,7 @@ static void init() {
s_bt_layer = bitmap_layer_create(s_bt_grect_a);
bitmap_layer_set_compositing_mode(s_bt_layer, GCompOpSet);
bitmap_layer_set_alignment(s_bt_layer, GAlignCenter);
//// DEBUG memory usage layer
// s_memory_layer = text_layer_create(GRect(0, PBL_DISPLAY_HEIGHT - 24, PBL_DISPLAY_WIDTH, 14));
// text_layer_set_background_color(s_memory_layer, GColorClear);
// text_layer_set_font(s_memory_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
// text_layer_set_text_color(s_memory_layer, GColorWhite);
// PBL_IF_ROUND_ELSE(text_layer_set_text_alignment(s_memory_layer, GTextAlignmentCenter), text_layer_set_text_alignment(s_memory_layer, GTextAlignmentLeft));
// make sure the time is displayed from the start
// and that the date/bt indicators are centered
update_minute_1();
//// register with ConnectionService (bluetooth status)
connection_service_subscribe((ConnectionHandlers){.pebble_app_connection_handler = bluetooth_callback});
//// force a check to display correct bt indicator to start
@@ -473,39 +423,22 @@ static void init() {
// register with TickTimerService
tick_timer_service_subscribe(MINUTE_UNIT, minute_handler);
// DEBUG buttons
// window_set_click_config_provider(s_main_window, (ClickConfigProvider)click_config_provider);
}
// free memory on app exit
static void deinit() {
// unsubscribe
tick_timer_service_unsubscribe();
// destroy layers
text_layer_destroy(s_time_layer);
text_layer_destroy(s_date_layer);
bitmap_layer_destroy(s_bt_layer);
// DEBUG memory usage layer
// text_layer_destroy(s_memory_layer);
// unload font
fonts_unload_custom_font(s_custom_font);
// destroy gbitmaps
gbitmap_destroy(s_head);
gbitmap_destroy(s_butt);
gbitmap_destroy(s_bt0_icon);
gbitmap_destroy(s_bt1_icon);
// write to persistent storage
persist_write_int(storage_key_head, s_head_index);
persist_write_int(storage_key_butt, s_butt_index);
persist_write_int(storage_key_color, s_random_color_current.argb);
// destroy window
window_destroy(s_main_window);
}