From: Ruben Nic Date: Sun, 6 Jul 2025 15:52:43 +0000 (-0400) Subject: Port close enough clock face to second-movement (#31) X-Git-Url: https://git.earman.xyz/?a=commitdiff_plain;h=55f8eaa257ae618f729f02201ecfb25b6d64c978;p=sensor-watch.git Port close enough clock face to second-movement (#31) * Port over close enough clock face * Clean up the code and comments --- diff --git a/legacy/watch_faces/clock/close_enough_clock_face.c b/legacy/watch_faces/clock/close_enough_clock_face.c deleted file mode 100644 index 99dec5c..0000000 --- a/legacy/watch_faces/clock/close_enough_clock_face.c +++ /dev/null @@ -1,229 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2024 Ruben Nic - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include "close_enough_clock_face.h" -#include "watch.h" -#include "watch_utility.h" - -const char *words[12] = { - " ", - " 5", - "10", - "15", - "20", - "25", - "30", - "35", - "40", - "45", - "50", - "55", -}; - -static const char *past_word = " P"; -static const char *to_word = " 2"; -static const char *oclock_word = "OC"; - -// sets when in the five minute period we switch -// from "X past HH" to "X to HH+1" -static const int hour_switch_index = 8; - -static void _update_alarm_indicator(bool settings_alarm_enabled, close_enough_clock_state_t *state) { - state->alarm_enabled = settings_alarm_enabled; - if (state->alarm_enabled) { - watch_set_indicator(WATCH_INDICATOR_BELL); - } else { - watch_clear_indicator(WATCH_INDICATOR_BELL); - }; -} - -void close_enough_clock_face_setup(uint8_t watch_face_index, void ** context_ptr) { - (void) watch_face_index; - - if (*context_ptr == NULL) { - *context_ptr = malloc(sizeof(close_enough_clock_state_t)); - } -} - -void close_enough_clock_face_activate(void *context) { - close_enough_clock_state_t *state = (close_enough_clock_state_t *)context; - - if (watch_sleep_animation_is_running()) { - watch_stop_sleep_animation(); - } - - if (movement_clock_mode_24h()) { - watch_set_indicator(WATCH_INDICATOR_24H); - } - - // show alarm indicator if there is an active alarm - _update_alarm_indicator(movement_alarm_enabled(), state); - - // this ensures that none of the five_minute_periods will match, so we always rerender when the face activates - state->prev_five_minute_period = -1; - state->prev_min_checked = -1; -} - -bool close_enough_clock_face_loop(movement_event_t event, void *context) { - close_enough_clock_state_t *state = (close_enough_clock_state_t *)context; - - char buf[11]; - watch_date_time_t date_time; - bool show_next_hour = false; - int prev_five_minute_period; - int prev_min_checked; - int close_enough_hour; - - switch (event.event_type) { - case EVENT_ACTIVATE: - case EVENT_TICK: - case EVENT_LOW_ENERGY_UPDATE: - date_time = watch_rtc_get_date_time(); - prev_five_minute_period = state->prev_five_minute_period; - prev_min_checked = state->prev_min_checked; - - // check the battery voltage once a day... - if (date_time.unit.day != state->last_battery_check) { - state->last_battery_check = date_time.unit.day; - uint16_t voltage = watch_get_vcc_voltage(); - // 2.2 volts will happen when the battery has maybe 5-10% remaining? - // we can refine this later. - state->battery_low = (voltage < 2200); - } - - // ...and set the LAP indicator if low. - if (state->battery_low) { - watch_set_indicator(WATCH_INDICATOR_LAP); - } - - // same minute, skip update - if (date_time.unit.minute == prev_min_checked) { - break; - } else { - state->prev_min_checked = date_time.unit.minute; - } - - int five_minute_period = (date_time.unit.minute / 5) % 12; - - // If we are 60% to the next 5 interval, move up to the next period - if (fmodf(date_time.unit.minute / 5.0f, 1.0f) > 0.5f) { - // If we are on the last 5 interval and moving to the next period we need to display the next hour because we are wrapping around - if (five_minute_period == 11) { - show_next_hour = true; - } - - five_minute_period = (five_minute_period + 1) % 12; - } - - // same five_minute_period, skip update - if (five_minute_period == prev_five_minute_period) { - break; - } - - // we don't want to modify date_time.unit.hour just in case other watch faces use it - close_enough_hour = date_time.unit.hour; - - // move from "MM(mins) P HH" to "MM(mins) 2 HH+1" - if (five_minute_period >= hour_switch_index || show_next_hour) { - close_enough_hour = (close_enough_hour + 1) % 24; - } - - if (!movement_clock_mode_24h()) { - // if we are in 12 hour mode, do some cleanup. - if (close_enough_hour < 12) { - watch_clear_indicator(WATCH_INDICATOR_PM); - } else { - watch_set_indicator(WATCH_INDICATOR_PM); - } - - close_enough_hour %= 12; - if (close_enough_hour == 0) { - close_enough_hour = 12; - } - - date_time.unit.hour %= 12; - if (date_time.unit.hour == 0) { - date_time.unit.hour = 12; - } - } - - char first_word[3]; - char second_word[3]; - char third_word[3]; - if (five_minute_period == 0) { // "HH OC", - sprintf(first_word, "%2d", close_enough_hour); - strncpy(second_word, words[five_minute_period], 3); - strncpy(third_word, oclock_word, 3); - } else { - int words_length = sizeof(words) / sizeof(words[0]); - - strncpy( - first_word, - five_minute_period >= hour_switch_index ? - words[words_length - five_minute_period] : - words[five_minute_period], - 3 - ); - strncpy( - second_word, - five_minute_period >= hour_switch_index ? - to_word : past_word, - 3 - ); - sprintf(third_word, "%2d", close_enough_hour); - } - - sprintf( - buf, - "%s%2d%s%s%s", - watch_utility_get_weekday(date_time), - date_time.unit.day, - first_word, - second_word, - third_word - ); - - watch_display_string(buf, 0); - state->prev_five_minute_period = five_minute_period; - - // handle alarm indicator - if (state->alarm_enabled != movement_alarm_enabled()) { - _update_alarm_indicator(movement_alarm_enabled(), state); - } - - break; - - default: - return movement_default_loop_handler(event); - } - - return true; -} - -void close_enough_clock_face_resign(void *context) { - (void) context; -} diff --git a/legacy/watch_faces/clock/close_enough_clock_face.h b/legacy/watch_faces/clock/close_enough_clock_face.h deleted file mode 100644 index e693043..0000000 --- a/legacy/watch_faces/clock/close_enough_clock_face.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2024 Ruben Nic - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#ifndef CLOSE_ENOUGH_CLOCK_FACE_H_ -#define CLOSE_ENOUGH_CLOCK_FACE_H_ - -/* - * CLOSE ENOUGH CLOCK FACE - * - * Displays the current time; but only in periods of 5. - * Just in the in the formats of: - * - "10 past 5" - * - "15 to 7" - * - "6 o'clock" - * - */ - -#include "movement.h" - -typedef struct { - int prev_five_minute_period; - int prev_min_checked; - uint8_t last_battery_check; - bool battery_low; - bool alarm_enabled; -} close_enough_clock_state_t; - -void close_enough_clock_face_setup(uint8_t watch_face_index, void ** context_ptr); -void close_enough_clock_face_activate(void *context); -bool close_enough_clock_face_loop(movement_event_t event, void *context); -void close_enough_clock_face_resign(void *context); - -#define close_enough_clock_face ((const watch_face_t){ \ - close_enough_clock_face_setup, \ - close_enough_clock_face_activate, \ - close_enough_clock_face_loop, \ - close_enough_clock_face_resign, \ - NULL, \ -}) - -#endif // CLOSE_ENOUGH_CLOCK_FACE_H_ diff --git a/movement_config.h b/movement_config.h index 0da8da4..af2da8f 100644 --- a/movement_config.h +++ b/movement_config.h @@ -28,6 +28,7 @@ #include "movement_faces.h" const watch_face_t watch_faces[] = { + close_enough_face, clock_face, world_clock_face, sunrise_sunset_face, diff --git a/movement_faces.h b/movement_faces.h index a275527..6e12fe3 100644 --- a/movement_faces.h +++ b/movement_faces.h @@ -51,6 +51,7 @@ #include "nanosec_face.h" #include "mars_time_face.h" #include "peek_memory_face.h" +#include "close_enough_face.h" #include "tarot_face.h" #include "kitchen_conversions_face.h" #include "periodic_table_face.h" diff --git a/watch-faces.mk b/watch-faces.mk index 2dbc2c5..bcb168b 100644 --- a/watch-faces.mk +++ b/watch-faces.mk @@ -28,6 +28,7 @@ SRCS += \ ./watch-faces/settings/nanosec_face.c \ ./watch-faces/io/chirpy_demo_face.c \ ./watch-faces/io/irda_upload_face.c \ + ./watch-faces/clock/close_enough_face.c \ ./watch-faces/complication/tarot_face.c \ ./watch-faces/complication/kitchen_conversions_face.c \ ./watch-faces/complication/periodic_table_face.c \ diff --git a/watch-faces/clock/close_enough_face.c b/watch-faces/clock/close_enough_face.c new file mode 100644 index 0000000..a0bdb6f --- /dev/null +++ b/watch-faces/clock/close_enough_face.c @@ -0,0 +1,250 @@ +/* + * MIT License + * + * Copyright (c) 2025 Ruben Nic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include "close_enough_face.h" +#include "watch.h" +#include "watch_utility.h" +#include "watch_common_display.h" + +// 2.4 volts seems to offer adequate warning of a low battery condition? +// refined based on user reports and personal observations; may need further adjustment. +#ifndef CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD +#define CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD 2400 +#endif + +static const char *words[12] = { + " ", + " 5", + "10", + "15", + "20", + "25", + "30", + "35", + "40", + "45", + "50", + "55", +}; + +static const char *past_word = " P"; +static const char *to_word = " 2"; +static const char *oclock_word = "OC"; + +// sets when in the five minute period we switch +// from "X past HH" to "X to HH+1" +static const int hour_switch_index = 8; + +static void clock_stop_tick_tock_animation(void) { + if (watch_sleep_animation_is_running()) { + watch_stop_sleep_animation(); + watch_stop_blink(); + } +} + +static void clock_indicate(watch_indicator_t indicator, bool on) { + if (on) { + watch_set_indicator(indicator); + } else { + watch_clear_indicator(indicator); + } +} + +void close_enough_face_setup(uint8_t watch_face_index, void ** context_ptr) { + (void) watch_face_index; + if (*context_ptr == NULL) { + *context_ptr = malloc(sizeof(close_enough_state_t)); + memset(*context_ptr, 0, sizeof(close_enough_state_t)); + } +} + +void close_enough_face_activate(void *context) { + close_enough_state_t *state = (close_enough_state_t *)context; + + clock_stop_tick_tock_animation(); + + clock_indicate(WATCH_INDICATOR_BELL, movement_alarm_enabled()); + clock_indicate(WATCH_INDICATOR_24H, !!movement_clock_mode_24h()); + + // this ensures that none of the five_minute_periods will match, so we always rerender when the face activates + state->prev_five_minute_period = -1; + state->prev_min_checked = -1; +} + +static void clock_check_battery_periodically(close_enough_state_t *state) { + // If the battery is low, skip the check. We have already indicated it. + if (state->battery_low) { + return; + } + + watch_date_time_t date_time = movement_get_local_date_time(); + + if (date_time.unit.day == state->last_battery_check) { return; } + + state->last_battery_check = date_time.unit.day; + + uint16_t voltage = watch_get_vcc_voltage(); + + state->battery_low = voltage < CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD; + + if (watch_get_lcd_type() == WATCH_LCD_TYPE_CUSTOM) { + // interlocking arrows imply "exchange" the battery. + clock_indicate(WATCH_INDICATOR_ARROWS, state->battery_low); + } else { + // LAP indicator on classic LCD is an adequate fallback. + clock_indicate(WATCH_INDICATOR_LAP, state->battery_low); + } +} + +bool close_enough_face_loop(movement_event_t event, void *context) { + close_enough_state_t *state = (close_enough_state_t *)context; + watch_date_time_t date_time; + bool show_next_hour = false; + int prev_five_minute_period; + int prev_min_checked; + int close_enough_hour; + + switch (event.event_type) { + case EVENT_ACTIVATE: + case EVENT_TICK: + case EVENT_LOW_ENERGY_UPDATE: + date_time = movement_get_local_date_time(); + prev_five_minute_period = state->prev_five_minute_period; + prev_min_checked = state->prev_min_checked; + + // check the battery voltage once a day... + clock_check_battery_periodically(state); + + // same minute, skip update + if (date_time.unit.minute == prev_min_checked) { + break; + } else { + state->prev_min_checked = date_time.unit.minute; + } + + int five_minute_period = (date_time.unit.minute / 5) % 12; + + // Move to next five minute period if we are above 50% through the current five minute period (we are only checking the remainder) + if (fmodf(date_time.unit.minute / 5.0f, 1.0f) > 0.5f) { + // If we are on the last 5 interval and moving to the next period we need to display the next hour + if (five_minute_period == 11) { + show_next_hour = true; + } + + five_minute_period = (five_minute_period + 1) % 12; + } + + // same five_minute_period, skip update + if (five_minute_period == prev_five_minute_period) { + break; + } + + close_enough_hour = date_time.unit.hour; + + // move from "MM P HH" to "MM 2 HH+1" + if (five_minute_period >= hour_switch_index || show_next_hour) { + close_enough_hour = (close_enough_hour + 1) % 24; + show_next_hour = true; + } + + if (movement_clock_mode_24h() != MOVEMENT_CLOCK_MODE_24H) { + // if we are at "MM 2 12", don't show the PM indicator + if (close_enough_hour < 12 || show_next_hour) { + watch_clear_indicator(WATCH_INDICATOR_PM); + } else { + watch_set_indicator(WATCH_INDICATOR_PM); + } + + close_enough_hour %= 12; + if (close_enough_hour == 0) { + close_enough_hour = 12; + } + } + + char first_word[3]; + char second_word[3]; + char third_word[3]; + if (five_minute_period == 0) { // " HH OC", + sprintf(first_word, " ", 3); + sprintf(second_word, "%2d", close_enough_hour); + strncpy(third_word, oclock_word, 3); + } else { // "MM P HH" or "MM 2 HH+1" + int words_length = sizeof(words) / sizeof(words[0]); + + strncpy( + first_word, + show_next_hour ? + words[words_length - five_minute_period] : + words[five_minute_period], + 3 + ); + strncpy( + second_word, + show_next_hour ? to_word : past_word, + 3 + ); + sprintf(third_word, "%2d", close_enough_hour); + } + + watch_display_text_with_fallback( + WATCH_POSITION_TOP_LEFT, + watch_utility_get_long_weekday(date_time), watch_utility_get_weekday(date_time) + ); + + char day_buf[2 + 1]; + sprintf(day_buf, "%2d", date_time.unit.day); + watch_display_text( + WATCH_POSITION_TOP_RIGHT, + day_buf + ); + + char words_buf[6 + 1]; + sprintf(words_buf, + "%s%s%s", + first_word, + second_word, + third_word + ); + watch_display_text( + WATCH_POSITION_BOTTOM, + words_buf + ); + + state->prev_five_minute_period = five_minute_period; + break; + + default: + return movement_default_loop_handler(event); + } + + return true; +} + +void close_enough_face_resign(void *context) { + (void) context; +} + diff --git a/watch-faces/clock/close_enough_face.h b/watch-faces/clock/close_enough_face.h new file mode 100644 index 0000000..ecd4118 --- /dev/null +++ b/watch-faces/clock/close_enough_face.h @@ -0,0 +1,57 @@ +/* + * MIT License + * + * Copyright (c) 2025 Ruben Nic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include "movement.h" + +/* + * CLOSE ENOUGH CLOCK FACE + * + * Displays the current time; but only in periods of 5. + * Some examples: + * - 5:10 is "10 past 5" displayed as "10 P 5" + * - 5:45 is "15 to 6" displayed as "15 2 6" + * - 6:00 is "6 o'clock" displayed as "6 OC" + */ + +typedef struct { + int prev_five_minute_period; + int prev_min_checked; + uint8_t last_battery_check; + bool battery_low; +} close_enough_state_t; + +void close_enough_face_setup(uint8_t watch_face_index, void ** context_ptr); +void close_enough_face_activate(void *context); +bool close_enough_face_loop(movement_event_t event, void *context); +void close_enough_face_resign(void *context); + +#define close_enough_face ((const watch_face_t){ \ + close_enough_face_setup, \ + close_enough_face_activate, \ + close_enough_face_loop, \ + close_enough_face_resign, \ + NULL, \ +})