summaryrefslogtreecommitdiffstats
path: root/movement/watch_faces/settings/finetune_face.c
blob: 89c816657841f94f1bf1d2fff5e764981f213c4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * MIT License
 *
 * Copyright (c) 2022 Mikhail Svarichevsky https://3.14.by/
 *
 * 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.
 *
 * FineTune face allows to align watch with sub-second precision in 25/250ms accuracy.
 * Counts time since previous finetune, and allows to calculate & apply ppm correction for nanosec.
 *
 * Main screen - adjust delay (light/alarm)
 * Long mode press - show hours since previous finetune
 * Long mode press - show calculated ppm correction. You can apply it with long light, or just reset finetune timer with long alarm.
 *
 * Finetune will apply crystal aging correction on every finetune save (as aging is calculated since "last finetune" timestamp) - but you should worry
 * about aging only on second/third years of watch calibration (if you are really looking at less than 10 seconds per year of error).
 *
 * Warning, do not use at the first second of a month, as you might stay at the same month and it will surprise you.
 * Just wait 1 second...We are not fully replicating RTC timer behavior when RTC is off.
 * Simulating months and years is... too much complexity.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "finetune_face.h"
#include "nanosec_face.h"
#include "watch_utility.h"

extern nanosec_state_t nanosec_state;

int total_adjustment;
int8_t finetune_page;

void finetune_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
    (void) settings;
    (void) watch_face_index;
    (void) context_ptr;
    // Do any pin or peripheral setup here; this will be called whenever the watch wakes from deep sleep.
}

void finetune_face_activate(movement_settings_t *settings, void *context) {
    (void) settings;
    (void) context;

    // Handle any tasks related to your watch face coming on screen.
    watch_display_string("FT", 0);
    total_adjustment = 0;
    finetune_page = 0;
}

static float finetune_get_hours_passed(void) {
    uint32_t current_time = watch_utility_date_time_to_unix_time(watch_rtc_get_date_time(), 0);
    return (current_time - nanosec_state.last_correction_time) / 3600.0f;
}

static float finetune_get_correction(void) {
    return total_adjustment / (finetune_get_hours_passed() * 3600.0f) * 1000.0f;
}

static void finetune_update_display(void) {
    char buf[25];

    if (finetune_page == 0) {
        watch_display_string("FT", 0);
        watch_date_time date_time = watch_rtc_get_date_time();
        sprintf(buf, "%02d", date_time.unit.second);
        watch_display_string(buf, 8);

        sprintf(buf, "%04d", abs(total_adjustment));
        watch_display_string(buf, 4);

        if (total_adjustment < 0) {
            watch_display_string("--", 2);
        } else {
            watch_display_string("  ", 2);
        }
    } else if (finetune_page == 1) {
        float hours = finetune_get_hours_passed();

        sprintf(buf, "DT  %4d%02d", (int)hours, (int)(fmodf(hours, 1.) * 100));
        watch_display_string(buf, 0);
    } else if (finetune_page == 2) {
        if (finetune_get_hours_passed() < 6) {
            sprintf(buf, " F  6HR   ");
            watch_display_string(buf, 0);
        } else {
            float correction = finetune_get_correction();
            sprintf(buf, " F%s%2d%04d", (total_adjustment < 0) ? " -" : "  ", (int)fabsf(correction), (int)(remainderf(fabsf(correction), 1.) * 10000));
            watch_display_string(buf, 0);
        }
    }
}

static void finetune_adjust_subseconds(int delta) {
    // Update display first ot make it appear faster for the user
    if (delta > 500)
        total_adjustment += (delta - 1000);
    else
        total_adjustment += delta;
    finetune_update_display();

    // Then delay clock
    watch_rtc_enable(false);
    delay_ms(delta);
    if (delta > 500) {
        watch_date_time date_time = watch_rtc_get_date_time();
        date_time.unit.second = (date_time.unit.second + 1) % 60;
        if (date_time.unit.second == 0) { // Overflow
            date_time.unit.minute = (date_time.unit.minute + 1) % 60;
            if (date_time.unit.minute == 0) { // Overflow
                date_time.unit.hour = (date_time.unit.hour + 1) % 24;
                if (date_time.unit.hour == 0) // Overflow
                    date_time.unit.day++;
            }
        }
        watch_rtc_set_date_time(date_time);
    }
    watch_rtc_enable(true);
}

static void finetune_update_correction_time(void) {
    // Update aging, as we update correciton time - we must bake accrued aging into static offset
    nanosec_state.freq_correction += roundf(nanosec_get_aging() * 100);

    // Remember when we last corrected time
    nanosec_state.last_correction_time = watch_utility_date_time_to_unix_time(watch_rtc_get_date_time(), 0);
    nanosec_save();
    movement_move_to_face(0); // Go to main face after saving settings
}

bool finetune_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {

    (void) settings;
    (void) context;

    switch (event.event_type) {
        case EVENT_ACTIVATE:
            // Show your initial UI here.
            finetune_update_display();
            break;

        case EVENT_TICK:
            // If needed, update your display here, at canonical 0.5sec position.
            // We flash green LED once per minute to measure clock error, when we are not on first screen
            if (finetune_page!=0) {
                watch_date_time date_time;
                date_time = watch_rtc_get_date_time();
                if (date_time.unit.second == 0) {
                    watch_set_led_green();
                    #ifndef __EMSCRIPTEN__
                    delay_us(500);
                    #endif
                    watch_set_led_off();
                }
            }

            finetune_update_display();
            break;

        case EVENT_MODE_BUTTON_UP:
            // Only allow for fast exit when correction is 0!!!
            if (finetune_page == 0 && total_adjustment == 0) {
                movement_move_to_next_face();
            } else {
                finetune_page = (finetune_page + 1) % 3;
                finetune_update_display();
            }
            break;

        case EVENT_MODE_LONG_PRESS:
            // You shouldn't need to change this case; Mode almost always moves to the next watch face.
            finetune_page = (finetune_page + 1) % 3;
            finetune_update_display();
            break;

        case EVENT_LIGHT_LONG_PRESS:
            // We are making it slower by 250ms
            if (finetune_page == 0) {
                finetune_adjust_subseconds(250);
            } else if (finetune_page == 2 && finetune_get_hours_passed() >= 6) {
                // Applying ppm correction, only if >6 hours passed
                nanosec_state.freq_correction += (int)round(finetune_get_correction() * 100);
                finetune_update_correction_time();
            }
            break;

        case EVENT_LIGHT_BUTTON_UP:
            // We are making it slower by 25ms
            if (finetune_page == 0) {
                finetune_adjust_subseconds(25);
            }
            break;

        case EVENT_ALARM_LONG_PRESS:
            if (finetune_page == 0) {
                finetune_adjust_subseconds(750);
            } else if (finetune_page == 2) {
                // Exit without applying correction to ppm, but update correction time
                finetune_update_correction_time();
            }
            break;

        case EVENT_ALARM_BUTTON_UP:
            if (finetune_page == 0) {
                finetune_adjust_subseconds(975);
            }
            break;

        case EVENT_TIMEOUT:
            // Your watch face will receive this event after a period of inactivity. If it makes sense to resign,
            // you may uncomment this line to move back to the first watch face in the list:
            if (total_adjustment == 0) // Timeout only works if no adjustment was made
                movement_move_to_face(0);
            break;

        case EVENT_LOW_ENERGY_UPDATE:
            // If you did not resign in EVENT_TIMEOUT, you can use this event to update the display once a minute.
            // Avoid displaying fast-updating values like seconds, since the display won't update again for 60 seconds.
            // You should also consider starting the tick animation, to show the wearer that this is sleep mode:
            // watch_start_tick_animation(500);
            break;

        default:
            movement_default_loop_handler(event, settings);
            break;
    }

    // return true if the watch can enter standby mode. If you are PWM'ing an LED or buzzing the buzzer here,
    // you should return false since the PWM driver does not operate in standby mode.
    return true;
}

void finetune_face_resign(movement_settings_t *settings, void *context) {
    (void) settings;
    (void) context;

    if (total_adjustment != 0) {
        finetune_update_correction_time();
    }
}