|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2022 Joey Castillo |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdlib.h> |
| 26 | +#include "simple_clock_face.h" |
| 27 | +#include "watch.h" |
| 28 | +#include "watch_utility.h" |
| 29 | +#include "watch_private_display.h" |
| 30 | + |
| 31 | +static void _update_alarm_indicator(bool settings_alarm_enabled, simple_clock_state_t *state) { |
| 32 | + state->alarm_enabled = settings_alarm_enabled; |
| 33 | + if (state->alarm_enabled) watch_set_indicator(WATCH_INDICATOR_SIGNAL); |
| 34 | + else watch_clear_indicator(WATCH_INDICATOR_SIGNAL); |
| 35 | +} |
| 36 | + |
| 37 | +void simple_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { |
| 38 | + (void) settings; |
| 39 | + (void) watch_face_index; |
| 40 | + |
| 41 | + if (*context_ptr == NULL) { |
| 42 | + *context_ptr = malloc(sizeof(simple_clock_state_t)); |
| 43 | + simple_clock_state_t *state = (simple_clock_state_t *)*context_ptr; |
| 44 | + state->signal_enabled = false; |
| 45 | + state->watch_face_index = watch_face_index; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +void simple_clock_face_activate(movement_settings_t *settings, void *context) { |
| 50 | + simple_clock_state_t *state = (simple_clock_state_t *)context; |
| 51 | + |
| 52 | + if (watch_tick_animation_is_running()) watch_stop_tick_animation(); |
| 53 | + |
| 54 | + if (settings->bit.clock_mode_24h) watch_set_indicator(WATCH_INDICATOR_24H); |
| 55 | + |
| 56 | + // handle chime indicator |
| 57 | + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); |
| 58 | + else watch_clear_indicator(WATCH_INDICATOR_BELL); |
| 59 | + |
| 60 | + // show alarm indicator if there is an active alarm |
| 61 | + _update_alarm_indicator(settings->bit.alarm_enabled, state); |
| 62 | + |
| 63 | + watch_set_colon(); |
| 64 | + |
| 65 | + // this ensures that none of the timestamp fields will match, so we can re-render them all. |
| 66 | + state->previous_date_time = 0xFFFFFFFF; |
| 67 | +} |
| 68 | + |
| 69 | +bool simple_clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { |
| 70 | + simple_clock_state_t *state = (simple_clock_state_t *)context; |
| 71 | + char buf[11]; |
| 72 | + uint8_t pos; |
| 73 | + |
| 74 | + watch_date_time date_time; |
| 75 | + uint32_t previous_date_time; |
| 76 | + switch (event.event_type) { |
| 77 | + case EVENT_ACTIVATE: |
| 78 | + case EVENT_TICK: |
| 79 | + case EVENT_LOW_ENERGY_UPDATE: |
| 80 | + date_time = watch_rtc_get_date_time(); |
| 81 | + previous_date_time = state->previous_date_time; |
| 82 | + state->previous_date_time = date_time.reg; |
| 83 | + |
| 84 | + // check the battery voltage once a day... |
| 85 | + if (date_time.unit.day != state->last_battery_check) { |
| 86 | + state->last_battery_check = date_time.unit.day; |
| 87 | + watch_enable_adc(); |
| 88 | + uint16_t voltage = watch_get_vcc_voltage(); |
| 89 | + watch_disable_adc(); |
| 90 | + // 2.2 volts will happen when the battery has maybe 5-10% remaining? |
| 91 | + // we can refine this later. |
| 92 | + state->battery_low = (voltage < 2200); |
| 93 | + } |
| 94 | + |
| 95 | + // ...and set the LAP indicator if low. |
| 96 | + if (state->battery_low) watch_set_indicator(WATCH_INDICATOR_LAP); |
| 97 | + |
| 98 | + if ((date_time.reg >> 6) == (previous_date_time >> 6) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { |
| 99 | + // everything before seconds is the same, don't waste cycles setting those segments. |
| 100 | + watch_display_character_lp_seconds('0' + date_time.unit.second / 10, 8); |
| 101 | + watch_display_character_lp_seconds('0' + date_time.unit.second % 10, 9); |
| 102 | + break; |
| 103 | + } else if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { |
| 104 | + // everything before minutes is the same. |
| 105 | + pos = 6; |
| 106 | + sprintf(buf, "%02d%02d", date_time.unit.minute, date_time.unit.second); |
| 107 | + } else { |
| 108 | + // other stuff changed; let's do it all. |
| 109 | + if (!settings->bit.clock_mode_24h) { |
| 110 | + // if we are in 12 hour mode, do some cleanup. |
| 111 | + if (date_time.unit.hour < 12) { |
| 112 | + watch_clear_indicator(WATCH_INDICATOR_PM); |
| 113 | + } else { |
| 114 | + watch_set_indicator(WATCH_INDICATOR_PM); |
| 115 | + } |
| 116 | + date_time.unit.hour %= 12; |
| 117 | + if (date_time.unit.hour == 0) date_time.unit.hour = 12; |
| 118 | + } |
| 119 | + pos = 0; |
| 120 | + if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { |
| 121 | + if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); |
| 122 | + sprintf(buf, "%s%2d%2d%02d ", watch_utility_get_weekday(date_time), date_time.unit.day, date_time.unit.hour, date_time.unit.minute); |
| 123 | + } else { |
| 124 | + sprintf(buf, "%s%2d%2d%02d%02d", watch_utility_get_weekday(date_time), date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second); |
| 125 | + } |
| 126 | + } |
| 127 | + watch_display_string(buf, pos); |
| 128 | + // handle alarm indicator |
| 129 | + if (state->alarm_enabled != settings->bit.alarm_enabled) _update_alarm_indicator(settings->bit.alarm_enabled, state); |
| 130 | + break; |
| 131 | + case EVENT_ALARM_LONG_PRESS: |
| 132 | + state->signal_enabled = !state->signal_enabled; |
| 133 | + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); |
| 134 | + else watch_clear_indicator(WATCH_INDICATOR_BELL); |
| 135 | + break; |
| 136 | + case EVENT_BACKGROUND_TASK: |
| 137 | + // uncomment this line to snap back to the clock face when the hour signal sounds: |
| 138 | + // movement_move_to_face(state->watch_face_index); |
| 139 | + movement_play_signal(); |
| 140 | + break; |
| 141 | + default: |
| 142 | + return movement_default_loop_handler(event, settings); |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | +} |
| 147 | + |
| 148 | +void simple_clock_face_resign(movement_settings_t *settings, void *context) { |
| 149 | + (void) settings; |
| 150 | + (void) context; |
| 151 | +} |
| 152 | + |
| 153 | +bool simple_clock_face_wants_background_task(movement_settings_t *settings, void *context) { |
| 154 | + (void) settings; |
| 155 | + simple_clock_state_t *state = (simple_clock_state_t *)context; |
| 156 | + if (!state->signal_enabled) return false; |
| 157 | + |
| 158 | + watch_date_time date_time = watch_rtc_get_date_time(); |
| 159 | + |
| 160 | + return date_time.unit.minute == 0; |
| 161 | +} |
0 commit comments