From b34ff32f76f355866c3bd267dcc687c0d1958d29 Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 18 Jan 2020 13:56:25 +0100 Subject: DateTimeController is now updated in the system task. It runs every 1s in Running mode, and every 1h in sleep mode. This should allow to keep the watch on time for more than 4 hours. --- src/Components/DateTime/DateTimeController.cpp | 58 ++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 9 deletions(-) (limited to 'src/Components/DateTime/DateTimeController.cpp') diff --git a/src/Components/DateTime/DateTimeController.cpp b/src/Components/DateTime/DateTimeController.cpp index 1c15642e..cd512705 100644 --- a/src/Components/DateTime/DateTimeController.cpp +++ b/src/Components/DateTime/DateTimeController.cpp @@ -1,16 +1,56 @@ #include "DateTimeController.h" +#include +#include using namespace Pinetime::Controllers; -void DateTime::UpdateTime(uint16_t year, Months month, uint8_t day, Days dayOfWeek, uint8_t hour, uint8_t minute, - uint8_t second) { - this->year = year; - this->month = month; - this->dayOfWeek = dayOfWeek; - this->day = day; - this->hour = hour; - this->minute = minute; - this->second = second; +void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, + uint8_t second, uint32_t systickCounter) { + + currentDateTime = {}; + currentDateTime += date::years( year-1970); + currentDateTime += date::days( day - 1); + currentDateTime += date::months( month - 1); + + currentDateTime += std::chrono::hours(hour); + currentDateTime += std::chrono::minutes (minute); + currentDateTime += std::chrono::seconds (second); + + currentDateTime -= std::chrono::hours(3); // TODO WHYYYY? + NRF_LOG_INFO("%d %d %d ", day, month, year); + NRF_LOG_INFO("%d %d %d ", hour, minute, second); + previousSystickCounter = systickCounter; + 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); + + +} + +void DateTime::UpdateTime(uint32_t systickCounter) { + uint32_t systickDelta = 0; + if(systickCounter < previousSystickCounter) { + systickDelta = 0xffffff - previousSystickCounter; + systickDelta += systickCounter + 1; + } else { + systickDelta = systickCounter - previousSystickCounter; + } + + previousSystickCounter = systickCounter; + currentDateTime += std::chrono::milliseconds(systickDelta); + + auto dp = date::floor(currentDateTime); + auto time = date::make_time(currentDateTime-dp); + auto yearMonthDay = date::year_month_day(dp); + + year = (int)yearMonthDay.year(); + month = static_cast((unsigned)yearMonthDay.month()); + day = (unsigned)yearMonthDay.day(); + dayOfWeek = static_cast(date::weekday(yearMonthDay).iso_encoding()); + + hour = time.hours().count(); + minute = time.minutes().count(); + second = time.seconds().count(); } -- cgit v1.2.3-70-g09d2