Improve date/time accuracy and handle counter overflow.

This commit is contained in:
JF 2020-01-12 16:00:45 +01:00
parent 9baf00bbfe
commit f23ef842ca
3 changed files with 24 additions and 14 deletions

View File

@ -146,6 +146,16 @@ void DisplayApp::Refresh() {
state = States::Running; state = States::Running;
break; break;
case Messages::UpdateDateTime: case Messages::UpdateDateTime:
currentDateTime = {};
currentDateTime += date::years( dateTimeController.Year()-1970);
currentDateTime += date::days( dateTimeController.Day() - 1);
currentDateTime += date::months( (int)dateTimeController.Month() - 1);
currentDateTime += std::chrono::hours(dateTimeController.Hours());
currentDateTime += std::chrono::minutes (dateTimeController.Minutes());
currentDateTime += std::chrono::seconds (dateTimeController.Seconds());
currentDateTime -= std::chrono::hours(3); // TODO WHYYYY?
break; break;
case Messages::UpdateBleConnection: case Messages::UpdateBleConnection:
bleConnectionUpdated = true; bleConnectionUpdated = true;
@ -163,6 +173,15 @@ void DisplayApp::Refresh() {
void DisplayApp::RunningState() { void DisplayApp::RunningState() {
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
uint32_t systickDelta = 0;
if(systick_counter < previousSystickCounter) {
systickDelta = 0xffffff - previousSystickCounter;
systickDelta += systick_counter + 1;
} else {
systickDelta = systick_counter - previousSystickCounter;
}
previousSystickCounter = systick_counter;
if (batteryLevelUpdated) { if (batteryLevelUpdated) {
char batteryChar[11]; char batteryChar[11];
@ -181,19 +200,8 @@ void DisplayApp::RunningState() {
gfx->DrawString(10, 0, color, "BLE", &smallFont, false); gfx->DrawString(10, 0, color, "BLE", &smallFont, false);
} }
auto raw = systick_counter / 1000; // TODO date/time management should be done in module DateTimeController
auto currentDeltaSeconds = raw - deltaSeconds; currentDateTime += std::chrono::milliseconds(systickDelta);
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> currentDateTime;
currentDateTime += date::years( dateTimeController.Year()-1970);
currentDateTime += date::days( dateTimeController.Day() - 1);
currentDateTime += date::months( (int)dateTimeController.Month() - 1);
currentDateTime += std::chrono::hours(dateTimeController.Hours());
currentDateTime += std::chrono::minutes (dateTimeController.Minutes());
currentDateTime += std::chrono::seconds(dateTimeController.Seconds() + currentDeltaSeconds);
currentDateTime -= std::chrono::hours(3); // TODO WHYYYY?
auto dp = date::floor<date::days>(currentDateTime); auto dp = date::floor<date::days>(currentDateTime);
auto time = date::make_time(currentDateTime-dp); auto time = date::make_time(currentDateTime-dp);

View File

@ -65,6 +65,8 @@ namespace Pinetime {
Pinetime::Drivers::Cst816S touchPanel; Pinetime::Drivers::Cst816S touchPanel;
void OnTouchEvent(); void OnTouchEvent();
uint32_t previousSystickCounter = 0;
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> currentDateTime;
}; };
} }
} }

View File

@ -58,7 +58,7 @@
#define configUSE_PREEMPTION 1 #define configUSE_PREEMPTION 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
#define configUSE_TICKLESS_IDLE 1 #define configUSE_TICKLESS_IDLE 1
#define configUSE_TICKLESS_IDLE_SIMPLE_DEBUG 1 /* See into vPortSuppressTicksAndSleep source code for explanation */ #define configUSE_TICKLESS_IDLE_SIMPLE_DEBUG 0 /* See into vPortSuppressTicksAndSleep source code for explanation */
#define configCPU_CLOCK_HZ ( SystemCoreClock ) #define configCPU_CLOCK_HZ ( SystemCoreClock )
#define configTICK_RATE_HZ 1024 #define configTICK_RATE_HZ 1024
#define configMAX_PRIORITIES ( 3 ) #define configMAX_PRIORITIES ( 3 )