diff options
author | Mikhail Svarichevsky <3@14.by> | 2023-01-11 00:56:26 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-10 16:56:26 -0500 |
commit | 6b71711079bd35cab61356e7b47c27445fd5eee5 (patch) | |
tree | a090a8d78d2e08db52e2b7640babfea448665fd6 /movement/watch_faces/settings | |
parent | fee6145e4d79a0d67d7e390692152bfdf94676a3 (diff) | |
download | Sensor-Watch-6b71711079bd35cab61356e7b47c27445fd5eee5.tar.gz Sensor-Watch-6b71711079bd35cab61356e7b47c27445fd5eee5.tar.bz2 Sensor-Watch-6b71711079bd35cab61356e7b47c27445fd5eee5.zip |
Precision watch update (#152)
* 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>
Diffstat (limited to 'movement/watch_faces/settings')
-rw-r--r-- | movement/watch_faces/settings/finetune_face.c | 257 | ||||
-rw-r--r-- | movement/watch_faces/settings/finetune_face.h | 49 | ||||
-rw-r--r-- | movement/watch_faces/settings/nanosec_face.c | 392 | ||||
-rw-r--r-- | movement/watch_faces/settings/nanosec_face.h | 67 | ||||
-rw-r--r-- | movement/watch_faces/settings/set_time_hackwatch_face.c | 286 | ||||
-rw-r--r-- | movement/watch_faces/settings/set_time_hackwatch_face.h | 43 |
6 files changed, 1094 insertions, 0 deletions
diff --git a/movement/watch_faces/settings/finetune_face.c b/movement/watch_faces/settings/finetune_face.c new file mode 100644 index 00000000..f328cbcb --- /dev/null +++ b/movement/watch_faces/settings/finetune_face.c @@ -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 index 00000000..6a80bf2b --- /dev/null +++ b/movement/watch_faces/settings/finetune_face.h @@ -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 index 00000000..e28b0350 --- /dev/null +++ b/movement/watch_faces/settings/nanosec_face.c @@ -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 index 00000000..044275af --- /dev/null +++ b/movement/watch_faces/settings/nanosec_face.h @@ -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 index 00000000..79f0d356 --- /dev/null +++ b/movement/watch_faces/settings/set_time_hackwatch_face.c @@ -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 index 00000000..6d820660 --- /dev/null +++ b/movement/watch_faces/settings/set_time_hackwatch_face.h @@ -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_ |