From 55f8eaa257ae618f729f02201ecfb25b6d64c978 Mon Sep 17 00:00:00 2001 From: Ruben Nic Date: Sun, 6 Jul 2025 11:52:43 -0400 Subject: [PATCH] Port close enough clock face to second-movement (#31) * Port over close enough clock face * Clean up the code and comments --- movement_config.h | 1 + movement_faces.h | 1 + watch-faces.mk | 1 + .../clock/close_enough_face.c | 171 ++++++++++-------- .../clock/close_enough_face.h | 41 ++--- 5 files changed, 117 insertions(+), 98 deletions(-) rename legacy/watch_faces/clock/close_enough_clock_face.c => watch-faces/clock/close_enough_face.c (55%) rename legacy/watch_faces/clock/close_enough_clock_face.h => watch-faces/clock/close_enough_face.h (64%) diff --git a/movement_config.h b/movement_config.h index 0da8da43..af2da8fb 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 a2755279..6e12fe31 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 2dbc2c52..bcb168b8 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/legacy/watch_faces/clock/close_enough_clock_face.c b/watch-faces/clock/close_enough_face.c similarity index 55% rename from legacy/watch_faces/clock/close_enough_clock_face.c rename to watch-faces/clock/close_enough_face.c index 99dec5c4..a0bdb6f4 100644 --- a/legacy/watch_faces/clock/close_enough_clock_face.c +++ b/watch-faces/clock/close_enough_face.c @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2024 Ruben Nic + * 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 @@ -25,11 +25,18 @@ #include #include #include -#include "close_enough_clock_face.h" +#include "close_enough_face.h" #include "watch.h" #include "watch_utility.h" +#include "watch_common_display.h" -const char *words[12] = { +// 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", @@ -52,46 +59,69 @@ static const char *oclock_word = "OC"; // 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; - +static void clock_stop_tick_tock_animation(void) { if (watch_sleep_animation_is_running()) { watch_stop_sleep_animation(); + watch_stop_blink(); } +} - if (movement_clock_mode_24h()) { - watch_set_indicator(WATCH_INDICATOR_24H); +static void clock_indicate(watch_indicator_t indicator, bool on) { + if (on) { + watch_set_indicator(indicator); + } else { + watch_clear_indicator(indicator); } +} - // show alarm indicator if there is an active alarm - _update_alarm_indicator(movement_alarm_enabled(), state); +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; } -bool close_enough_clock_face_loop(movement_event_t event, void *context) { - close_enough_clock_state_t *state = (close_enough_clock_state_t *)context; +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; + } - char buf[11]; + 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; @@ -102,23 +132,12 @@ bool close_enough_clock_face_loop(movement_event_t event, void *context) { case EVENT_ACTIVATE: case EVENT_TICK: case EVENT_LOW_ENERGY_UPDATE: - date_time = watch_rtc_get_date_time(); + 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... - 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); - } + clock_check_battery_periodically(state); // same minute, skip update if (date_time.unit.minute == prev_min_checked) { @@ -129,9 +148,9 @@ bool close_enough_clock_face_loop(movement_event_t event, void *context) { 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 + // 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 because we are wrapping around + // 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; } @@ -144,17 +163,17 @@ bool close_enough_clock_face_loop(movement_event_t event, void *context) { 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" + // 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()) { - // if we are in 12 hour mode, do some cleanup. - if (close_enough_hour < 12) { + 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); @@ -164,57 +183,58 @@ bool close_enough_clock_face_loop(movement_event_t event, void *context) { 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); + 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 { + } else { // "MM P HH" or "MM 2 HH+1" int words_length = sizeof(words) / sizeof(words[0]); strncpy( first_word, - five_minute_period >= hour_switch_index ? + show_next_hour ? words[words_length - five_minute_period] : words[five_minute_period], 3 ); strncpy( second_word, - five_minute_period >= hour_switch_index ? - to_word : past_word, + show_next_hour ? 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, + 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 + ); - 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: @@ -224,6 +244,7 @@ bool close_enough_clock_face_loop(movement_event_t event, void *context) { return true; } -void close_enough_clock_face_resign(void *context) { +void close_enough_face_resign(void *context) { (void) context; } + diff --git a/legacy/watch_faces/clock/close_enough_clock_face.h b/watch-faces/clock/close_enough_face.h similarity index 64% rename from legacy/watch_faces/clock/close_enough_clock_face.h rename to watch-faces/clock/close_enough_face.h index e693043d..ecd41188 100644 --- a/legacy/watch_faces/clock/close_enough_clock_face.h +++ b/watch-faces/clock/close_enough_face.h @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2024 Ruben Nic + * 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 @@ -22,41 +22,36 @@ * SOFTWARE. */ -#ifndef CLOSE_ENOUGH_CLOCK_FACE_H_ -#define CLOSE_ENOUGH_CLOCK_FACE_H_ +#pragma once + +#include "movement.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" - * + * 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" */ -#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; +} close_enough_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); +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_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, \ +#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, \ }) - -#endif // CLOSE_ENOUGH_CLOCK_FACE_H_