-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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 <stdlib.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#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); | ||
|
||
if (day_percentage > 0.0 && day_percentage < 1.0) { | ||
uint16_t percentage = day_percentage * 10000; | ||
sprintf(buf, "DA%2d %04d", date_time.unit.day, percentage); | ||
} else { | ||
uint16_t percentage = night_percentage * 10000; | ||
sprintf(buf, "NI%2d %04d", date_time.unit.day, percentage); | ||
} | ||
watch_display_string(buf, 0); | ||
} | ||
|
||
if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { | ||
if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); | ||
} | ||
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ | ||
|