Merge pull request #281 from Kistelini/day_one_face

Improvements for Day One face
This commit is contained in:
Wesley Aptekar-Cassels 2023-11-27 23:24:04 -05:00 committed by GitHub
commit 91c82ee5f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 160 additions and 76 deletions

View File

@ -27,24 +27,54 @@
#include "day_one_face.h" #include "day_one_face.h"
#include "watch.h" #include "watch.h"
static const uint8_t days_in_month[12] = {31, 29, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
static uint32_t _day_one_face_juliandaynum(uint16_t year, uint16_t month, uint16_t day) { static uint32_t _day_one_face_juliandaynum(uint16_t year, uint16_t month, uint16_t day) {
// from here: https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation // from here: https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
return (1461 * (year + 4800 + (month - 14) / 12)) / 4 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 - (3 * ((year + 4900 + (month - 14) / 12) / 100))/4 + day - 32075; return (1461 * (year + 4800 + (month - 14) / 12)) / 4 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 - (3 * ((year + 4900 + (month - 14) / 12) / 100))/4 + day - 32075;
} }
static void _day_one_face_update(day_one_state_t state) { static void _day_one_face_update(day_one_state_t *state) {
char buf[15]; char buf[15];
watch_date_time date_time = watch_rtc_get_date_time(); watch_date_time date_time = watch_rtc_get_date_time();
uint32_t julian_date = _day_one_face_juliandaynum(date_time.unit.year + WATCH_RTC_REFERENCE_YEAR, date_time.unit.month, date_time.unit.day); uint32_t julian_date = _day_one_face_juliandaynum(date_time.unit.year + WATCH_RTC_REFERENCE_YEAR, date_time.unit.month, date_time.unit.day);
uint32_t julian_birthdate = _day_one_face_juliandaynum(state.birth_year, state.birth_month, state.birth_day); uint32_t julian_birthdate = _day_one_face_juliandaynum(state->birth_year, state->birth_month, state->birth_day);
if (julian_date < julian_birthdate) { if (julian_date < julian_birthdate) {
sprintf(buf, "DA %6lu", julian_birthdate - julian_date); sprintf(buf, "DA %6lu", julian_birthdate - julian_date);
} else { } else {
sprintf(buf, "DA %6lu", julian_date - julian_birthdate); sprintf(buf, "DA %6lu", julian_date - julian_birthdate);
} }
watch_display_string(buf, 0); watch_display_string(buf, 0);
} }
static void _day_one_face_abort_quick_cycle(day_one_state_t *state) {
if (state->quick_cycle) {
state->quick_cycle = false;
movement_request_tick_frequency(4);
}
}
static void _day_one_face_increment(day_one_state_t *state) {
state->birthday_changed = true;
switch (state->current_page) {
case PAGE_YEAR:
state->birth_year = state->birth_year + 1;
if (state->birth_year > 2080) state->birth_year = 1900;
break;
case PAGE_MONTH:
state->birth_month = (state->birth_month % 12) + 1;
break;
case PAGE_DAY:
state->birth_day = state->birth_day + 1;
if (state->birth_day == 0 || state->birth_day > days_in_month[state->birth_month - 1]) {
state->birth_day = 1;
}
break;
default:
break;
}
}
void day_one_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { void day_one_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
(void) settings; (void) settings;
(void) watch_face_index; (void) watch_face_index;
@ -54,7 +84,7 @@ void day_one_face_setup(movement_settings_t *settings, uint8_t watch_face_index,
movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2); movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2);
if (movement_birthdate.reg == 0) { if (movement_birthdate.reg == 0) {
// if birth date is totally blank, set a reasonable starting date. this works well for anyone under 63, but // if birth date is totally blank, set a reasonable starting date. this works well for anyone under 63, but
// you can keep pressing to go back to 1900; just pass the current year. also picked this date because if you // you can keep pressing to go back to 1900; just pass the year 2080. also picked this date because if you
// set it to 1959-01-02, it counts up from the launch of Luna-1, the first spacecraft to leave the well. // set it to 1959-01-02, it counts up from the launch of Luna-1, the first spacecraft to leave the well.
movement_birthdate.bit.year = 1959; movement_birthdate.bit.year = 1959;
movement_birthdate.bit.month = 1; movement_birthdate.bit.month = 1;
@ -68,11 +98,9 @@ void day_one_face_activate(movement_settings_t *settings, void *context) {
(void) settings; (void) settings;
day_one_state_t *state = (day_one_state_t *)context; day_one_state_t *state = (day_one_state_t *)context;
// stash the current year, useful in birthday setting mode. state->current_page = PAGE_DISPLAY;
watch_date_time date_time = watch_rtc_get_date_time(); state->quick_cycle = false;
state->current_year = date_time.unit.year + WATCH_RTC_REFERENCE_YEAR; state->ticks = 0;
// reset the current page to 0, display days alive.
state->current_page = 0;
// fetch the user's birth date from the birthday register. // fetch the user's birth date from the birthday register.
movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2); movement_birthdate_t movement_birthdate = (movement_birthdate_t) watch_get_backup_data(2);
@ -85,96 +113,143 @@ bool day_one_face_loop(movement_event_t event, movement_settings_t *settings, vo
(void) settings; (void) settings;
day_one_state_t *state = (day_one_state_t *)context; day_one_state_t *state = (day_one_state_t *)context;
const uint8_t days_in_month[12] = {31, 29, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31}; char buf[9];
char buf[6];
switch (event.event_type) { switch (event.event_type) {
case EVENT_ACTIVATE: case EVENT_ACTIVATE:
_day_one_face_update(*state); _day_one_face_update(state);
break; break;
case EVENT_LOW_ENERGY_UPDATE: case EVENT_LOW_ENERGY_UPDATE:
case EVENT_TICK: case EVENT_TICK:
if (state->current_page != 0) { if (state->quick_cycle) {
if (watch_get_pin_level(BTN_ALARM)) {
_day_one_face_increment(state);
} else {
_day_one_face_abort_quick_cycle(state);
}
}
switch (state->current_page) {
// if in settings mode, update whatever the current page is // if in settings mode, update whatever the current page is
switch (state->current_page) { case PAGE_YEAR:
case 1: watch_display_string("YR ", 0);
watch_display_string("YR ", 0); if (event.subsecond % 2) {
if (event.subsecond % 2) { sprintf(buf, "%4d", state->birth_year);
sprintf(buf, "%4d", state->birth_year); watch_display_string(buf, 4);
watch_display_string(buf, 4); }
} break;
break; case PAGE_MONTH:
case 2: watch_display_string("MO ", 0);
watch_display_string("MO ", 0); if (event.subsecond % 2) {
if (event.subsecond % 2) { sprintf(buf, "%2d", state->birth_month);
sprintf(buf, "%2d", state->birth_month); watch_display_string(buf, 4);
watch_display_string(buf, 4); }
} break;
break; case PAGE_DAY:
case 3: watch_display_string("DA ", 0);
watch_display_string("DA ", 0); if (event.subsecond % 2) {
if (event.subsecond % 2) { sprintf(buf, "%2d", state->birth_day);
sprintf(buf, "%2d", state->birth_day); watch_display_string(buf, 6);
watch_display_string(buf, 6); }
} break;
break;
}
} else {
// otherwise, check if we have to update. the display only needs to change at midnight! // otherwise, check if we have to update. the display only needs to change at midnight!
watch_date_time date_time = watch_rtc_get_date_time(); case PAGE_DISPLAY: {
if (date_time.unit.hour == 0 && date_time.unit.minute == 0 && date_time.unit.second == 0) { watch_date_time date_time = watch_rtc_get_date_time();
_day_one_face_update(*state); if (date_time.unit.hour == 0 && date_time.unit.minute == 0 && date_time.unit.second == 0) {
} _day_one_face_update(state);
}
break;}
case PAGE_DATE:
if (state->ticks > 0) {
state->ticks--;
} else {
state->current_page = PAGE_DISPLAY;
_day_one_face_update(state);
}
break;
default:
break;
} }
break; break;
case EVENT_LIGHT_BUTTON_DOWN: case EVENT_LIGHT_BUTTON_DOWN:
// only illuminate if we're in display mode // only illuminate if we're in display mode
if (state->current_page == 0) movement_illuminate_led(); switch (state->current_page) {
case PAGE_DISPLAY:
// fall through
case PAGE_DATE:
movement_illuminate_led();
break;
default:
break;
}
break; break;
case EVENT_LIGHT_BUTTON_UP: case EVENT_LIGHT_BUTTON_UP:
// otherwise use the light button to advance settings pages. // otherwise use the light button to advance settings pages.
if (state->current_page != 0) { switch (state->current_page) {
// go to next setting page... case PAGE_YEAR:
state->current_page = (state->current_page + 1) % 4; // fall through
if (state->current_page == 0) { case PAGE_MONTH:
// ...unless we've been pushed back to display mode. // fall through
movement_request_tick_frequency(1); case PAGE_DAY:
// force display since it normally won't update til midnight. // go to next setting page...
_day_one_face_update(*state); state->current_page = (state->current_page + 1) % 4;
} if (state->current_page == PAGE_DISPLAY) {
// ...unless we've been pushed back to display mode.
movement_request_tick_frequency(1);
// force display since it normally won't update til midnight.
_day_one_face_update(state);
}
break;
default:
break;
} }
break; break;
case EVENT_ALARM_BUTTON_UP: case EVENT_ALARM_BUTTON_UP:
// if we are on a settings page, increment whatever value we're setting. // if we are on a settings page, increment whatever value we're setting.
if (state->current_page != 0) { switch (state->current_page) {
state->birthday_changed = true; case PAGE_YEAR:
switch (state->current_page) { // fall through
case 1: case PAGE_MONTH:
state->birth_year = state->birth_year + 1; // fall through
if (state->birth_year > state->current_year) state->birth_year = 1900; case PAGE_DAY:
break; _day_one_face_abort_quick_cycle(state);
case 2: _day_one_face_increment(state);
state->birth_month = (state->birth_month % 12) + 1; break;
break; case PAGE_DISPLAY:
case 3: state->current_page = PAGE_DATE;
state->birth_day = state->birth_day + 1; sprintf(buf, "%04d%02d%02d", state->birth_year % 10000, state->birth_month % 100, state->birth_day % 100);
if (state->birth_day == 0 || state->birth_day > days_in_month[state->birth_month - 1]) { watch_display_string(buf, 2);
state->birth_day = 1; state->ticks = 2;
} break;
break; default:
} break;
} }
break; break;
case EVENT_ALARM_LONG_PRESS: case EVENT_ALARM_LONG_PRESS:
// if we aren't already in settings mode, put us there. // if we aren't already in settings mode, put us there.
if (state->current_page == 0) { switch (state->current_page) {
state->current_page++; case PAGE_DISPLAY:
movement_request_tick_frequency(4); state->current_page++;
movement_request_tick_frequency(4);
break;
case PAGE_YEAR:
// fall through
case PAGE_MONTH:
// fall through
case PAGE_DAY:
state->quick_cycle = true;
movement_request_tick_frequency(8);
break;
default:
break;
} }
break; break;
case EVENT_ALARM_LONG_UP:
_day_one_face_abort_quick_cycle(state);
break;
case EVENT_TIMEOUT: case EVENT_TIMEOUT:
_day_one_face_abort_quick_cycle(state);
// return home if we're on a settings page (this saves our changes when we resign). // return home if we're on a settings page (this saves our changes when we resign).
if (state->current_page != 0) { if (state->current_page != PAGE_DISPLAY) {
movement_move_to_face(0); movement_move_to_face(0);
} }
break; break;

View File

@ -28,7 +28,7 @@
/* /*
* DAY ONE face * DAY ONE face
* *
* This watch face displays the number of days since a given date. * This watch face displays the number of days since or until a given date.
* It was originally designed to display the number of days youve been alive, * It was originally designed to display the number of days youve been alive,
* but technically it can count up from any date in the 20th century or the * but technically it can count up from any date in the 20th century or the
* 21st century, so far. * 21st century, so far.
@ -49,13 +49,22 @@
#include "movement.h" #include "movement.h"
typedef enum {
PAGE_DISPLAY,
PAGE_YEAR,
PAGE_MONTH,
PAGE_DAY,
PAGE_DATE
} day_one_page_t;
typedef struct { typedef struct {
uint8_t current_page; day_one_page_t current_page;
uint16_t current_year;
uint16_t birth_year; uint16_t birth_year;
uint8_t birth_month; uint8_t birth_month;
uint8_t birth_day; uint8_t birth_day;
bool birthday_changed; bool birthday_changed;
bool quick_cycle;
uint8_t ticks;
} day_one_state_t; } day_one_state_t;
void day_one_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); void day_one_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);