port nanosec and finetune to Second Movement

This commit is contained in:
Joey Castillo
2025-05-21 00:50:15 -04:00
parent 20a72b4590
commit e0010f6760
6 changed files with 37 additions and 24 deletions

View File

@ -1,242 +0,0 @@
/*
* 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.
*/
#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(uint8_t watch_face_index, void ** context_ptr) {
(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(void *context) {
(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_t 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_t 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, void *context) {
(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_t 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_sleep_animation(500);
break;
case EVENT_LIGHT_BUTTON_DOWN:
// don't light up every time light is hit
break;
default:
movement_default_loop_handler(event);
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(void *context) {
(void) context;
if (total_adjustment != 0) {
finetune_update_correction_time();
}
}

View File

@ -1,77 +0,0 @@
/*
* 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_
/*
* FINETUNE face
*
* 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.
*
* Best used in conjunction with the NANOSEC face.
*
* 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.
*
* For full usage instructions, please refer to the wiki:
* https://www.sensorwatch.net/docs/watchfaces/nanosec/
*/
#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(uint8_t watch_face_index, void ** context_ptr);
void finetune_face_activate(void *context);
bool finetune_face_loop(movement_event_t event, void *context);
void finetune_face_resign(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_

View File

@ -1,375 +0,0 @@
/*
* 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.
*/
#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_t 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(uint8_t watch_face_index, void ** context_ptr) {
(void) watch_face_index;
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(void *context) {
(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;
// if ALARM decreases profile below 0, roll back around
if (nanosec_state.correction_profile < 0) {
nanosec_state.correction_profile += 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;
}
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_t 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, void *context) {
(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:
if (nanosec_screen == 4) { // If we are in profile - still decrease by 1
value_increase(-1);
} else {
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_sleep_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;
case EVENT_LIGHT_BUTTON_DOWN:
// don't light up every time light is hit
break;
default:
movement_default_loop_handler(event);
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(void *context) {
(void) context;
nanosec_ui_save();
}
// Background freq correction
movement_watch_face_advisory_t nanosec_face_advise(void *context) {
(void) context;
movement_watch_face_advisory_t retval = { 0 };
// No need for background correction if we are on profile 0 - static hardware correction.
if (nanosec_state.correction_profile != 0) {
watch_date_time_t date_time = watch_rtc_get_date_time();
retval.wants_background_task = date_time.unit.minute % nanosec_state.correction_cadence == 0;
}
return retval;
}

View File

@ -1,108 +0,0 @@
/*
* 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_
/*
* NANOSEC face
*
* 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).
*
* Best used in conjunction with the FINETUNE face.
*
* 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.
*
* For full usage instructions, please refer to the wiki:
* https://www.sensorwatch.net/docs/watchfaces/nanosec/
*/
#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(uint8_t watch_face_index, void ** context_ptr);
void nanosec_face_activate(void *context);
bool nanosec_face_loop(movement_event_t event, void *context);
void nanosec_face_resign(void *context);
movement_watch_face_advisory_t nanosec_face_advise(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_advise, \
})
#endif // NANOSEC_FACE_H_