From f1d4d4ce89e46a02b719baf1f33947a6ef53b23f Mon Sep 17 00:00:00 2001 From: Wesley Aptekar-Cassels Date: Thu, 9 Nov 2023 00:03:28 -0500 Subject: [PATCH 1/4] Add day_night_percentage_face. --- movement/make/Makefile | 1 + movement/movement_faces.h | 1 + .../clock/day_night_percentage_face.c | 121 ++++++++++++++++++ .../clock/day_night_percentage_face.h | 61 +++++++++ 4 files changed, 184 insertions(+) create mode 100644 movement/watch_faces/clock/day_night_percentage_face.c create mode 100644 movement/watch_faces/clock/day_night_percentage_face.h diff --git a/movement/make/Makefile b/movement/make/Makefile index 625c77296..3953e31ed 100644 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -118,6 +118,7 @@ SRCS += \ ../watch_faces/complication/flashlight_face.c \ ../watch_faces/clock/decimal_time_face.c \ ../watch_faces/clock/wyoscan_face.c \ + ../watch_faces/clock/day_night_percentage_face.c \ # New watch faces go above this line. # Leave this line at the bottom of the file; it has all the targets for making your project. diff --git a/movement/movement_faces.h b/movement/movement_faces.h index ff34c0638..db4f0a2d2 100644 --- a/movement/movement_faces.h +++ b/movement/movement_faces.h @@ -95,6 +95,7 @@ #include "flashlight_face.h" #include "decimal_time_face.h" #include "wyoscan_face.h" +#include "day_night_percentage_face.h" // New includes go above this line. #endif // MOVEMENT_FACES_H_ diff --git a/movement/watch_faces/clock/day_night_percentage_face.c b/movement/watch_faces/clock/day_night_percentage_face.c new file mode 100644 index 000000000..ed62653ff --- /dev/null +++ b/movement/watch_faces/clock/day_night_percentage_face.c @@ -0,0 +1,121 @@ +/* + * MIT License + * + * Copyright (c) 2023 Wesley Aptekar-Cassels + * + * 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 "day_night_percentage_face.h" +#include "watch_utility.h" +#include "sunriset.h" + +void day_night_percentage_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { + (void) settings; + (void) watch_face_index; + (void) context_ptr; +} + +void day_night_percentage_face_activate(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} + +// fmod but handle negatives right +static double better_fmod(double x, double y) { + return fmod(fmod(x, y) + y, y); +} + +bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + (void) context; + + switch (event.event_type) { + case EVENT_ACTIVATE: + case EVENT_TICK: + case EVENT_LOW_ENERGY_UPDATE: + { + movement_location_t movement_location = (movement_location_t) watch_get_backup_data(1); + + if (movement_location.reg == 0) { + watch_display_string(" no Loc", 0); + return true; + } + + watch_date_time date_time = watch_rtc_get_date_time(); // the current local date / time + watch_date_time utc_now = watch_utility_date_time_convert_zone(date_time, movement_timezone_offsets[settings->bit.time_zone] * 60, 0); // the current date / time in UTC + + // Weird quirky unsigned things were happening when I tried to cast these directly to doubles below. + // it looks redundant, but extracting them to local int16's seemed to fix it. + int16_t lat_centi = (int16_t)movement_location.bit.latitude; + int16_t lon_centi = (int16_t)movement_location.bit.longitude; + + double lat = (double)lat_centi / 100.0; + double lon = (double)lon_centi / 100.0; + + double daylen = day_length(utc_now.unit.year + WATCH_RTC_REFERENCE_YEAR, utc_now.unit.month, utc_now.unit.day, lon, lat); + + double rise, set; + char buf[12]; + + int result = sun_rise_set(utc_now.unit.year + WATCH_RTC_REFERENCE_YEAR, utc_now.unit.month, utc_now.unit.day, lon, lat, &rise, &set); + + if (result != 0) { + sprintf(buf, "%s%2dEtrnal", result == 1 ? "DA" : "NI", date_time.unit.day); + watch_display_string(buf, 0); + } else { + double day_hours_decimal = utc_now.unit.hour + (utc_now.unit.minute + (utc_now.unit.second / 60.0)) / 60.0; + + double day_percentage = (24.0 - better_fmod(rise - day_hours_decimal, 24.0)) / daylen; + double night_percentage = (24.0 - better_fmod(set - day_hours_decimal, 24.0)) / (24 - daylen); + + uint16_t percentage; + char day_night[3]; + if (day_percentage > 0.0 && day_percentage < 1.0) { + percentage = day_percentage * 10000; + sprintf(day_night, "%s", "DA"); + } else { + percentage = night_percentage * 10000; + sprintf(day_night, "%s", "NI"); + } + if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { + if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); + sprintf(buf, "%s%2d %02d", day_night, date_time.unit.day, percentage / 100); + } else { + sprintf(buf, "%s%2d %04d", day_night, date_time.unit.day, percentage); + } + watch_display_string(buf, 0); + } + + break; + } + default: + return movement_default_loop_handler(event, settings); + } + + return true; +} + +void day_night_percentage_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} + diff --git a/movement/watch_faces/clock/day_night_percentage_face.h b/movement/watch_faces/clock/day_night_percentage_face.h new file mode 100644 index 000000000..9155745b5 --- /dev/null +++ b/movement/watch_faces/clock/day_night_percentage_face.h @@ -0,0 +1,61 @@ +/* + * MIT License + * + * Copyright (c) 2023 Wesley Aptekar-Cassels + * + * 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 DAY_NIGHT_PERCENTAGE_FACE_H_ +#define DAY_NIGHT_PERCENTAGE_FACE_H_ + +#include "movement.h" + +/* + * Day/night percentage face + * + * Shows the percentage of the way through the day/night the current time is. + * + * The weekday digits show "DA" or "NI" depending on whether it's currently day + * or night. The day digits show what the current day of the month is. The time + * digits show the percentage of the way through the day/night it is, with + * decimals in the smaller seconds digits. If the day or night will last for a + * full 24 hours, the text "Etrnal" is displayed instead of a percentage. + * + * This face does not currently offer any configuration. You must set the + * location register with some other face. + */ + +typedef struct {} day_night_percentage_state_t; + +void day_night_percentage_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void day_night_percentage_face_activate(movement_settings_t *settings, void *context); +bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void day_night_percentage_face_resign(movement_settings_t *settings, void *context); + +#define day_night_percentage_face ((const watch_face_t){ \ + day_night_percentage_face_setup, \ + day_night_percentage_face_activate, \ + day_night_percentage_face_loop, \ + day_night_percentage_face_resign, \ + NULL, \ +}) + +#endif // DAY_NIGHT_PERCENTAGE_FACE_H_ + From 2e8ee9965e843f9fc571681d7f3911fd68f36c7b Mon Sep 17 00:00:00 2001 From: Wesley Aptekar-Cassels Date: Thu, 9 Nov 2023 17:21:35 -0500 Subject: [PATCH 2/4] day_night_percentage_face: Calculate rise/set/daylen only once per day. --- .../clock/day_night_percentage_face.c | 81 +++++++++++-------- .../clock/day_night_percentage_face.h | 7 +- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/movement/watch_faces/clock/day_night_percentage_face.c b/movement/watch_faces/clock/day_night_percentage_face.c index ed62653ff..eaeeeb916 100644 --- a/movement/watch_faces/clock/day_night_percentage_face.c +++ b/movement/watch_faces/clock/day_night_percentage_face.c @@ -29,10 +29,42 @@ #include "watch_utility.h" #include "sunriset.h" +// fmod but handle negatives right +static double better_fmod(double x, double y) { + return fmod(fmod(x, y) + y, y); +} + +static void recalculate(watch_date_time utc_now, day_night_percentage_state_t *state) { + movement_location_t movement_location = (movement_location_t) watch_get_backup_data(1); + + if (movement_location.reg == 0) { + state->result = -2; + return; + } + + // Weird quirky unsigned things were happening when I tried to cast these directly to doubles below. + // it looks redundant, but extracting them to local int16's seemed to fix it. + int16_t lat_centi = (int16_t)movement_location.bit.latitude; + int16_t lon_centi = (int16_t)movement_location.bit.longitude; + + double lat = (double)lat_centi / 100.0; + double lon = (double)lon_centi / 100.0; + + state->daylen = day_length(utc_now.unit.year + WATCH_RTC_REFERENCE_YEAR, utc_now.unit.month, utc_now.unit.day, lon, lat); + + state->result = sun_rise_set(utc_now.unit.year + WATCH_RTC_REFERENCE_YEAR, utc_now.unit.month, utc_now.unit.day, lon, lat, &state->rise, &state->set); +} + void day_night_percentage_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { (void) settings; (void) watch_face_index; - (void) context_ptr; + + if (*context_ptr == NULL) { + *context_ptr = malloc(sizeof(day_night_percentage_state_t)); + day_night_percentage_state_t *state = (day_night_percentage_state_t *)*context_ptr; + watch_date_time utc_now = watch_utility_date_time_convert_zone(watch_rtc_get_date_time(), movement_timezone_offsets[settings->bit.time_zone] * 60, 0); + recalculate(utc_now, state); + } } void day_night_percentage_face_activate(movement_settings_t *settings, void *context) { @@ -40,52 +72,34 @@ void day_night_percentage_face_activate(movement_settings_t *settings, void *con (void) context; } -// fmod but handle negatives right -static double better_fmod(double x, double y) { - return fmod(fmod(x, y) + y, y); -} - bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { - (void) context; + day_night_percentage_state_t *state = (day_night_percentage_state_t *)context; + + char buf[12]; + watch_date_time date_time = watch_rtc_get_date_time(); + watch_date_time utc_now = watch_utility_date_time_convert_zone(date_time, movement_timezone_offsets[settings->bit.time_zone] * 60, 0); switch (event.event_type) { case EVENT_ACTIVATE: case EVENT_TICK: case EVENT_LOW_ENERGY_UPDATE: - { - movement_location_t movement_location = (movement_location_t) watch_get_backup_data(1); + if ((utc_now.unit.hour == 0 && utc_now.unit.minute == 0 && utc_now.unit.second == 0) || state->result == -2) { + recalculate(utc_now, state); + } - if (movement_location.reg == 0) { + if (state->result == -2) { watch_display_string(" no Loc", 0); - return true; + break; } - watch_date_time date_time = watch_rtc_get_date_time(); // the current local date / time - watch_date_time utc_now = watch_utility_date_time_convert_zone(date_time, movement_timezone_offsets[settings->bit.time_zone] * 60, 0); // the current date / time in UTC - - // Weird quirky unsigned things were happening when I tried to cast these directly to doubles below. - // it looks redundant, but extracting them to local int16's seemed to fix it. - int16_t lat_centi = (int16_t)movement_location.bit.latitude; - int16_t lon_centi = (int16_t)movement_location.bit.longitude; - - double lat = (double)lat_centi / 100.0; - double lon = (double)lon_centi / 100.0; - - double daylen = day_length(utc_now.unit.year + WATCH_RTC_REFERENCE_YEAR, utc_now.unit.month, utc_now.unit.day, lon, lat); - - double rise, set; - char buf[12]; - - int result = sun_rise_set(utc_now.unit.year + WATCH_RTC_REFERENCE_YEAR, utc_now.unit.month, utc_now.unit.day, lon, lat, &rise, &set); - - if (result != 0) { - sprintf(buf, "%s%2dEtrnal", result == 1 ? "DA" : "NI", date_time.unit.day); + if (state->result != 0) { + sprintf(buf, "%s%2dEtrnal", state->result == 1 ? "DA" : "NI", date_time.unit.day); watch_display_string(buf, 0); } else { double day_hours_decimal = utc_now.unit.hour + (utc_now.unit.minute + (utc_now.unit.second / 60.0)) / 60.0; - double day_percentage = (24.0 - better_fmod(rise - day_hours_decimal, 24.0)) / daylen; - double night_percentage = (24.0 - better_fmod(set - day_hours_decimal, 24.0)) / (24 - daylen); + double day_percentage = (24.0 - better_fmod(state->rise - day_hours_decimal, 24.0)) / state->daylen; + double night_percentage = (24.0 - better_fmod(state->set - day_hours_decimal, 24.0)) / (24 - state->daylen); uint16_t percentage; char day_night[3]; @@ -106,7 +120,6 @@ bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t } break; - } default: return movement_default_loop_handler(event, settings); } diff --git a/movement/watch_faces/clock/day_night_percentage_face.h b/movement/watch_faces/clock/day_night_percentage_face.h index 9155745b5..c5865329c 100644 --- a/movement/watch_faces/clock/day_night_percentage_face.h +++ b/movement/watch_faces/clock/day_night_percentage_face.h @@ -42,7 +42,12 @@ * location register with some other face. */ -typedef struct {} day_night_percentage_state_t; +typedef struct { + int result; // -1, 0, 1: result from sun_rise_set, -2: no location set + double rise; + double set; + double daylen; +} day_night_percentage_state_t; void day_night_percentage_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); void day_night_percentage_face_activate(movement_settings_t *settings, void *context); From 26f63dcaebb375dd7fb756cc482d321da529d92f Mon Sep 17 00:00:00 2001 From: Wesley Aptekar-Cassels Date: Thu, 9 Nov 2023 17:33:43 -0500 Subject: [PATCH 3/4] day_night_percentage_face: Use PM indicator instead of DA/NI. This allows for use of the weekday digits for displaying the weekday. --- .../clock/day_night_percentage_face.c | 17 +++++++++++------ .../clock/day_night_percentage_face.h | 10 +++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/movement/watch_faces/clock/day_night_percentage_face.c b/movement/watch_faces/clock/day_night_percentage_face.c index eaeeeb916..02062064a 100644 --- a/movement/watch_faces/clock/day_night_percentage_face.c +++ b/movement/watch_faces/clock/day_night_percentage_face.c @@ -92,8 +92,14 @@ bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t break; } + const char* weekday = watch_utility_get_weekday(date_time); if (state->result != 0) { - sprintf(buf, "%s%2dEtrnal", state->result == 1 ? "DA" : "NI", date_time.unit.day); + if (state->result == 1) { + watch_clear_indicator(WATCH_INDICATOR_PM); + } else { + watch_set_indicator(WATCH_INDICATOR_PM); + } + sprintf(buf, "%s%2dEtrnal", weekday, date_time.unit.day); watch_display_string(buf, 0); } else { double day_hours_decimal = utc_now.unit.hour + (utc_now.unit.minute + (utc_now.unit.second / 60.0)) / 60.0; @@ -102,19 +108,18 @@ bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t double night_percentage = (24.0 - better_fmod(state->set - day_hours_decimal, 24.0)) / (24 - state->daylen); uint16_t percentage; - char day_night[3]; if (day_percentage > 0.0 && day_percentage < 1.0) { percentage = day_percentage * 10000; - sprintf(day_night, "%s", "DA"); + watch_clear_indicator(WATCH_INDICATOR_PM); } else { percentage = night_percentage * 10000; - sprintf(day_night, "%s", "NI"); + watch_set_indicator(WATCH_INDICATOR_PM); } if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); - sprintf(buf, "%s%2d %02d", day_night, date_time.unit.day, percentage / 100); + sprintf(buf, "%s%2d %02d", weekday, date_time.unit.day, percentage / 100); } else { - sprintf(buf, "%s%2d %04d", day_night, date_time.unit.day, percentage); + sprintf(buf, "%s%2d %04d", weekday, date_time.unit.day, percentage); } watch_display_string(buf, 0); } diff --git a/movement/watch_faces/clock/day_night_percentage_face.h b/movement/watch_faces/clock/day_night_percentage_face.h index c5865329c..d172c7485 100644 --- a/movement/watch_faces/clock/day_night_percentage_face.h +++ b/movement/watch_faces/clock/day_night_percentage_face.h @@ -32,11 +32,11 @@ * * Shows the percentage of the way through the day/night the current time is. * - * The weekday digits show "DA" or "NI" depending on whether it's currently day - * or night. The day digits show what the current day of the month is. The time - * digits show the percentage of the way through the day/night it is, with - * decimals in the smaller seconds digits. If the day or night will last for a - * full 24 hours, the text "Etrnal" is displayed instead of a percentage. + * The time digits show the percentage of the way through the day/night it is, + * with decimals in the smaller seconds digits. If the day or night will last + * for a full 24 hours, the text "Etrnal" is displayed instead of a percentage. + * The "PM" indicator is set when it is currently nighttime. The weekday and + * day digits display the weekday and day, as one would expect. * * This face does not currently offer any configuration. You must set the * location register with some other face. From 691f2fe5060c25c12471b185af5cef7f8e4d57e6 Mon Sep 17 00:00:00 2001 From: Wesley Aptekar-Cassels Date: Thu, 9 Nov 2023 20:31:56 -0500 Subject: [PATCH 4/4] day_night_percentage_face: Clear seconds digits when entering LE mode. --- movement/watch_faces/clock/day_night_percentage_face.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/movement/watch_faces/clock/day_night_percentage_face.c b/movement/watch_faces/clock/day_night_percentage_face.c index 02062064a..86f07f9d3 100644 --- a/movement/watch_faces/clock/day_night_percentage_face.c +++ b/movement/watch_faces/clock/day_night_percentage_face.c @@ -117,7 +117,7 @@ bool day_night_percentage_face_loop(movement_event_t event, movement_settings_t } if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); - sprintf(buf, "%s%2d %02d", weekday, date_time.unit.day, percentage / 100); + sprintf(buf, "%s%2d %02d ", weekday, date_time.unit.day, percentage / 100); } else { sprintf(buf, "%s%2d %04d", weekday, date_time.unit.day, percentage); }