]> git.earman.xyz Git - sensor-watch.git/commitdiff
Precision watch update (#152)
authorMikhail Svarichevsky <3@14.by>
Tue, 10 Jan 2023 21:56:26 +0000 (00:56 +0300)
committerGitHub <noreply@github.com>
Tue, 10 Jan 2023 21:56:26 +0000 (16:56 -0500)
* Intermediate changes

* Databank working

* Main commit for precision timing

First version where all functions are supposed to be working

* Fix math error in nanosec. File storage for location.

* Remove obsolete comments

* Missing page name on pages rotation - thanks to jeremy

* Delete file.diff

* Cleanup+tempchart

1) finetune must always reset last calibration time when doing non-0 time correction, even when you are not applying ppm correction.
2) Dithers over 31 periods not 10, more resolution with still no risk of overflow
3) Minute-boundery finetune fix. I also just got this 1-minute error after finetune...
4) Write frequency calibration value in 1 operation rather than 2. All RTC writes must be single operations to avoid partially correct data.
5) Some code cleanup
6) Tempchart face is added for temperature statistics

* Update set_time_hackwatch_face.c

* Math error in display code of finetune, allow to update correction time even without correction - by long alarm press

* Increase reliability of stopping & starting RTC timer

As it's quite dangerous operation

* hackwatch - days adjust down fix by long alarm

* unify style

* More comments & last style change

* Simulator support

RTC operations (watch_rtc_enable and watch_rtc_freqcorr_write) are in common libs.

* Unicode fix

* Crystal aging is now adjustable (AA page in nanosec - annual aging, ppm/year)

Aging is baked into fixed offset every time finetune is performed, as it relies on last adjustment time.

* Blink on non-0 page every minute in finetune to measure clock error

* Rolling back private changes

* Cleanup

* Cleanup

* Quality of life changes in nanosec

1. Does not calculate & apply ppm correction if less than 6 hours passed since previous adjustment (as it gives very high correction values which are unrealistic and unhelpful)
2. Idle timeout resets to face 0 only if no correction was made

* unify style

* Fix low-power errors in nanosec infrastructure, faster display in finetune

* Merge fix

* unify style

Co-authored-by: Jeremy O'Brien <neutral@fastmail.com>
Co-authored-by: joeycastillo <joeycastillo@utexas.edu>
19 files changed:
movement/make/Makefile
movement/movement.c
movement/movement_faces.h
movement/watch_faces/clock/simple_clock_face.c
movement/watch_faces/complication/databank_face.c [new file with mode: 0644]
movement/watch_faces/complication/databank_face.h [new file with mode: 0644]
movement/watch_faces/complication/tempchart_face.c [new file with mode: 0644]
movement/watch_faces/complication/tempchart_face.h [new file with mode: 0644]
movement/watch_faces/settings/finetune_face.c [new file with mode: 0644]
movement/watch_faces/settings/finetune_face.h [new file with mode: 0644]
movement/watch_faces/settings/nanosec_face.c [new file with mode: 0644]
movement/watch_faces/settings/nanosec_face.h [new file with mode: 0644]
movement/watch_faces/settings/set_time_hackwatch_face.c [new file with mode: 0644]
movement/watch_faces/settings/set_time_hackwatch_face.h [new file with mode: 0644]
watch-library/hardware/watch/watch_rtc.c
watch-library/shared/watch/watch_private_display.c
watch-library/shared/watch/watch_private_display.h
watch-library/shared/watch/watch_rtc.h
watch-library/simulator/watch/watch_rtc.c

index 0e2a313ace9d1f03c780bb3e23ed3d05d33a2eef..680aa42c10d7560db7b48025b07595e6ccf50497 100644 (file)
@@ -46,6 +46,7 @@ SRCS += \
   ../watch_faces/clock/weeknumber_clock_face.c \
   ../watch_faces/settings/preferences_face.c \
   ../watch_faces/settings/set_time_face.c \
+  ../watch_faces/settings/set_time_hackwatch_face.c \
   ../watch_faces/sensor/thermistor_readout_face.c \
   ../watch_faces/sensor/thermistor_logging_face.c \
   ../watch_faces/sensor/thermistor_testing_face.c \
@@ -78,6 +79,10 @@ SRCS += \
   ../watch_faces/complication/rpn_calculator_alt_face.c \
   ../watch_faces/complication/stock_stopwatch_face.c \
   ../watch_faces/complication/tachymeter_face.c \
+  ../watch_faces/settings/nanosec_face.c \
+  ../watch_faces/settings/finetune_face.c \
+  ../watch_faces/complication/databank_face.c \
+  ../watch_faces/complication/tempchart_face.c \
 # New watch faces go above this line.
 
 # Leave this line at the bottom of the file; it has all the targets for making your project.
index 3997b4a4a7895e3e63d15404492a3b52693661a3..6c5f93a0d7c72c55e3050883957fdd9b55191ddf 100644 (file)
@@ -457,9 +457,7 @@ bool app_loop(void) {
         // (which would effectively disable the normal 'long press to face 0' behaviour).
         if (event.event_type == EVENT_MODE_LONG_PRESS 
             && !movement_state.watch_face_changed) {
-            if (movement_state.current_watch_face != 0) {
-                movement_move_to_face(0);
-            } else if (MOVEMENT_SECONDARY_FACE_INDEX) {
+            if (MOVEMENT_SECONDARY_FACE_INDEX && movement_state.current_watch_face == 0) {
                 movement_move_to_face(MOVEMENT_SECONDARY_FACE_INDEX);
             }
         }
index 7e62f873b156a6a10eb04dff4bd93f21d859391d..3ab32064ba9d96cd2f788048db6f436b147e7ce4 100644 (file)
@@ -29,6 +29,7 @@
 #include "world_clock_face.h"
 #include "preferences_face.h"
 #include "set_time_face.h"
+#include "set_time_hackwatch_face.h"
 #include "pulsometer_face.h"
 #include "thermistor_readout_face.h"
 #include "thermistor_logging_face.h"
 #include "weeknumber_clock_face.h"
 #include "stock_stopwatch_face.h"
 #include "tachymeter_face.h"
+#include "nanosec_face.h"
+#include "finetune_face.h"
+#include "databank_face.h"
+#include "tempchart_face.h"
 // New includes go above this line.
 
 #endif // MOVEMENT_FACES_H_
index fd1d167cbba6b8fd8e721c8683cc4fb05d8ae4eb..760559986dae422f8806810e3c2ade4e16148425 100644 (file)
@@ -26,6 +26,7 @@
 #include "simple_clock_face.h"
 #include "watch.h"
 #include "watch_utility.h"
+#include "watch_private_display.h"
 
 static void _update_alarm_indicator(bool settings_alarm_enabled, simple_clock_state_t *state) {
     state->alarm_enabled = settings_alarm_enabled;
@@ -96,8 +97,9 @@ bool simple_clock_face_loop(movement_event_t event, movement_settings_t *setting
 
             if ((date_time.reg >> 6) == (previous_date_time >> 6) && event.event_type != EVENT_LOW_ENERGY_UPDATE) {
                 // everything before seconds is the same, don't waste cycles setting those segments.
-                pos = 8;
-                sprintf(buf, "%02d", date_time.unit.second);
+                watch_display_character_lp_seconds('0' + date_time.unit.second / 10, 8);
+                watch_display_character_lp_seconds('0' + date_time.unit.second % 10, 9);
+                break;
             } else if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) {
                 // everything before minutes is the same.
                 pos = 6;
diff --git a/movement/watch_faces/complication/databank_face.c b/movement/watch_faces/complication/databank_face.c
new file mode 100644 (file)
index 0000000..9a5b333
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Mikhail Svarichevsky
+ *
+ * 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.
+ *
+ * Displays some pre-defined data that you might want to remember. Math constants, birthdays, phone numbers...
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "databank_face.h"
+#include "watch.h"
+#include "watch_private_display.h"
+
+const char *pi_data[] = {
+    "PI", "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442",
+    "S ", "9192631770",
+    "31", "2147483648",
+    "32", "4294967296",
+    "63", "9223372036854775808",
+    "64", "18446744073709551616",
+};
+//we show 6 characters per screen
+
+const int databank_num_pages = (sizeof(pi_data) / sizeof(char*) / 2);
+
+struct {
+    uint8_t current_word;
+    uint8_t databank_page;
+    bool animating;
+} databank_state;
+
+void databank_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
+    // These next two lines just silence the compiler warnings associated with unused parameters.
+    // We have no use for the settings or the watch_face_index, so we make that explicit here.
+    (void) settings;
+    (void) context_ptr;
+    (void) watch_face_index;
+    // At boot, context_ptr will be NULL indicating that we don't have anyplace to store our context.
+}
+
+void databank_face_activate(movement_settings_t *settings, void *context) {
+    // same as above: silence the warning, we don't need to check the settings.
+    (void) settings;
+    (void) context;
+    // we do however need to set some things in our context. Here we cast it to the correct type...
+    databank_state.current_word = 0;
+    databank_state.animating = true;
+}
+
+static void display()
+{
+    char buf[14];
+    int page = databank_state.current_word;
+    sprintf(buf, "%s%2d", pi_data[databank_state.databank_page * 2 + 0], page);
+    watch_display_string(buf, 0);
+    bool data_ended = false;
+    for (int i = 0; i < 6; i++) {
+        if (pi_data[databank_state.databank_page * 2 + 1][page * 6 + i] == 0) {
+            data_ended = true;
+        }
+
+        if (!data_ended) {
+            watch_display_character(pi_data[databank_state.databank_page * 2 + 1][page * 6 + i], 4 + i);
+        } else {
+            // only 6 digits per page
+            watch_display_character(' ', 4 + i);
+        }
+    }
+}
+
+bool databank_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+    int max_words = (strlen(pi_data[databank_state.databank_page * 2 + 1]) - 1) / 6 + 1;
+
+    switch (event.event_type) {
+        case EVENT_ACTIVATE:
+             display();
+        case EVENT_TICK:
+            // on activate and tick, if we are animating,
+            break;
+        case EVENT_LIGHT_BUTTON_UP:
+            // when the user presses 'light', we illuminate the LED. We could override this if
+            // our UI needed an additional button for input, consuming the light button press
+            // but not illuminating the LED.
+            databank_state.current_word = (databank_state.current_word + max_words - 1) % max_words;
+            display();
+            break;
+        case EVENT_LIGHT_LONG_PRESS:
+            databank_state.databank_page = (databank_state.databank_page + databank_num_pages - 1) % databank_num_pages;
+            databank_state.current_word = 0;
+            display();
+            break;
+        case EVENT_MODE_BUTTON_UP:
+            // when the user presses 'mode', we tell movement to move to the next watch face.
+            // movement will call our resign function, clear the screen, and transfer control
+            // to the next watch face in the list.
+            movement_move_to_next_face();
+            break;
+        case EVENT_ALARM_LONG_PRESS:
+            databank_state.databank_page = (databank_state.databank_page + 1) % databank_num_pages;
+            databank_state.current_word = 0;
+            display();
+            break;
+        case EVENT_ALARM_BUTTON_UP:
+            // when the user presses 'alarm', we toggle the state of the animation. If animating,
+            // we stop; if stopped, we resume.
+            databank_state.current_word = (databank_state.current_word + 1) % max_words;
+            display();
+            break;
+        case EVENT_LOW_ENERGY_UPDATE:
+            // This low energy mode update occurs once a minute, if the watch face is in the
+            // foreground when Movement enters low energy mode. We have the option of supporting
+            // this mode, but since our watch face animates once a second, the "Hello there" face
+            // isn't very useful in this mode. So we choose not to support it. (continued below)
+            break;
+        case EVENT_TIMEOUT:
+            // ... Instead, we respond to the timeout event. This event happens after a configurable
+            // interval on screen (1-30 minutes). The watch will give us this event as a chance to
+            // resign control if we want to, and in this case, we do.
+            // This function will return the watch to the first screen (usually a simple clock),
+            // and it will do it long before the watch enters low energy mode. This ensures we
+            // won't be on screen, and thus opts us out of getting the EVENT_LOW_ENERGY_UPDATE above.
+            movement_move_to_face(0);
+        default:
+            break;
+    }
+
+    return true;
+}
+
+void databank_face_resign(movement_settings_t *settings, void *context) {
+    // our watch face, like most watch faces, has nothing special to do when resigning.
+    // watch faces that enable a peripheral or interact with a sensor may want to turn it off here.
+    (void) settings;
+    (void) context;
+}
diff --git a/movement/watch_faces/complication/databank_face.h b/movement/watch_faces/complication/databank_face.h
new file mode 100644 (file)
index 0000000..1f204b2
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Joey Castillo
+ *
+ * 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.
+ */
+
+#ifndef DATABANK_FACE_H_
+#define DATABANK_FACE_H_
+
+#include "movement.h"
+
+void databank_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void databank_face_activate(movement_settings_t *settings, void *context);
+bool databank_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void databank_face_resign(movement_settings_t *settings, void *context);
+
+#define databank_face ((const watch_face_t){ \
+    databank_face_setup, \
+    databank_face_activate, \
+    databank_face_loop, \
+    databank_face_resign, \
+    NULL, \
+})
+
+#endif // DATABANK_FACE_H_
diff --git a/movement/watch_faces/complication/tempchart_face.c b/movement/watch_faces/complication/tempchart_face.c
new file mode 100644 (file)
index 0000000..f2bb50a
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Mikhail Svarichevsky
+ *
+ * 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.
+ *
+ * Gathers temperature statistics in a chart form. Statistics bins are per hour / per 0.5°C.
+ * Saved to file every day at 00:00. Can help improve watch precision in the future. 
+ * If you can gather statistics over few months, and then send tempchart.ini to 3@14.by - it 
+ * will help future generations of precision quartz watches. 
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "tempchart_face.h"
+#include "watch.h"
+#include "watch_private_display.h"
+#include "filesystem.h"
+#include "thermistor_driver.h"
+
+struct {
+    uint8_t stat[24 * 70];
+    uint16_t num_div;
+} tempchart_state;
+
+static void tempchart_save(void) {
+    filesystem_write_file("tempchart.ini", (char*)&tempchart_state, sizeof(tempchart_state));
+}
+
+void tempchart_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
+    // These next two lines just silence the compiler warnings associated with unused parameters.
+    // We have no use for the settings or the watch_face_index, so we make that explicit here.
+    (void) settings;
+    (void) context_ptr;
+    (void) watch_face_index;
+    // At boot, context_ptr will be NULL indicating that we don't have anyplace to store our context.
+    if (filesystem_get_file_size("tempchart.ini") != sizeof(tempchart_state)) {
+        // No previous ini or old version of ini file - create new config file
+        tempchart_state.num_div = 0;
+        for (int i = 0; i < 24 * 70; i++)
+            tempchart_state.stat[i] = 0;
+        tempchart_save();
+    } else
+        filesystem_read_file("tempchart.ini", (char*)&tempchart_state, sizeof(tempchart_state));
+
+}
+
+void tempchart_face_activate(movement_settings_t *settings, void *context) {
+    // same as above: silence the warning, we don't need to check the settings.
+    (void) settings;
+    (void) context;
+}
+
+static void display(void) {
+    int sum = 0;
+    for (int i = 0; i < 24 * 70; i++)
+        sum += tempchart_state.stat[i];
+
+    char buf[24];
+    sprintf(buf, "TS%2d%6d", tempchart_state.num_div, sum);
+    watch_display_string(buf, 0);
+}
+
+bool tempchart_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+
+    switch (event.event_type) {
+        case EVENT_ACTIVATE:
+             display();
+        case EVENT_TICK:
+            // on activate and tick, if we are animating,
+            break;
+        case EVENT_MODE_BUTTON_UP:
+            // when the user presses 'mode', we tell movement to move to the next watch face.
+            // movement will call our resign function, clear the screen, and transfer control
+            // to the next watch face in the list.
+            movement_move_to_next_face();
+            break;
+        case EVENT_LOW_ENERGY_UPDATE:
+            // This low energy mode update occurs once a minute, if the watch face is in the
+            // foreground when Movement enters low energy mode. We have the option of supporting
+            // this mode, but since our watch face animates once a second, the "Hello there" face
+            // isn't very useful in this mode. So we choose not to support it. (continued below)
+            break;
+        case EVENT_TIMEOUT:
+            // ... Instead, we respond to the timeout event. This event happens after a configurable
+            // interval on screen (1-30 minutes). The watch will give us this event as a chance to
+            // resign control if we want to, and in this case, we do.
+            // This function will return the watch to the first screen (usually a simple clock),
+            // and it will do it long before the watch enters low energy mode. This ensures we
+            // won't be on screen, and thus opts us out of getting the EVENT_LOW_ENERGY_UPDATE above.
+            movement_move_to_face(0);
+            break;
+        case EVENT_BACKGROUND_TASK:
+            // Here we measure temperature and do main frequency correction
+            thermistor_driver_enable();
+            float temperature_c = thermistor_driver_get_temperature();
+            thermistor_driver_disable();
+            watch_date_time date_time = watch_rtc_get_date_time();
+
+            int temp = round(temperature_c * 2);
+            if ((temp < 0) || (temp >= 70)) break;
+
+            if (tempchart_state.stat[date_time.unit.hour + temp * 24] == 255) { // We've reached the limit
+              tempchart_state.num_div++;
+              for (int i = 0; i < 24 * 70; i++)
+                tempchart_state.stat[i] = (tempchart_state.stat[i] + 1) >> 1; // So that we don't lose 1
+            }
+            tempchart_state.stat[date_time.unit.hour+temp*24]++;
+
+            if (date_time.unit.hour == 0 && date_time.unit.minute == 10)
+                tempchart_save();
+
+            break;
+
+        default:
+            break;
+    }
+
+    return true;
+}
+
+void tempchart_face_resign(movement_settings_t *settings, void *context) {
+    // our watch face, like most watch faces, has nothing special to do when resigning.
+    // watch faces that enable a peripheral or interact with a sensor may want to turn it off here.
+    (void) settings;
+    (void) context;
+}
+
+//background freq correction
+bool tempchart_face_wants_background_task(movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+    watch_date_time date_time = watch_rtc_get_date_time();
+
+    //Updating data every 5 minutes
+    return date_time.unit.minute % 5 == 0;
+}
diff --git a/movement/watch_faces/complication/tempchart_face.h b/movement/watch_faces/complication/tempchart_face.h
new file mode 100644 (file)
index 0000000..ce870c8
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Joey Castillo
+ *
+ * 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.
+ */
+
+#ifndef TEMPCHART_FACE_H_
+#define TEMPCHART_FACE_H_
+
+#include "movement.h"
+
+void tempchart_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void tempchart_face_activate(movement_settings_t *settings, void *context);
+bool tempchart_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void tempchart_face_resign(movement_settings_t *settings, void *context);
+bool tempchart_face_wants_background_task(movement_settings_t *settings, void *context);
+
+
+#define tempchart_face ((const watch_face_t){ \
+    tempchart_face_setup, \
+    tempchart_face_activate, \
+    tempchart_face_loop, \
+    tempchart_face_resign, \
+    tempchart_face_wants_background_task, \
+})
+
+#endif // TEMPCHART_FACE_H_
diff --git a/movement/watch_faces/settings/finetune_face.c b/movement/watch_faces/settings/finetune_face.c
new file mode 100644 (file)
index 0000000..f328cbc
--- /dev/null
@@ -0,0 +1,257 @@
+/*
+ * 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:
+            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();
+    }
+}
diff --git a/movement/watch_faces/settings/finetune_face.h b/movement/watch_faces/settings/finetune_face.h
new file mode 100644 (file)
index 0000000..6a80bf2
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+#ifndef FINETUNE_FACE_H_
+#define FINETUNE_FACE_H_
+
+#include "movement.h"
+
+typedef struct {
+    // Anything you need to keep track of, put it here!
+    uint8_t unused;
+} finetune_state_t;
+
+void finetune_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void finetune_face_activate(movement_settings_t *settings, void *context);
+bool finetune_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void finetune_face_resign(movement_settings_t *settings, void *context);
+
+#define finetune_face ((const watch_face_t){ \
+    finetune_face_setup, \
+    finetune_face_activate, \
+    finetune_face_loop, \
+    finetune_face_resign, \
+    NULL, \
+})
+
+#endif // FINETUNE_FACE_H_
+
diff --git a/movement/watch_faces/settings/nanosec_face.c b/movement/watch_faces/settings/nanosec_face.c
new file mode 100644 (file)
index 0000000..e28b035
--- /dev/null
@@ -0,0 +1,392 @@
+/*
+ * 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.
+ */
+
+/*
+ * The goal of nanosec face is dramatic improvement of SensorWatch accuracy.
+ * Minimum goal is <60 seconds of error per year. Full success is if we can reach <15 seconds per year (<0.47ppm error).
+ *
+ * It implements temperature correction using tempco from datasheet (and allows to adjust these)
+ * and allows to introduce offset fix. Therefore requires temperature sensor board.
+ *
+ * Most users will need to apply profile 3 ("default") or 2("conservative datasheet"), and tune first parameter -
+ * static offset (as it's different for every crystal sample).
+ *
+ * Frequency correction is dithered over 31 correction intervals (31x10 minutes or ~5 hours), to allow <0.1ppm correction resolution.
+ * 1ppm is 0.0864 sec per day.
+ * 0.1ppm is 0.00864 sec per day.
+ *
+ * To stay under 1ppm error you would need calibration of your specific instance of quartz crystal after some "burn-in" (ideally 1 year).
+ *
+ * Should improve TOTP experience.
+ *
+ * Default funing fork tempco: -0.034 ppm/°C², centered around 25°C
+ * We add optional cubic coefficient, which was measured in practice on my sample.
+ *
+ * Cadence (CD) - how many minutes between corrections. Default 10 minutes.
+ * Every minute might be too much. Every hour - slightly less power consumption but also less precision.
+ *
+ * Can compensate crystal aging (ppm/year) - but you really should be worrying about it on second/third years of watch calibration. *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "thermistor_driver.h"
+#include "nanosec_face.h"
+#include "filesystem.h"
+#include "watch_utility.h"
+
+int16_t freq_correction_residual = 0; // Dithering 0.1ppm correction, does not need to be configured.
+int16_t freq_correction_previous = -30000;
+#define dithering 31
+
+nanosec_state_t nanosec_state;
+
+#define nanosec_max_screen 7
+int8_t nanosec_screen = 0;
+bool nanosec_changed = false; // We try to avoid saving settings when no changes were made, for example when just browsing through face
+
+const float voltage_coefficient = 0.241666667 * dithering; // 10 * ppm/V. Nominal frequency is at 3V.
+
+static void nanosec_init_profile(void) {
+    nanosec_changed = true;
+    nanosec_state.correction_cadence = 10;
+    watch_date_time date_time = watch_rtc_get_date_time();
+    nanosec_state.last_correction_time = watch_utility_date_time_to_unix_time(date_time, 0);
+
+    // init data after changing profile - do that once per profile selection
+    switch (nanosec_state.correction_profile) {
+        case 0: // No tempco, no dithering
+            nanosec_state.freq_correction = 0;
+            nanosec_state.center_temperature = 2500;
+            nanosec_state.quadratic_tempco = 0;
+            nanosec_state.cubic_tempco = 0;
+            nanosec_state.aging_ppm_pa = 0;
+            break;
+        case 1: // No tempco, with dithering
+            nanosec_state.freq_correction = 0;
+            nanosec_state.center_temperature = 2500;
+            nanosec_state.quadratic_tempco = 0;
+            nanosec_state.cubic_tempco = 0;
+            nanosec_state.aging_ppm_pa = 0;
+            break;
+        case 2: // Datasheet correction
+            nanosec_state.freq_correction = 0;
+            nanosec_state.center_temperature = 2500;
+            nanosec_state.quadratic_tempco = 3400;
+            nanosec_state.cubic_tempco = 0;
+            nanosec_state.aging_ppm_pa = 0;
+            break;
+        case 3: // Datasheet correction + cubic coefficient
+            nanosec_state.freq_correction = 0;
+            nanosec_state.center_temperature = 2500;
+            nanosec_state.quadratic_tempco = 3400;
+            nanosec_state.cubic_tempco = 1360;
+            nanosec_state.aging_ppm_pa = 0;
+            break;
+        case 4: // Full custom
+            nanosec_state.freq_correction = 1768;
+            nanosec_state.center_temperature = 2653;
+            nanosec_state.quadratic_tempco = 4091;
+            nanosec_state.cubic_tempco = 1359;
+            nanosec_state.aging_ppm_pa = 0;
+            break;
+    }
+}
+
+static void nanosec_internal_write_RTC_correction(int16_t value, int16_t sign) {
+    if (sign == 0) {
+        if (value == freq_correction_previous)
+            return; // Do not write same correction value twice
+        freq_correction_previous = value;
+    } else {
+        if (value == -freq_correction_previous)
+            return; // Do not write same correction value twice
+        freq_correction_previous = -value;
+    }
+
+    watch_rtc_freqcorr_write(value, sign);
+}
+
+// Receives clock correction, already corrected for temperature and battery voltage, multiplied by "dithering"
+static void apply_RTC_correction(int16_t correction) {
+    correction += freq_correction_residual;
+    int32_t correction_lr = (int32_t)correction * 2 / dithering; // int division
+    if (correction_lr & 1) {
+        if (correction_lr > 0) {
+            correction_lr++;
+        } else {
+            correction_lr--;
+        }
+    }
+    correction_lr >>= 1;
+    freq_correction_residual = correction - correction_lr * dithering;
+
+    // Warning! Freqcorr is not signed int8!!
+    // First we clamp it to 8-bit range
+    if (correction_lr > 127) {
+        nanosec_internal_write_RTC_correction(127, 0);
+    } else if (correction_lr < -127) {
+        nanosec_internal_write_RTC_correction(127, 1);
+    } else if (correction_lr < 0) {
+        nanosec_internal_write_RTC_correction(abs(correction_lr), 1);
+    } else { // correction
+        nanosec_internal_write_RTC_correction(correction_lr, 0);
+    }
+}
+
+// User-related saves
+void nanosec_ui_save(void) {
+    if (nanosec_changed)
+        nanosec_save();
+}
+
+// This is low-level save function, that can be used by other faces
+void nanosec_save(void) {
+    if (nanosec_state.correction_profile == 0) {
+        freq_correction_residual = 0;
+        apply_RTC_correction(nanosec_state.freq_correction * 1.0f * dithering / 100); // Will be divided by dithering inside, final resolution is mere 1ppm
+    }
+
+    filesystem_write_file("nanosec.ini", (char*)&nanosec_state, sizeof(nanosec_state));
+    nanosec_changed = false;
+}
+
+void nanosec_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
+    (void) watch_face_index;
+    (void) settings;
+
+    if (*context_ptr == NULL) {
+        if (filesystem_get_file_size("nanosec.ini") != sizeof(nanosec_state)) {
+            // No previous ini or old version of ini file - create new config file
+            nanosec_state.correction_profile = 3;
+            nanosec_init_profile();
+            nanosec_ui_save();
+        } else {
+            filesystem_read_file("nanosec.ini", (char*)&nanosec_state, sizeof(nanosec_state));
+        }
+
+        freq_correction_residual = 0;
+        nanosec_screen = 0;
+
+        *context_ptr = (void *)1; // No need to re-read from filesystem when exiting low power mode
+    }
+}
+
+void nanosec_face_activate(movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+
+    // Handle any tasks related to your watch face coming on screen.
+    nanosec_changed = false;
+}
+
+static void nanosec_update_display() {
+    char buf[14];
+
+    switch (nanosec_screen) {
+        case 0:
+            sprintf(buf, "FC  %6d", nanosec_state.freq_correction);
+            break;
+        case 1:
+            sprintf(buf, "T0  %6d", nanosec_state.center_temperature);
+            break;
+        case 2:
+            sprintf(buf, "2C  %6d", nanosec_state.quadratic_tempco);
+            break;
+        case 3:
+            sprintf(buf, "3C  %6d", nanosec_state.cubic_tempco);
+            break;
+        case 4: // Profile
+            sprintf(buf, "PR      P%1d", nanosec_state.correction_profile);
+            break;
+        case 5: // Cadence
+            sprintf(buf, "CD      %2d", nanosec_state.correction_cadence);
+            break;
+        case 6: // Aging
+            sprintf(buf, "AA  %6d", nanosec_state.aging_ppm_pa);
+            break;
+    }
+    watch_display_string(buf, 0);
+}
+
+static void value_increase(int16_t delta) {
+    nanosec_changed = true;
+
+    switch (nanosec_screen) {
+        case 0:
+            nanosec_state.freq_correction += delta;
+            break;
+        case 1:
+            nanosec_state.center_temperature += delta;
+            break;
+        case 2:
+            nanosec_state.quadratic_tempco += delta;
+            break;
+        case 3:
+            nanosec_state.cubic_tempco += delta;
+            break;
+        case 4: // Profile
+            nanosec_state.correction_profile = (nanosec_state.correction_profile + delta) % nanosec_profile_count;
+            break;
+        case 5: // Cadence
+            switch (nanosec_state.correction_cadence) {
+                case 1:
+                    nanosec_state.correction_cadence = (delta > 0) ? 5 : 60;
+                    break;
+                case 5:
+                    nanosec_state.correction_cadence = (delta > 0) ? 10 : 1;
+                    break;
+                case 10:
+                    nanosec_state.correction_cadence = (delta > 0) ? 20 : 5;
+                    break;
+                case 20:
+                    nanosec_state.correction_cadence = (delta > 0) ? 60 : 10;
+                    break;
+                case 60:
+                    nanosec_state.correction_cadence = (delta > 0) ? 1 : 20;
+                    break;
+            }
+            nanosec_state.correction_profile = (nanosec_state.correction_profile + delta) % nanosec_profile_count;
+            break;
+        case 6: // Aging
+            nanosec_state.aging_ppm_pa += delta;
+            break;
+    }
+
+    nanosec_update_display();
+}
+
+static void nanosec_next_edit_screen(void) {
+    nanosec_screen = (nanosec_screen + 1) % nanosec_max_screen;
+    nanosec_update_display();
+}
+
+float nanosec_get_aging() // Returns aging correction in ppm
+{
+    watch_date_time date_time = watch_rtc_get_date_time();
+    float years = (watch_utility_date_time_to_unix_time(date_time, 0) - nanosec_state.last_correction_time) / 31536000.0f; // Years passed since finetune
+    return years*nanosec_state.aging_ppm_pa/100.0f;
+}
+
+
+bool nanosec_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.
+            nanosec_screen = 0; // Start at page 0
+            nanosec_update_display();
+            break;
+        case EVENT_TICK:
+            break;
+        case EVENT_MODE_BUTTON_UP:
+            if (nanosec_screen == 0) { // we can exit face only on the 0th page
+                nanosec_ui_save();
+                movement_move_to_next_face();
+            } else {
+                nanosec_next_edit_screen();
+            }
+            break;
+        case EVENT_MODE_LONG_PRESS:
+            nanosec_next_edit_screen();
+            break;
+        case EVENT_LIGHT_BUTTON_UP:
+            value_increase(1);
+            break;
+        case EVENT_LIGHT_LONG_PRESS:
+            if (nanosec_screen == 4) { // If we are in profile - apply profiles
+                nanosec_init_profile();
+                nanosec_screen = 0;
+                nanosec_update_display();
+            } else {
+                value_increase(50);
+            }
+            break;
+        case EVENT_ALARM_BUTTON_UP:
+            value_increase(-1);
+            break;
+        case EVENT_ALARM_LONG_PRESS:
+            value_increase(-50);
+            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:
+            // 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;
+        case EVENT_BACKGROUND_TASK:
+            // Here we measure temperature and do main frequency correction
+            thermistor_driver_enable();
+            float temperature_c = thermistor_driver_get_temperature();
+            float voltage = (float)watch_get_vcc_voltage() / 1000.0;
+            thermistor_driver_disable();
+            // L22 correction scaling is 0.95367ppm per 1 in FREQCORR
+            // At wrong temperature crystall starting to run slow, negative correction will speed up frequency to correct
+            // Default 32kHz correciton factor is -0.034, centered around 25°C
+            float dt = temperature_c - nanosec_state.center_temperature / 100.0;
+
+            int16_t correction = round((
+                        nanosec_state.freq_correction / 100.0f * dithering +
+                        (-nanosec_state.quadratic_tempco / 100000.0 * dithering) * dt * dt +
+                        (nanosec_state.cubic_tempco / 10000000.0 * dithering) * dt * dt * dt +
+                        (voltage - 3.0) * voltage_coefficient +
+                        nanosec_get_aging() * dithering
+                        ) / 0.95367); // 1 correction unit is 0.095367ppm.
+
+            apply_RTC_correction(correction);
+            break;
+        default:
+            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 nanosec_face_resign(movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+
+    nanosec_ui_save();
+}
+
+// Background freq correction
+bool nanosec_face_wants_background_task(movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+    if (nanosec_state.correction_profile == 0)
+        return 0; // No need for background correction if we are on profile 0 - static hardware correction.
+    watch_date_time date_time = watch_rtc_get_date_time();
+
+    return date_time.unit.minute % nanosec_state.correction_cadence == 0;
+}
diff --git a/movement/watch_faces/settings/nanosec_face.h b/movement/watch_faces/settings/nanosec_face.h
new file mode 100644 (file)
index 0000000..044275a
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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.
+ */
+
+#ifndef NANOSEC_FACE_H_
+#define NANOSEC_FACE_H_
+
+#include "movement.h"
+
+#define nanosec_profile_count 5
+typedef struct {
+    // Correction profiles:
+    // 0 - static hardware correction.
+    // 1 - static correction with dithering.
+    // 2 - datasheet quadratic correction (universal).
+    // 3 - cubic correction conservative (likely universal).
+    // 4 - cubic correction finetuned (sample-specific).
+    int8_t correction_profile;
+    int16_t freq_correction; // Static correction - multiplied by 100
+    int16_t center_temperature; // Multiplied by 100, +25.0 -> +2500
+    int16_t quadratic_tempco; // 0.034 -> 3400, multiplied by 100000. Stored positive, used as negative.
+    int16_t cubic_tempco; // default 0, 0.000136 -> 1360, multiplied by 10000000. Stored positive, used positive.
+    int8_t correction_cadence;
+    uint32_t last_correction_time; // Not used at the moment - but will in the future
+    int16_t aging_ppm_pa; // multiplied by 100. Aging per year. 
+} nanosec_state_t;
+
+void nanosec_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void nanosec_face_activate(movement_settings_t *settings, void *context);
+bool nanosec_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void nanosec_face_resign(movement_settings_t *settings, void *context);
+bool nanosec_face_wants_background_task(movement_settings_t *settings, void *context);
+void nanosec_ui_save(void);
+void nanosec_save(void);
+float nanosec_get_aging(void);
+
+
+#define nanosec_face ((const watch_face_t) { \
+    nanosec_face_setup, \
+    nanosec_face_activate, \
+    nanosec_face_loop, \
+    nanosec_face_resign, \
+    nanosec_face_wants_background_task, \
+})
+
+#endif // NANOSEC_FACE_H_
+
diff --git a/movement/watch_faces/settings/set_time_hackwatch_face.c b/movement/watch_faces/settings/set_time_hackwatch_face.c
new file mode 100644 (file)
index 0000000..79f0d35
--- /dev/null
@@ -0,0 +1,286 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Joey Castillo
+ * 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.
+ * 
+ * 
+ * 
+ * This is an extended version of set_time face which allow setting seconds precisely.
+ * To achieve that - press and hold alarm button few seconds before 00 and release exaclty as reference clock turns 00.
+ * All settings can go up, or down (long alarm press).
+ * 
+ * The challenge is that SensorWatch display is delayed 0.5 seconds vs hardware RTC clock. It is caused by interrupts being generated by raising
+ * edge of counter. It means there is no way to precisely trigger at 0.5s, as events at different frequencies slightly mismatch. 
+ * This watch face achieves this approximately by triggering at 15th out of 32Hz events.
+ * 
+ * If you are <30 seconds when setting seconds - you will stay in the same minute. Otherwise - you will go to next minute. 
+ * 
+ * Note that changing anything will slightly delay subseconds counter. This is why this face sets seconds last 
+ * to achiveve best precision. Still, best possible precision is achieved with finetune face. 
+ */
+
+#include <stdlib.h>
+#include "set_time_hackwatch_face.h"
+#include "watch.h"
+
+char set_time_hackwatch_face_titles[][3] = {"HR", "M1", "SE", "YR", "MO", "DA", "ZO"};
+#define set_time_hackwatch_face_NUM_SETTINGS (sizeof(set_time_hackwatch_face_titles) / sizeof(*set_time_hackwatch_face_titles))
+
+watch_date_time date_time_settings;
+
+void set_time_hackwatch_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
+    (void) settings;
+    (void) watch_face_index;
+    if (*context_ptr == NULL) *context_ptr = malloc(sizeof(uint8_t));
+}
+
+void set_time_hackwatch_face_activate(movement_settings_t *settings, void *context) {
+    (void) settings;
+    *((uint8_t *)context) = 3;
+    movement_request_tick_frequency(32);
+    date_time_settings = watch_rtc_get_date_time();
+}
+
+bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
+    uint8_t current_page = *((uint8_t *)context);
+    const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
+
+    if (event.subsecond == 15) // Delay displayed time update by ~0.5 seconds, to align phase exactly to main clock at 1Hz
+        date_time_settings = watch_rtc_get_date_time();
+
+    static int8_t seconds_reset_sequence;
+
+    switch (event.event_type) {
+        case EVENT_MODE_BUTTON_UP:
+            if (current_page == 2)
+                watch_rtc_enable(true);
+            movement_move_to_next_face();
+            return false;
+        case EVENT_LIGHT_LONG_PRESS:
+            current_page = (current_page + set_time_hackwatch_face_NUM_SETTINGS - 1) % set_time_hackwatch_face_NUM_SETTINGS;
+            if (current_page == 2)
+                seconds_reset_sequence = 0;
+
+            *((uint8_t *)context) = current_page;
+            break;
+        case EVENT_LIGHT_BUTTON_UP:
+            if (current_page == 2)
+                watch_rtc_enable(true);
+
+            current_page = (current_page + 1) % set_time_hackwatch_face_NUM_SETTINGS;
+            if (current_page == 2)
+                seconds_reset_sequence = 0;
+
+            *((uint8_t *)context) = current_page;
+            break;
+        case EVENT_TICK:
+            // We use it to wait for "middle" subsecond position
+            if (current_page == 2 && seconds_reset_sequence == 1 && event.subsecond == 15) { // wait ~0.5sec - until we reach half second point
+                watch_rtc_enable(false);
+                seconds_reset_sequence = 2;
+
+                // Set new time while RTC is off, to get perfect start
+                if (date_time_settings.unit.second > 30) {
+                    date_time_settings.unit.minute = (date_time_settings.unit.minute + 1) % 60; // Roll to next minute if we are almost there
+                    if (date_time_settings.unit.minute == 0) { // Overflow
+                        date_time_settings.unit.hour = (date_time_settings.unit.hour + 1) % 24;
+                        if (date_time_settings.unit.hour == 0) // Overflow
+                            date_time_settings.unit.day++;
+                    }
+                }
+                date_time_settings.unit.second = 0;
+                watch_rtc_set_date_time(date_time_settings);
+            }
+            break;
+        case EVENT_ALARM_BUTTON_DOWN:
+            if (current_page == 2) {
+                watch_rtc_enable(true); // If it is disabled accidentally - re-enable it
+                seconds_reset_sequence = 1; // Waiting for whole second
+            }
+            break;
+        case EVENT_ALARM_LONG_PRESS:
+            switch (current_page) {
+                case 0: // hour
+                    date_time_settings.unit.hour = (date_time_settings.unit.hour + 24 -1) % 24;
+                    break;
+                case 1: // minute
+                    date_time_settings.unit.minute = (date_time_settings.unit.minute + 60 - 1) % 60;
+                    break;
+                case 3: // year
+                    // only allow 2021-2061. fix this sometime later
+                    date_time_settings.unit.year = (date_time_settings.unit.year + 50 - 1) % 50;
+                    break;
+                case 4: // month
+                    date_time_settings.unit.month = (date_time_settings.unit.month + 12 - 2) % 12 + 1;
+                    break;
+                case 5: // day
+                    date_time_settings.unit.day = date_time_settings.unit.day - 2;
+                    // can't set to the 29th on a leap year. if it's february 29, set to 11:59 on the 28th.
+                    // and it should roll over.
+                    if (date_time_settings.unit.day == 0) {
+                        date_time_settings.unit.day = days_in_month[date_time_settings.unit.month - 1];
+                    } else
+                        date_time_settings.unit.day++;
+                    break;
+                case 6: // time zone
+                    if (settings->bit.time_zone > 0) {
+                        settings->bit.time_zone--;
+                    } else {
+                        settings->bit.time_zone = 40;
+                    }
+                    break;
+            }
+            if (current_page != 2) // Do not set time when we are at seconds, it was already set previously
+                watch_rtc_set_date_time(date_time_settings);
+            break;
+        
+        case EVENT_ALARM_LONG_UP://Setting seconds on long release
+            switch (current_page) {
+                case 2: // second
+                    seconds_reset_sequence = 0;
+                    watch_rtc_enable(true);
+                    break;
+            }
+            break;
+        case EVENT_ALARM_BUTTON_UP:
+            switch (current_page) {
+                case 0: // hour
+                    date_time_settings.unit.hour = (date_time_settings.unit.hour + 1) % 24;
+                    break;
+                case 1: // minute
+                    date_time_settings.unit.minute = (date_time_settings.unit.minute + 1) % 60;
+                    break;
+                case 2: // second
+                    seconds_reset_sequence = 0;
+                    watch_rtc_enable(true);
+                    break;
+                case 3: // year
+                    // only allow 2021-2061. fix this sometime later
+                    date_time_settings.unit.year = ((date_time_settings.unit.year % 50) + 1);
+                    break;
+                case 4: // month
+                    date_time_settings.unit.month = (date_time_settings.unit.month % 12) + 1;
+                    break;
+                case 5: // day
+                    date_time_settings.unit.day = date_time_settings.unit.day + 1;
+                    // can't set to the 29th on a leap year. if it's february 29, set to 11:59 on the 28th.
+                    // and it should roll over.
+                    if (date_time_settings.unit.day > days_in_month[date_time_settings.unit.month - 1]) {
+                        date_time_settings.unit.day = 1;
+                    }
+                    break;
+                case 6: // time zone
+                    settings->bit.time_zone++;
+                    if (settings->bit.time_zone > 40) settings->bit.time_zone = 0;
+                    break;
+            }
+            if (current_page != 2) // Do not set time when we are at seconds, it was already set previously
+                watch_rtc_set_date_time(date_time_settings);
+            //TODO: Do not update whole RTC, just what we are changing
+            break;
+        case EVENT_TIMEOUT:
+            movement_move_to_face(0);
+            break;
+        default:
+            break;
+    }
+
+    char buf[11];
+    if (current_page < 3) {
+        watch_set_colon();
+        if (settings->bit.clock_mode_24h) {
+            watch_set_indicator(WATCH_INDICATOR_24H);
+            sprintf(buf,
+                    "%s  %2d%02d%02d",
+                    set_time_hackwatch_face_titles[current_page],
+                    date_time_settings.unit.hour,
+                    date_time_settings.unit.minute,
+                    date_time_settings.unit.second);
+        } else {
+            sprintf(buf,
+                    "%s  %2d%02d%02d",
+                    set_time_hackwatch_face_titles[current_page],
+                    (date_time_settings.unit.hour % 12) ? (date_time_settings.unit.hour % 12) : 12,
+                    date_time_settings.unit.minute,
+                    date_time_settings.unit.second);
+            if (date_time_settings.unit.hour < 12) {
+                watch_clear_indicator(WATCH_INDICATOR_PM);
+            } else {
+                watch_set_indicator(WATCH_INDICATOR_PM);
+            }
+        }
+    } else if (current_page < 6) {
+        watch_clear_colon();
+        watch_clear_indicator(WATCH_INDICATOR_24H);
+        watch_clear_indicator(WATCH_INDICATOR_PM);
+        sprintf(buf,
+                "%s  %2d%02d%02d",
+                set_time_hackwatch_face_titles[current_page],
+                date_time_settings.unit.year + 20,
+                date_time_settings.unit.month,
+                date_time_settings.unit.day);
+    } else {
+        if ((event.subsecond / 8 ) % 2) {
+            watch_clear_colon();
+            sprintf(buf, "%s        ", set_time_hackwatch_face_titles[current_page]);
+        } else {
+            watch_set_colon();
+            sprintf(buf,
+                    "%s %3d%02d  ",
+                    set_time_hackwatch_face_titles[current_page],
+                    (int8_t)(movement_timezone_offsets[settings->bit.time_zone] / 60),
+                    (int8_t)(movement_timezone_offsets[settings->bit.time_zone] % 60) * (movement_timezone_offsets[settings->bit.time_zone] < 0 ? -1 : 1));
+        }
+    }
+
+    // blink up the parameter we're setting
+    if ( (event.subsecond / 8) % 2) {
+        switch (current_page) {
+            case 0:
+            case 3:
+                buf[4] = buf[5] = ' ';
+                break;
+            case 1:
+            case 4:
+                buf[6] = buf[7] = ' ';
+                break;
+            case 2:
+                // Only blink first number when setting seconds, to make it easier to see subsecond error
+                buf[8] = ' ';
+                break;
+            case 5:
+                buf[8] = buf[9] = ' ';
+                break;
+        }
+    }
+
+    watch_display_string(buf, 0);
+
+    return true;
+}
+
+void set_time_hackwatch_face_resign(movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+    watch_set_led_off();
+    watch_store_backup_data(settings->reg, 0);
+}
diff --git a/movement/watch_faces/settings/set_time_hackwatch_face.h b/movement/watch_faces/settings/set_time_hackwatch_face.h
new file mode 100644 (file)
index 0000000..6d82066
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Joey Castillo
+ *
+ * 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.
+ */
+
+#ifndef SET_TIME_HACKWATCH_FACE_H_
+#define SET_TIME_HACKWATCH_FACE_H_
+
+#include "movement.h"
+
+void set_time_hackwatch_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void set_time_hackwatch_face_activate(movement_settings_t *settings, void *context);
+bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void set_time_hackwatch_face_resign(movement_settings_t *settings, void *context);
+
+#define set_time_hackwatch_face ((const watch_face_t){ \
+    set_time_hackwatch_face_setup, \
+    set_time_hackwatch_face_activate, \
+    set_time_hackwatch_face_loop, \
+    set_time_hackwatch_face_resign, \
+    NULL, \
+})
+
+#endif // SET_TIME_HACKWATCH_FACE_H_
index 8382e5714b142d772e1617f03dbff85854a2ff2e..881e25753c7f05d63999fd81f8e196c9d51a639a 100644 (file)
@@ -57,6 +57,7 @@ void _watch_rtc_init(void) {
 }
 
 void watch_rtc_set_date_time(watch_date_time date_time) {
+    _sync_rtc(); // Double sync as without it at high Hz faces setting time is unrealiable (specifically, set_time_hackwatch)
     RTC->MODE2.CLOCK.reg = date_time.reg;
     _sync_rtc();
 }
@@ -137,7 +138,7 @@ void RTC_Handler(void) {
                     tick_callbacks[i]();
                 }
                 RTC->MODE2.INTFLAG.reg = 1 << i;
-                break;
+//                break; Uncertain if this fix is requried. We were discussing in discord. Might slightly increase power consumption. 
             }
         }
     } else if ((interrupt_status & interrupt_enabled) & RTC_MODE2_INTFLAG_TAMPER) {
@@ -160,3 +161,27 @@ void RTC_Handler(void) {
         RTC->MODE2.INTFLAG.reg = RTC_MODE2_INTFLAG_ALARM0;
     }
 }
+
+void watch_rtc_enable(bool en)
+{
+    // Writing it twice - as it's quite dangerous operation.
+    // If write fails - we might hang with RTC off, which means no recovery possible
+    while (RTC->MODE2.SYNCBUSY.reg);
+    RTC->MODE2.CTRLA.bit.ENABLE = en ? 1 : 0;
+    while (RTC->MODE2.SYNCBUSY.reg);
+    RTC->MODE2.CTRLA.bit.ENABLE = en ? 1 : 0;
+    while (RTC->MODE2.SYNCBUSY.reg);
+}
+
+void watch_rtc_freqcorr_write(int16_t value, int16_t sign)
+{
+    RTC_FREQCORR_Type data;
+
+    data.bit.VALUE = value;
+    data.bit.SIGN = sign;
+
+    RTC->MODE2.FREQCORR.reg = data.reg; // Setting correction in single write operation
+
+    // We do not sycnronize. We are not in a hurry
+}
+
index 474e5ffd9862819bf4b23d8ca01e46a89789ad85..245b20edd045aae03c680badd08595429a05f2c8 100644 (file)
@@ -82,8 +82,12 @@ void watch_display_character(uint8_t character, uint8_t position) {
             continue;
         }
         uint8_t seg = segmap & 0x3F;
-        watch_clear_pixel(com, seg);
-        if (segdata & 1) watch_set_pixel(com, seg);
+
+        if (segdata & 1)
+          watch_set_pixel(com, seg);
+        else
+          watch_clear_pixel(com, seg);
+
         segmap = segmap >> 8;
         segdata = segdata >> 1;
     }
@@ -93,6 +97,32 @@ void watch_display_character(uint8_t character, uint8_t position) {
     else if (position == 1 && (character == 'B' || character == 'D' || character == '@')) watch_set_pixel(0, 12); // add funky ninth segment
 }
 
+void watch_display_character_lp_seconds(uint8_t character, uint8_t position) {
+    // Will only work for digits and for positions  8 and 9 - but less code & checks to reduce power consumption
+
+    uint64_t segmap = Segment_Map[position];
+    uint64_t segdata = Character_Set[character - 0x20];
+
+    for (int i = 0; i < 8; i++) {
+        uint8_t com = (segmap & 0xFF) >> 6;
+        if (com > 2) {
+            // COM3 means no segment exists; skip it.
+            segmap = segmap >> 8;
+            segdata = segdata >> 1;
+            continue;
+        }
+        uint8_t seg = segmap & 0x3F;
+
+        if (segdata & 1)
+          watch_set_pixel(com, seg);
+        else
+          watch_clear_pixel(com, seg);
+
+        segmap = segmap >> 8;
+        segdata = segdata >> 1;
+    }
+}
+
 void watch_display_string(char *string, uint8_t position) {
     size_t i = 0;
     while(string[i] != 0) {
index 11219cf1489b45924d15eb4c8de03d59ba45fd06..606b8dc3e2dc9d1d2d80ff199c2cbc53b1b0dd50 100644 (file)
@@ -142,5 +142,7 @@ static const uint64_t Segment_Map[] = {
 static const uint8_t Num_Chars = 10;
 
 void watch_display_character(uint8_t character, uint8_t position);
+void watch_display_character_lp_seconds(uint8_t character, uint8_t position);
+
 
 #endif
index 6609e6b66731f241ea665a8d13a86a036a0d7344..3e63bb55893f856bed19751094bd88ad8d0bc9dd 100644 (file)
@@ -147,5 +147,15 @@ void watch_rtc_disable_matching_periodic_callbacks(uint8_t mask);
   */
 void watch_rtc_disable_all_periodic_callbacks(void);
 
+/** @brief Enable/disable RTC while in-flight. This is quite dangerous operation, so we repeat writing register twice.
+ * Used when temporarily pausing RTC when adjusting subsecond, which are not accessible otherwise.
+  */
+void watch_rtc_enable(bool en);
+
+/** @brief Adjusts frequency correction in single register write. Not waiting for syncronisation to save power - if you won't write new
+ * correction value in the same ~millisecond - will not cause issue.
+  */
+void watch_rtc_freqcorr_write(int16_t value, int16_t sign);
+
 /// @}
 #endif
index 107ae56ceab91a34ac3562f4b6af2e32265ff90f..fa80d6b494b51b3ac94d210acd550fd7578d6723 100644 (file)
@@ -197,3 +197,13 @@ void watch_rtc_disable_alarm_callback(void) {
         alarm_interval_id = -1;
     }
 }
+
+void watch_rtc_enable(bool en)
+{
+    //Not simulated
+}
+
+void watch_rtc_freqcorr_write(int16_t value, int16_t sign)
+{
+    //Not simulated
+}