day_one_face: cleanup

This commit is contained in:
Christian Buschau 2023-09-19 15:21:00 +02:00
parent bc9b4ce700
commit 8c7e9fa558
No known key found for this signature in database
GPG Key ID: 74B8EC7053894AC4
2 changed files with 33 additions and 23 deletions

View File

@ -32,15 +32,15 @@ static uint32_t _day_one_face_juliandaynum(uint16_t year, uint16_t month, uint16
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);
} }
@ -71,8 +71,7 @@ void day_one_face_activate(movement_settings_t *settings, void *context) {
// stash the current year, useful in birthday setting mode. // stash the current year, useful in birthday setting mode.
watch_date_time date_time = watch_rtc_get_date_time(); watch_date_time date_time = watch_rtc_get_date_time();
state->current_year = date_time.unit.year + WATCH_RTC_REFERENCE_YEAR; state->current_year = date_time.unit.year + WATCH_RTC_REFERENCE_YEAR;
// reset the current page to 0, display days alive. state->current_page = PAGE_DISPLAY;
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);
@ -90,91 +89,95 @@ bool day_one_face_loop(movement_event_t event, movement_settings_t *settings, vo
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->current_page != PAGE_DISPLAY) {
// if in settings mode, update whatever the current page is // if in settings mode, update whatever the current page is
switch (state->current_page) { switch (state->current_page) {
case 1: case PAGE_YEAR:
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 2: case PAGE_MONTH:
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 3: case PAGE_DAY:
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;
default:
break;
} }
} else { } 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(); watch_date_time date_time = watch_rtc_get_date_time();
if (date_time.unit.hour == 0 && date_time.unit.minute == 0 && date_time.unit.second == 0) { if (date_time.unit.hour == 0 && date_time.unit.minute == 0 && date_time.unit.second == 0) {
_day_one_face_update(*state); _day_one_face_update(state);
} }
} }
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(); if (state->current_page == PAGE_DISPLAY) movement_illuminate_led();
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) { if (state->current_page != PAGE_DISPLAY) {
// go to next setting page... // go to next setting page...
state->current_page = (state->current_page + 1) % 4; state->current_page = (state->current_page + 1) % 4;
if (state->current_page == 0) { if (state->current_page == 0) {
// ...unless we've been pushed back to display mode. // ...unless we've been pushed back to display mode.
movement_request_tick_frequency(1); movement_request_tick_frequency(1);
// force display since it normally won't update til midnight. // force display since it normally won't update til midnight.
_day_one_face_update(*state); _day_one_face_update(state);
} }
} }
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) { if (state->current_page != PAGE_DISPLAY) {
state->birthday_changed = true; state->birthday_changed = true;
switch (state->current_page) { switch (state->current_page) {
case 1: case PAGE_YEAR:
state->birth_year = state->birth_year + 1; state->birth_year = state->birth_year + 1;
if (state->birth_year > state->current_year) state->birth_year = 1900; if (state->birth_year > state->current_year) state->birth_year = 1900;
break; break;
case 2: case PAGE_MONTH:
state->birth_month = (state->birth_month % 12) + 1; state->birth_month = (state->birth_month % 12) + 1;
break; break;
case 3: case PAGE_DAY:
state->birth_day = state->birth_day + 1; state->birth_day = state->birth_day + 1;
if (state->birth_day == 0 || state->birth_day > days_in_month[state->birth_month - 1]) { if (state->birth_day == 0 || state->birth_day > days_in_month[state->birth_month - 1]) {
state->birth_day = 1; state->birth_day = 1;
} }
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) { if (state->current_page == PAGE_DISPLAY) {
state->current_page++; state->current_page++;
movement_request_tick_frequency(4); movement_request_tick_frequency(4);
} }
break; break;
case EVENT_TIMEOUT: case EVENT_TIMEOUT:
// 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

@ -27,11 +27,18 @@
#include "movement.h" #include "movement.h"
// The Day One face is designed to count upwards from the wearer's date of birth. It also functions as an // The Day One face is designed to count the days since or until a given date. It also functions as an
// interface for setting the birth date register, which other watch faces can use for various purposes. // interface for setting the birth date register, which other watch faces can use for various purposes.
typedef enum {
PAGE_DISPLAY,
PAGE_YEAR,
PAGE_MONTH,
PAGE_DAY
} day_one_page_t;
typedef struct { typedef struct {
uint8_t current_page; day_one_page_t current_page;
uint16_t current_year; uint16_t current_year;
uint16_t birth_year; uint16_t birth_year;
uint8_t birth_month; uint8_t birth_month;