From e50390b673fda13a31463509a0d26165f1a4d1ea Mon Sep 17 00:00:00 2001 From: David Volovskiy Date: Sat, 3 Aug 2024 09:33:49 -0400 Subject: [PATCH] DST roll is now a global variable in watch_rtc --- movement/movement.c | 20 +++++++++++++ movement/movement.h | 1 + movement/watch_faces/clock/clock_face.c | 29 +++++------------- .../clock/minute_repeater_decimal_face.c | 4 +-- .../clock/repetition_minute_face.c | 4 +-- .../clock/simple_clock_bin_led_face.c | 4 +-- .../watch_faces/clock/simple_clock_face.c | 30 +++++-------------- movement/watch_faces/settings/set_time_face.c | 1 - .../settings/set_time_hackwatch_face.c | 3 -- watch-library/hardware/watch/watch_rtc.c | 14 +++++++++ watch-library/shared/watch/watch_rtc.h | 6 ++++ watch-library/simulator/watch/watch_rtc.c | 14 +++++++++ 12 files changed, 75 insertions(+), 55 deletions(-) diff --git a/movement/movement.c b/movement/movement.c index 4afe273..eabc29c 100644 --- a/movement/movement.c +++ b/movement/movement.c @@ -430,6 +430,26 @@ uint8_t movement_claim_backup_register(void) { return movement_state.next_available_backup_register++; } +uint8_t check_and_act_on_daylight_savings(watch_date_time date_time) { + if (movement_state.settings.bit.dst_active) return date_time.unit.hour; + uint8_t dst_result = get_dst_status(date_time); + bool dst_skip_rolling_back = get_dst_skip_rolling_back(); + + if (dst_skip_rolling_back && (dst_result == DST_ENDED)) { + clear_dst_skip_rolling_back(); + } + else if (dst_result == DST_ENDING && !dst_skip_rolling_back) { + set_dst_skip_rolling_back(); + date_time.unit.hour = (date_time.unit.hour + 24 - 1) % 24; + watch_rtc_set_date_time(date_time); + } + else if (dst_result == DST_STARTING) { + date_time.unit.hour = (date_time.unit.hour + 1) % 24; + watch_rtc_set_date_time(date_time); + } + return date_time.unit.hour; +} + int16_t get_timezone_offset(uint8_t timezone_idx, watch_date_time date_time) { if (!movement_state.settings.bit.dst_active) return movement_timezone_offsets[timezone_idx]; if (dst_occurring(date_time)) diff --git a/movement/movement.h b/movement/movement.h index d0595fb..6dd38ba 100644 --- a/movement/movement.h +++ b/movement/movement.h @@ -313,6 +313,7 @@ void movement_play_alarm(void); void movement_play_alarm_beeps(uint8_t rounds, BuzzerNote alarm_note); uint8_t movement_claim_backup_register(void); +uint8_t check_and_act_on_daylight_savings(watch_date_time date_time); // Returns the currently set hour int16_t get_timezone_offset(uint8_t timezone_idx, watch_date_time date_time); #endif // MOVEMENT_H_ diff --git a/movement/watch_faces/clock/clock_face.c b/movement/watch_faces/clock/clock_face.c index f9ec58a..af601ce 100644 --- a/movement/watch_faces/clock/clock_face.c +++ b/movement/watch_faces/clock/clock_face.c @@ -280,31 +280,16 @@ void clock_face_resign(movement_settings_t *settings, void *context) { (void) context; } -static void check_and_act_on_daylight_savings(movement_settings_t *settings, watch_date_time date_time) { - if (!settings ->bit.dst_active) return; - uint8_t dst_result = get_dst_status(date_time); - - if (settings ->bit.dst_skip_rolling_back && dst_result == DST_ENDED) { - settings ->bit.dst_skip_rolling_back = false; - return; - } - else if (dst_result == DST_ENDING && !settings ->bit.dst_skip_rolling_back) { - settings ->bit.dst_skip_rolling_back = true; - date_time.unit.hour = (date_time.unit.hour + 24 - 1) % 24; - watch_rtc_set_date_time(date_time); - return; - } - else if (dst_result == DST_STARTING) { - date_time.unit.hour = (date_time.unit.hour + 1) % 24; - watch_rtc_set_date_time(date_time); - return; - } -} - bool clock_face_wants_background_task(movement_settings_t *settings, void *context) { + (void) settings; clock_state_t *state = (clock_state_t *) context; watch_date_time date_time = watch_rtc_get_date_time(); - check_and_act_on_daylight_savings(settings, date_time); + uint8_t hour_dst = check_and_act_on_daylight_savings(date_time); + if(hour_dst != date_time.unit.hour) { + char buf[3 + 1]; + sprintf(buf, "%2d", hour_dst); + watch_display_string(buf, 4); + } if (!state->time_signal_enabled) return false; return date_time.unit.minute == 0; diff --git a/movement/watch_faces/clock/minute_repeater_decimal_face.c b/movement/watch_faces/clock/minute_repeater_decimal_face.c index 2cedc30..ab0e6de 100644 --- a/movement/watch_faces/clock/minute_repeater_decimal_face.c +++ b/movement/watch_faces/clock/minute_repeater_decimal_face.c @@ -230,9 +230,9 @@ void minute_repeater_decimal_face_resign(movement_settings_t *settings, void *co bool minute_repeater_decimal_face_wants_background_task(movement_settings_t *settings, void *context) { (void) settings; minute_repeater_decimal_state_t *state = (minute_repeater_decimal_state_t *)context; - if (!state->signal_enabled) return false; - watch_date_time date_time = watch_rtc_get_date_time(); + check_and_act_on_daylight_savings(date_time); + if (!state->signal_enabled) return false; return date_time.unit.minute == 0; } diff --git a/movement/watch_faces/clock/repetition_minute_face.c b/movement/watch_faces/clock/repetition_minute_face.c index e9e5e31..1afc4ce 100644 --- a/movement/watch_faces/clock/repetition_minute_face.c +++ b/movement/watch_faces/clock/repetition_minute_face.c @@ -213,9 +213,9 @@ void repetition_minute_face_resign(movement_settings_t *settings, void *context) bool repetition_minute_face_wants_background_task(movement_settings_t *settings, void *context) { (void) settings; repetition_minute_state_t *state = (repetition_minute_state_t *)context; - if (!state->signal_enabled) return false; - watch_date_time date_time = watch_rtc_get_date_time(); + check_and_act_on_daylight_savings(date_time); + if (!state->signal_enabled) return false; return date_time.unit.minute == 0; } diff --git a/movement/watch_faces/clock/simple_clock_bin_led_face.c b/movement/watch_faces/clock/simple_clock_bin_led_face.c index cf39c18..1762cfa 100644 --- a/movement/watch_faces/clock/simple_clock_bin_led_face.c +++ b/movement/watch_faces/clock/simple_clock_bin_led_face.c @@ -214,9 +214,9 @@ void simple_clock_bin_led_face_resign(movement_settings_t *settings, void *conte bool simple_clock_bin_led_face_wants_background_task(movement_settings_t *settings, void *context) { (void) settings; simple_clock_bin_led_state_t *state = (simple_clock_bin_led_state_t *)context; - if (!state->signal_enabled) return false; - watch_date_time date_time = watch_rtc_get_date_time(); + check_and_act_on_daylight_savings(date_time); + if (!state->signal_enabled) return false; return date_time.unit.minute == 0; } diff --git a/movement/watch_faces/clock/simple_clock_face.c b/movement/watch_faces/clock/simple_clock_face.c index a7e08a9..3866e2c 100644 --- a/movement/watch_faces/clock/simple_clock_face.c +++ b/movement/watch_faces/clock/simple_clock_face.c @@ -150,31 +150,15 @@ void simple_clock_face_resign(movement_settings_t *settings, void *context) { (void) context; } -static void check_and_act_on_daylight_savings(movement_settings_t *settings, watch_date_time date_time) { - if (!settings ->bit.dst_active) return; - uint8_t dst_result = get_dst_status(date_time); - - if (settings ->bit.dst_skip_rolling_back && (dst_result == DST_ENDED)) { - settings ->bit.dst_skip_rolling_back = false; - return; - } - else if (dst_result == DST_ENDING && !settings ->bit.dst_skip_rolling_back) { - settings ->bit.dst_skip_rolling_back = true; - date_time.unit.hour = (date_time.unit.hour + 24 - 1) % 24; - watch_rtc_set_date_time(date_time); - return; - } - else if (dst_result == DST_STARTING) { - date_time.unit.hour = (date_time.unit.hour + 1) % 24; - watch_rtc_set_date_time(date_time); - return; - } -} - bool simple_clock_face_wants_background_task(movement_settings_t *settings, void *context) { simple_clock_state_t *state = (simple_clock_state_t *)context; - date_time = watch_rtc_get_date_time(); - check_and_act_on_daylight_savings(settings, date_time); + watch_date_time date_time = watch_rtc_get_date_time(); + uint8_t hour_dst = check_and_act_on_daylight_savings(date_time); + if(hour_dst != date_time.unit.hour) { + char buf[3 + 1]; + sprintf(buf, "%2d", hour_dst); + watch_display_string(buf, 4); + } if (!state->signal_enabled) return false; return date_time.unit.minute == 0; diff --git a/movement/watch_faces/settings/set_time_face.c b/movement/watch_faces/settings/set_time_face.c index 3f7d0c7..b16cb6d 100644 --- a/movement/watch_faces/settings/set_time_face.c +++ b/movement/watch_faces/settings/set_time_face.c @@ -72,7 +72,6 @@ static void _handle_alarm_button(movement_settings_t *settings, watch_date_time break; } watch_rtc_set_date_time(date_time); - settings->bit.dst_skip_rolling_back = false; } static void _abort_quick_ticks() { diff --git a/movement/watch_faces/settings/set_time_hackwatch_face.c b/movement/watch_faces/settings/set_time_hackwatch_face.c index be293ab..44ae04e 100644 --- a/movement/watch_faces/settings/set_time_hackwatch_face.c +++ b/movement/watch_faces/settings/set_time_hackwatch_face.c @@ -94,7 +94,6 @@ bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *s } date_time_settings.unit.second = 0; watch_rtc_set_date_time(date_time_settings); - settings->bit.dst_skip_rolling_back = false; } break; case EVENT_ALARM_BUTTON_DOWN: @@ -137,7 +136,6 @@ bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *s } if (current_page != 2) { // Do not set time when we are at seconds, it was already set previously watch_rtc_set_date_time(date_time_settings); - settings->bit.dst_skip_rolling_back = false; } break; @@ -183,7 +181,6 @@ bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *s } if (current_page != 2) { // Do not set time when we are at seconds, it was already set previously watch_rtc_set_date_time(date_time_settings); - settings->bit.dst_skip_rolling_back = false; } //TODO: Do not update whole RTC, just what we are changing break; diff --git a/watch-library/hardware/watch/watch_rtc.c b/watch-library/hardware/watch/watch_rtc.c index 93cb9f1..67ecb53 100644 --- a/watch-library/hardware/watch/watch_rtc.c +++ b/watch-library/hardware/watch/watch_rtc.c @@ -30,6 +30,19 @@ ext_irq_cb_t btn_alarm_callback; ext_irq_cb_t a2_callback; ext_irq_cb_t a4_callback; +static bool dst_skip_rolling_back; +bool get_dst_skip_rolling_back(void) { + return dst_skip_rolling_back; +} + +void set_dst_skip_rolling_back(void) { + dst_skip_rolling_back = true; +} + +void clear_dst_skip_rolling_back(void) { + dst_skip_rolling_back = false; +} + bool _watch_rtc_is_enabled(void) { return RTC->MODE2.CTRLA.bit.ENABLE; } @@ -60,6 +73,7 @@ void watch_rtc_set_date_time(watch_date_time date_time) { _sync_rtc(); // Double sync as without it at high Hz faces setting time is unrealiable (specifically, set_time_hackwatch) RTC->MODE2.CLOCK.reg = date_time.reg; _sync_rtc(); + clear_dst_skip_rolling_back(); } watch_date_time watch_rtc_get_date_time(void) { diff --git a/watch-library/shared/watch/watch_rtc.h b/watch-library/shared/watch/watch_rtc.h index 3e63bb5..a9134eb 100644 --- a/watch-library/shared/watch/watch_rtc.h +++ b/watch-library/shared/watch/watch_rtc.h @@ -157,5 +157,11 @@ void watch_rtc_enable(bool en); */ void watch_rtc_freqcorr_write(int16_t value, int16_t sign); +/** @brief Returns if we're currently at a point where the we rolled back for DST and need to ignore the next DST segment + */ +bool get_dst_skip_rolling_back(void); +void set_dst_skip_rolling_back(void); +void clear_dst_skip_rolling_back(void); + /// @} #endif diff --git a/watch-library/simulator/watch/watch_rtc.c b/watch-library/simulator/watch/watch_rtc.c index 2bb6074..9fe9e29 100644 --- a/watch-library/simulator/watch/watch_rtc.c +++ b/watch-library/simulator/watch/watch_rtc.c @@ -39,6 +39,19 @@ ext_irq_cb_t btn_alarm_callback; ext_irq_cb_t a2_callback; ext_irq_cb_t a4_callback; +static bool dst_skip_rolling_back; +bool get_dst_skip_rolling_back(void) { + return dst_skip_rolling_back; +} + +void set_dst_skip_rolling_back(void) { + dst_skip_rolling_back = true; +} + +void clear_dst_skip_rolling_back(void) { + dst_skip_rolling_back = false; +} + bool _watch_rtc_is_enabled(void) { return true; } @@ -57,6 +70,7 @@ void watch_rtc_set_date_time(watch_date_time date_time) { const date = new Date(year, month - 1, day, hour, minute, second); return date - Date.now(); }, date_time.reg); + clear_dst_skip_rolling_back(); } watch_date_time watch_rtc_get_date_time(void) {