world clock: store settings in backup register if possible

This commit is contained in:
Joey Castillo 2022-02-12 22:52:26 -05:00
parent 0bb267c702
commit ee95229e19
2 changed files with 34 additions and 26 deletions

View File

@ -33,10 +33,13 @@ void world_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_in
(void) watch_face_index; (void) watch_face_index;
if (*context_ptr == NULL) { if (*context_ptr == NULL) {
*context_ptr = malloc(sizeof(world_clock_state_t)); *context_ptr = malloc(sizeof(world_clock_state_t));
world_clock_state_t *state = (world_clock_state_t *)*context_ptr; memset(*context_ptr, 0, sizeof(world_clock_state_t));
state->settings.char_0 = 0; uint8_t backup_register = movement_claim_backup_register();
state->settings.char_1 = 0; if (backup_register) {
state->settings.timezone_index = 0; // start at UTC world_clock_state_t *state = (world_clock_state_t *)*context_ptr;
state->settings.reg = watch_get_backup_data(backup_register);
state->backup_register = backup_register;
}
} }
} }
@ -64,7 +67,7 @@ static bool world_clock_face_do_display_mode(movement_event_t event, movement_se
case EVENT_LOW_ENERGY_UPDATE: case EVENT_LOW_ENERGY_UPDATE:
date_time = watch_rtc_get_date_time(); date_time = watch_rtc_get_date_time();
timestamp = watch_utility_date_time_to_unix_time(date_time, movement_timezone_offsets[settings->bit.time_zone] * 60); timestamp = watch_utility_date_time_to_unix_time(date_time, movement_timezone_offsets[settings->bit.time_zone] * 60);
date_time = watch_utility_date_time_from_unix_time(timestamp, movement_timezone_offsets[state->settings.timezone_index] * 60); date_time = watch_utility_date_time_from_unix_time(timestamp, movement_timezone_offsets[state->settings.bit.timezone_index] * 60);
previous_date_time = state->previous_date_time; previous_date_time = state->previous_date_time;
state->previous_date_time = date_time.reg; state->previous_date_time = date_time.reg;
@ -92,15 +95,15 @@ static bool world_clock_face_do_display_mode(movement_event_t event, movement_se
if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { if (event.event_type == EVENT_LOW_ENERGY_UPDATE) {
if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); if (!watch_tick_animation_is_running()) watch_start_tick_animation(500);
sprintf(buf, "%c%c%2d%2d%02d ", sprintf(buf, "%c%c%2d%2d%02d ",
movement_valid_position_0_chars[state->settings.char_0], movement_valid_position_0_chars[state->settings.bit.char_0],
movement_valid_position_1_chars[state->settings.char_1], movement_valid_position_1_chars[state->settings.bit.char_1],
date_time.unit.day, date_time.unit.day,
date_time.unit.hour, date_time.unit.hour,
date_time.unit.minute); date_time.unit.minute);
} else { } else {
sprintf(buf, "%c%c%2d%2d%02d%02d", sprintf(buf, "%c%c%2d%2d%02d%02d",
movement_valid_position_0_chars[state->settings.char_0], movement_valid_position_0_chars[state->settings.bit.char_0],
movement_valid_position_1_chars[state->settings.char_1], movement_valid_position_1_chars[state->settings.bit.char_1],
date_time.unit.day, date_time.unit.day,
date_time.unit.hour, date_time.unit.hour,
date_time.unit.minute, date_time.unit.minute,
@ -129,6 +132,7 @@ static bool world_clock_face_do_display_mode(movement_event_t event, movement_se
static bool _world_clock_face_do_settings_mode(movement_event_t event, movement_settings_t *settings, world_clock_state_t *state) { static bool _world_clock_face_do_settings_mode(movement_event_t event, movement_settings_t *settings, world_clock_state_t *state) {
switch (event.event_type) { switch (event.event_type) {
case EVENT_MODE_BUTTON_UP: case EVENT_MODE_BUTTON_UP:
if (state->backup_register) watch_store_backup_data(state->settings.reg, state->backup_register);
movement_move_to_next_face(); movement_move_to_next_face();
return false; return false;
case EVENT_LIGHT_BUTTON_DOWN: case EVENT_LIGHT_BUTTON_DOWN:
@ -137,26 +141,27 @@ static bool _world_clock_face_do_settings_mode(movement_event_t event, movement_
movement_request_tick_frequency(1); movement_request_tick_frequency(1);
state->current_screen = 0; state->current_screen = 0;
state->previous_date_time = 0xFFFFFFFF; state->previous_date_time = 0xFFFFFFFF;
if (state->backup_register) watch_store_backup_data(state->settings.reg, state->backup_register);
world_clock_face_do_display_mode(event, settings, state); world_clock_face_do_display_mode(event, settings, state);
} }
break; break;
case EVENT_ALARM_BUTTON_DOWN: case EVENT_ALARM_BUTTON_DOWN:
switch (state->current_screen) { switch (state->current_screen) {
case 1: case 1:
state->settings.char_0++; state->settings.bit.char_0++;
if (state->settings.char_0 >= strlen(movement_valid_position_0_chars)) { if (state->settings.bit.char_0 >= strlen(movement_valid_position_0_chars)) {
state->settings.char_0 = 0; state->settings.bit.char_0 = 0;
} }
break; break;
case 2: case 2:
state->settings.char_1++; state->settings.bit.char_1++;
if (state->settings.char_1 >= strlen(movement_valid_position_1_chars)) { if (state->settings.bit.char_1 >= strlen(movement_valid_position_1_chars)) {
state->settings.char_1 = 0; state->settings.bit.char_1 = 0;
} }
break; break;
case 3: case 3:
state->settings.timezone_index++; state->settings.bit.timezone_index++;
if (state->settings.timezone_index > 40) state->settings.timezone_index = 0; if (state->settings.bit.timezone_index > 40) state->settings.bit.timezone_index = 0;
break; break;
} }
break; break;
@ -169,10 +174,10 @@ static bool _world_clock_face_do_settings_mode(movement_event_t event, movement_
char buf[13]; char buf[13];
sprintf(buf, "%c%c %3d%02d ", sprintf(buf, "%c%c %3d%02d ",
movement_valid_position_0_chars[state->settings.char_0], movement_valid_position_0_chars[state->settings.bit.char_0],
movement_valid_position_1_chars[state->settings.char_1], movement_valid_position_1_chars[state->settings.bit.char_1],
(int8_t) (movement_timezone_offsets[state->settings.timezone_index] / 60), (int8_t) (movement_timezone_offsets[state->settings.bit.timezone_index] / 60),
(int8_t) (movement_timezone_offsets[state->settings.timezone_index] % 60) * (movement_timezone_offsets[state->settings.timezone_index] < 0 ? -1 : 1)); (int8_t) (movement_timezone_offsets[state->settings.bit.timezone_index] % 60) * (movement_timezone_offsets[state->settings.bit.timezone_index] < 0 ? -1 : 1));
watch_set_colon(); watch_set_colon();
watch_clear_indicator(WATCH_INDICATOR_PM); watch_clear_indicator(WATCH_INDICATOR_PM);

View File

@ -26,15 +26,18 @@
#define WORLD_CLOCK_FACE_H_ #define WORLD_CLOCK_FACE_H_
#include "movement.h" #include "movement.h"
typedef union {
typedef struct { struct {
uint8_t char_0; uint8_t char_0;
uint8_t char_1; uint8_t char_1;
uint8_t timezone_index; uint8_t timezone_index;
} bit;
uint32_t reg;
} world_clock_settings_t; } world_clock_settings_t;
typedef struct { typedef struct {
world_clock_settings_t settings; world_clock_settings_t settings;
uint8_t backup_register;
uint8_t current_screen; uint8_t current_screen;
uint32_t previous_date_time; uint32_t previous_date_time;
} world_clock_state_t; } world_clock_state_t;