summaryrefslogtreecommitdiff
path: root/src/components/datetime/DateTimeController.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/datetime/DateTimeController.cpp')
-rw-r--r--src/components/datetime/DateTimeController.cpp43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp
index 3bfbdc7..01e2859 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);
@@ -38,34 +38,27 @@ void DateTime::SetTime(
UpdateTime(systickCounter);
NRF_LOG_INFO("* %d %d %d ", this->hour, this->minute, this->second);
- NRF_LOG_INFO("* %d %d %d ", this->day, this->month, this->year);
+ NRF_LOG_INFO("* %d %d %d ", this->day, ((int)this->month), this->year);
systemTask->PushMessage(System::Messages::OnNewTime);
}
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);
- }
+ // 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(correctedDelta);
- uptime += std::chrono::seconds(correctedDelta);
+ currentDateTime += std::chrono::seconds(systickDelta);
+ uptime += std::chrono::seconds(systickDelta);
auto dp = date::floor<date::days>(currentDateTime);
auto time = date::make_time(currentDateTime - dp);
@@ -89,15 +82,15 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
isHourAlreadyNotified = false;
}
- if ((minute == 0 || minute == 30) && !isHalfHourAlreadyNotified) {
- isHalfHourAlreadyNotified = true;
+ if (minute%chimePeriod) {
+ hasChimed = false;
+ } else if (!hasChimed) {
if (systemTask != nullptr) {
- systemTask->PushMessage(System::Messages::OnNewHalfHour);
+ systemTask->PushMessage(System::Messages::OnChime);
+ hasChimed = true;
}
- } else if (minute != 0 && minute != 30) {
- isHalfHourAlreadyNotified = false;
}
-
+
// Notify new day to SystemTask
if (hour == 0 and not isMidnightAlreadyNotified) {
isMidnightAlreadyNotified = true;