summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichele Bini <michele.bini@gmail.com>2022-04-07 13:45:14 (GMT)
committerMichele Bini <michele.bini@gmail.com>2022-04-07 14:02:08 (GMT)
commit78df8c51d4349a0d6e48aabd5bf77f7da611b11c (patch)
treea28cab32011532ee1574fa228194e6f96b2b95eb
parent043517f85124810310e5a854fb02b52fb9feccb3 (diff)
Streamline datetime code, adjust precision to the one actually used
-rw-r--r--src/components/datetime/DateTimeController.cpp31
-rw-r--r--src/components/datetime/DateTimeController.h9
2 files changed, 17 insertions, 23 deletions
diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp
index 6022474..90ce3fe 100644
--- a/src/components/datetime/DateTimeController.cpp
+++ b/src/components/datetime/DateTimeController.cpp
@@ -14,7 +14,7 @@ namespace {
DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} {
}
-void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
+void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> t) {
this->currentDateTime = t;
UpdateTime(previousSystickCounter); // Update internal state without updating the time
}
@@ -30,7 +30,7 @@ void DateTime::SetTime(
/* .tm_year = */ year - 1900,
};
tm.tm_isdst = -1; // Use DST value from local time zone
- currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
+ currentDateTime = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::from_time_t(std::mktime(&tm)));
NRF_LOG_INFO("%d %d %d ", day, month, year);
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
@@ -45,27 +45,20 @@ void DateTime::SetTime(
void DateTime::UpdateTime(uint32_t systickCounter) {
// Handle systick counter overflow
- uint32_t systickDelta = 0;
- if (systickCounter < previousSystickCounter) {
- systickDelta = 0xffffff - previousSystickCounter;
- systickDelta += systickCounter + 1;
- } else {
- systickDelta = systickCounter - previousSystickCounter;
- }
+ uint32_t systickDelta = (systickCounter - previousSystickCounter) & 0xffffff;
+ previousSystickCounter = systickCounter;
/*
* 1000 ms = 1024 ticks
*/
- auto correctedDelta = systickDelta / 1024;
- auto rest = (systickDelta - (correctedDelta * 1024));
- if (systickCounter >= rest) {
- previousSystickCounter = systickCounter - rest;
- } else {
- previousSystickCounter = 0xffffff - (rest - systickCounter);
- }
-
- currentDateTime += std::chrono::seconds(correctedDelta);
- uptime += std::chrono::seconds(correctedDelta);
+ // auto newSeconds = systickDelta >> 10;
+ // auto rest = systickDelta & ((1 << 10)-1);
+ subsecondTicks += systickDelta & ((1 << 10)-1);
+ systickDelta = (systickDelta >> 10) + (subsecondTicks >> 10);
+ subsecondTicks &= ((1 << 10)-1);
+
+ currentDateTime += std::chrono::seconds(systickDelta);
+ uptime += std::chrono::seconds(systickDelta);
auto dp = date::floor<date::days>(currentDateTime);
auto time = date::make_time(currentDateTime - dp);
diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h
index e964b4c..ca2a0fe 100644
--- a/src/components/datetime/DateTimeController.h
+++ b/src/components/datetime/DateTimeController.h
@@ -65,7 +65,7 @@ namespace Pinetime {
const char* DayOfWeekShortToString() const;
static const char* MonthShortToStringLow(Months month);
- std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> CurrentDateTime() const {
+ std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> CurrentDateTime() const {
return currentDateTime;
}
std::chrono::seconds Uptime() const {
@@ -73,7 +73,7 @@ namespace Pinetime {
}
void Register(System::SystemTask* systemTask);
- void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t);
+ void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> t);
std::string FormattedTime();
private:
@@ -84,9 +84,10 @@ namespace Pinetime {
uint8_t hour = 0;
uint8_t minute = 0;
uint8_t second = 0;
+ uint16_t subsecondTicks = 0;
- uint32_t previousSystickCounter = 0;
- std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> currentDateTime;
+ uint32_t previousSystickCounter = 0; // FIXME: This could probably be 16 bits?
+ std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> currentDateTime;
std::chrono::seconds uptime {0};
bool isMidnightAlreadyNotified = false;