Skip to content

Commit

Permalink
made caching not happen sooner than needed when switching faces
Browse files Browse the repository at this point in the history
  • Loading branch information
voloved committed Oct 27, 2024
1 parent cd71d76 commit c30b120
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
31 changes: 23 additions & 8 deletions movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void * watch_face_contexts[MOVEMENT_NUM_FACES];
watch_date_time_t scheduled_tasks[MOVEMENT_NUM_FACES];
const int32_t movement_le_inactivity_deadlines[8] = {INT_MAX, 600, 3600, 7200, 21600, 43200, 86400, 604800};
const int16_t movement_timeout_inactivity_deadlines[4] = {60, 120, 300, 1800};
static watch_date_time_t _dst_last_cache;
movement_event_t event;

int8_t _movement_dst_offset_cache[NUM_ZONE_NAMES] = {0};
Expand Down Expand Up @@ -115,11 +116,10 @@ static watch_date_time_t _movement_convert_udate_to_date_time(udatetime_t date_t
};
}

static bool _movement_update_dst_offset_cache(void) {
static bool _movement_update_dst_offset_cache(watch_date_time_t system_date_time) {
uzone_t local_zone;
udatetime_t udate_time;
bool dst_changed = false;
watch_date_time_t system_date_time = watch_rtc_get_date_time();

for (uint8_t i = 0; i < NUM_ZONE_NAMES; i++) {
unpack_zone(&zone_defns[i], "", &local_zone);
Expand All @@ -140,7 +140,7 @@ static bool _movement_update_dst_offset_cache(void) {
_movement_dst_offset_cache[i] = TIMEZONE_DOES_NOT_OBSERVE;
}
}

_dst_last_cache.reg = system_date_time.reg;
return dst_changed;
}

Expand Down Expand Up @@ -206,7 +206,8 @@ static inline void _movement_disable_fast_tick_if_possible(void) {
}

static void _movement_handle_top_of_minute(void) {
watch_date_time_t date_time = watch_rtc_get_date_time();
watch_date_time_t utc_now = movement_get_utc_date_time();
watch_date_time_t date_time = watch_utility_date_time_convert_zone(utc_now, 0, movement_get_current_timezone_offset());

#ifdef HAS_ACCELEROMETER
// every minute, we want to log whether the accelerometer is asleep or awake.
Expand All @@ -215,7 +216,7 @@ static void _movement_handle_top_of_minute(void) {

// update the DST offset cache if the current time matches the DST minute, hour, and month
if (_movement_check_dst_changeover_occurring_now(date_time)) {
_movement_update_dst_offset_cache();
_movement_update_dst_offset_cache(utc_now);
}

for(uint8_t i = 0; i < MOVEMENT_NUM_FACES; i++) {
Expand Down Expand Up @@ -466,12 +467,26 @@ watch_date_time_t movement_get_utc_date_time(void) {
}

bool movement_update_dst_offset_cache(void) {
return _movement_update_dst_offset_cache();
return _movement_update_dst_offset_cache(movement_get_utc_date_time());
}

bool movement_update_dst_offset_cache_if_needed(watch_date_time_t utc_now) {
const uint8_t min_to_trigger = 30; // We want to check every half-hour, but no need to cache more than once in a hour-hour.
// Checks if the last 20 bits don't match (hours, days, months, years)
uint32_t ydmh_compare = (utc_now.reg ^ _dst_last_cache.reg) & 0xFFFFF;
if(ydmh_compare != 0) return _movement_update_dst_offset_cache(utc_now);
int8_t delta_actual = utc_now.unit.minute - _dst_last_cache.unit.minute;
if (delta_actual == 0) return false;
int8_t delta_min = min_to_trigger - (_dst_last_cache.unit.minute % min_to_trigger);
if (delta_actual >= delta_min || delta_actual < 0) return _movement_update_dst_offset_cache(utc_now);
return false;
}

watch_date_time_t movement_get_date_time_in_zone(uint8_t zone_index) {
watch_date_time_t date_time = movement_get_utc_date_time();
int32_t offset = movement_get_current_timezone_offset_for_zone(zone_index);
return watch_utility_date_time_convert_zone(watch_rtc_get_date_time(), 0, offset);
movement_update_dst_offset_cache_if_needed(date_time);
return watch_utility_date_time_convert_zone(date_time, 0, offset);
}

watch_date_time_t movement_get_local_date_time(void) {
Expand Down Expand Up @@ -644,7 +659,7 @@ void app_setup(void) {
}

// populate the DST offset cache
_movement_update_dst_offset_cache();
_movement_update_dst_offset_cache(movement_get_utc_date_time());

#if __EMSCRIPTEN__
int32_t time_zone_offset = EM_ASM_INT({
Expand Down
1 change: 1 addition & 0 deletions movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ watch_date_time_t movement_get_utc_date_time(void);
watch_date_time_t movement_get_local_date_time(void);
watch_date_time_t movement_get_date_time_in_zone(uint8_t zone_index);
bool movement_update_dst_offset_cache(void);
bool movement_update_dst_offset_cache_if_needed(watch_date_time_t utc_now);

void movement_set_local_date_time(watch_date_time_t date_time);

Expand Down
2 changes: 1 addition & 1 deletion movement/watch_faces/clock/world_clock2_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void world_clock2_face_activate(void *context)
movement_request_tick_frequency(4);
break;
}
movement_update_dst_offset_cache();
movement_update_dst_offset_cache_if_needed(movement_get_utc_date_time());
refresh_face = true;
}

Expand Down
1 change: 0 additions & 1 deletion movement/watch_faces/settings/finetune_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,4 @@ void finetune_face_resign(void *context) {
if (total_adjustment != 0) {
finetune_update_correction_time();
}
movement_update_dst_offset_cache();
}
4 changes: 2 additions & 2 deletions movement/watch_faces/settings/set_time_hackwatch_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ bool set_time_hackwatch_face_loop(movement_event_t event, void *context) {
}

void set_time_hackwatch_face_resign(void *context) {
(void) context;
uint8_t current_page = *((uint8_t *)context);
watch_set_led_off();
movement_store_settings();
movement_update_dst_offset_cache();
if (current_page == 6) movement_update_dst_offset_cache();
}
2 changes: 1 addition & 1 deletion watch-faces/clock/world_clock_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void world_clock_face_activate(void *context) {
world_clock_state_t *state = (world_clock_state_t *)context;

state->current_screen = 0;
movement_update_dst_offset_cache();
movement_update_dst_offset_cache_if_needed(movement_get_utc_date_time());
_update_timezone_offset(state);

if (watch_sleep_animation_is_running()) watch_stop_sleep_animation();
Expand Down
2 changes: 1 addition & 1 deletion watch-faces/complication/sunrise_sunset_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void sunrise_sunset_face_activate(void *context) {
movement_location_t movement_location = (movement_location_t) watch_get_backup_data(1);
state->working_latitude = _sunrise_sunset_face_struct_from_latlon(movement_location.bit.latitude);
state->working_longitude = _sunrise_sunset_face_struct_from_latlon(movement_location.bit.longitude);
movement_update_dst_offset_cache();
movement_update_dst_offset_cache_if_needed(movement_get_utc_date_time());
}

bool sunrise_sunset_face_loop(movement_event_t event, void *context) {
Expand Down
4 changes: 2 additions & 2 deletions watch-faces/settings/set_time_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ bool set_time_face_loop(movement_event_t event, void *context) {
}

void set_time_face_resign(void *context) {
(void) context;
uint8_t current_page = *((uint8_t *)context);
watch_set_led_off();
movement_store_settings();
movement_update_dst_offset_cache();
if (current_page == SET_TIME_TZ) movement_update_dst_offset_cache();
}

0 comments on commit c30b120

Please sign in to comment.