From 0bb8457f2bf08a679b8d5d145a71d11b03c42d04 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 28 Jun 2026 17:26:37 -0400 Subject: [PATCH] apply_settings cleanup --- src/c/main.c | 90 ++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 59 deletions(-) diff --git a/src/c/main.c b/src/c/main.c index 5238dc0..9594a20 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -58,6 +58,12 @@ static const uint8_t s_bar_height = 8; static const uint8_t s_bar_width = 17; static const uint8_t s_bar_spacing = s_bar_height + 1; +// resource ID arrays for digit bitmaps +static const uint32_t s_digit_resource_ids[10] = {RESOURCE_ID_0, RESOURCE_ID_1, RESOURCE_ID_2, RESOURCE_ID_3, RESOURCE_ID_4, + RESOURCE_ID_5, RESOURCE_ID_6, RESOURCE_ID_7, RESOURCE_ID_8, RESOURCE_ID_9}; +static const uint32_t s_digit_mini_resource_ids[10] = {RESOURCE_ID_0_M, RESOURCE_ID_1_M, RESOURCE_ID_2_M, RESOURCE_ID_3_M, RESOURCE_ID_4_M, + RESOURCE_ID_5_M, RESOURCE_ID_6_M, RESOURCE_ID_7_M, RESOURCE_ID_8_M, RESOURCE_ID_9_M}; + static void toggle_fuel_warning_state(bool enable_warning, bool force) { if (!force && (enable_warning == s_fuel_is_warning)) { return; @@ -304,6 +310,15 @@ static void peek_did_change(void *context) { } } +static void recolor_digit_bitmaps(GBitmap **bitmaps, GColor *applied, GColor new_color) { + if (applied->argb == new_color.argb) + return; + for (int i = 0; i < 10; ++i) { + replace_gbitmap_color(*applied, new_color, bitmaps[i], NULL); + } + *applied = new_color; +} + static void apply_settings() { bool force_update_minutes = false; @@ -373,50 +388,26 @@ static void apply_settings() { // 1. check if the minute bitmaps are loaded and equal the hours if (s_minute_bitmaps[0] && (s_minute_bitmaps[0] != s_hour_bitmaps[0])) { // 2a. if loaded and unequal, we can just change the color of the loaded bitmaps - if (s_applied_minute.argb != settings.ColorMinutes.argb) { - for (int i = 0; i < 10; ++i) { - replace_gbitmap_color(s_applied_minute, settings.ColorMinutes, s_minute_bitmaps[i], NULL); - } - s_applied_minute = settings.ColorMinutes; - } + recolor_digit_bitmaps(s_minute_bitmaps, &s_applied_minute, settings.ColorMinutes); } else { - // 2b. otherwise, we must load them and set the colors now (they do not change with BT status so this must be managed here) - s_minute_bitmaps[0] = gbitmap_create_with_resource(RESOURCE_ID_0); - s_minute_bitmaps[1] = gbitmap_create_with_resource(RESOURCE_ID_1); - s_minute_bitmaps[2] = gbitmap_create_with_resource(RESOURCE_ID_2); - s_minute_bitmaps[3] = gbitmap_create_with_resource(RESOURCE_ID_3); - s_minute_bitmaps[4] = gbitmap_create_with_resource(RESOURCE_ID_4); - s_minute_bitmaps[5] = gbitmap_create_with_resource(RESOURCE_ID_5); - s_minute_bitmaps[6] = gbitmap_create_with_resource(RESOURCE_ID_6); - s_minute_bitmaps[7] = gbitmap_create_with_resource(RESOURCE_ID_7); - s_minute_bitmaps[8] = gbitmap_create_with_resource(RESOURCE_ID_8); - s_minute_bitmaps[9] = gbitmap_create_with_resource(RESOURCE_ID_9); - s_applied_minute = GColorWhite; - if (s_applied_minute.argb != settings.ColorMinutes.argb) { - for (int i = 0; i < 10; ++i) { - replace_gbitmap_color(s_applied_minute, settings.ColorMinutes, s_minute_bitmaps[i], NULL); - } - s_applied_minute = settings.ColorMinutes; + // 2b. otherwise, load them and set the colors now + for (int i = 0; i < 10; ++i) { + s_minute_bitmaps[i] = gbitmap_create_with_resource(s_digit_resource_ids[i]); } + s_applied_minute = GColorWhite; + recolor_digit_bitmaps(s_minute_bitmaps, &s_applied_minute, settings.ColorMinutes); force_update_minutes = true; } } else { - // if hours and minutes must be the same color... - // 1. check if the minute bitmaps equal the hours if (s_minute_bitmaps[0] != s_hour_bitmaps[0]) { - // 2. if not, check if they have been loaded if (s_minute_bitmaps[0]) { - // 3a. if they have been, destroy them and set them as pointers to the hour bitmaps for (int i = 0; i < 10; ++i) { gbitmap_destroy(s_minute_bitmaps[i]); - s_minute_bitmaps[i] = s_hour_bitmaps[i]; - } - } else { - // 3b. otherwise, just point them to the hour bitmaps - for (int i = 0; i < 10; ++i) { - s_minute_bitmaps[i] = s_hour_bitmaps[i]; } } + for (int i = 0; i < 10; ++i) { + s_minute_bitmaps[i] = s_hour_bitmaps[i]; + } } } // re-sub bt tracking (will update hour/month color; also minute/day if not unique) @@ -450,25 +441,13 @@ static void apply_settings() { unobstructed_area_service_unsubscribe(); if (settings.EnablePeek) { if (!s_minute_bitmaps_mini[0]) { - s_minute_bitmaps_mini[0] = gbitmap_create_with_resource(RESOURCE_ID_0_M); - s_minute_bitmaps_mini[1] = gbitmap_create_with_resource(RESOURCE_ID_1_M); - s_minute_bitmaps_mini[2] = gbitmap_create_with_resource(RESOURCE_ID_2_M); - s_minute_bitmaps_mini[3] = gbitmap_create_with_resource(RESOURCE_ID_3_M); - s_minute_bitmaps_mini[4] = gbitmap_create_with_resource(RESOURCE_ID_4_M); - s_minute_bitmaps_mini[5] = gbitmap_create_with_resource(RESOURCE_ID_5_M); - s_minute_bitmaps_mini[6] = gbitmap_create_with_resource(RESOURCE_ID_6_M); - s_minute_bitmaps_mini[7] = gbitmap_create_with_resource(RESOURCE_ID_7_M); - s_minute_bitmaps_mini[8] = gbitmap_create_with_resource(RESOURCE_ID_8_M); - s_minute_bitmaps_mini[9] = gbitmap_create_with_resource(RESOURCE_ID_9_M); + for (int i = 0; i < 10; ++i) { + s_minute_bitmaps_mini[i] = gbitmap_create_with_resource(s_digit_mini_resource_ids[i]); + } s_applied_minute_mini = GColorWhite; } //// set bitmap color - if (s_applied_minute_mini.argb != settings.ColorMinutes.argb) { - for (int i = 0; i < 10; ++i) { - replace_gbitmap_color(s_applied_minute_mini, settings.ColorMinutes, s_minute_bitmaps_mini[i], NULL); - } - s_applied_minute_mini = settings.ColorMinutes; - } + recolor_digit_bitmaps(s_minute_bitmaps_mini, &s_applied_minute_mini, settings.ColorMinutes); force_update_minutes = true; // mg handling @@ -712,16 +691,9 @@ static void init() { } // load required bitmaps (hours and letters) - s_hour_bitmaps[0] = gbitmap_create_with_resource(RESOURCE_ID_0); - s_hour_bitmaps[1] = gbitmap_create_with_resource(RESOURCE_ID_1); - s_hour_bitmaps[2] = gbitmap_create_with_resource(RESOURCE_ID_2); - s_hour_bitmaps[3] = gbitmap_create_with_resource(RESOURCE_ID_3); - s_hour_bitmaps[4] = gbitmap_create_with_resource(RESOURCE_ID_4); - s_hour_bitmaps[5] = gbitmap_create_with_resource(RESOURCE_ID_5); - s_hour_bitmaps[6] = gbitmap_create_with_resource(RESOURCE_ID_6); - s_hour_bitmaps[7] = gbitmap_create_with_resource(RESOURCE_ID_7); - s_hour_bitmaps[8] = gbitmap_create_with_resource(RESOURCE_ID_8); - s_hour_bitmaps[9] = gbitmap_create_with_resource(RESOURCE_ID_9); + for (int i = 0; i < 10; ++i) { + s_hour_bitmaps[i] = gbitmap_create_with_resource(s_digit_resource_ids[i]); + } s_c_icon = gbitmap_create_with_resource(RESOURCE_ID_C); s_h_icon = gbitmap_create_with_resource(RESOURCE_ID_H); s_e_icon = gbitmap_create_with_resource(RESOURCE_ID_E);