Skip to content

Commit a3acbfa

Browse files
committed
Continuous time updates
1 parent 4ca2112 commit a3acbfa

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

src/components/ble/SimpleWeatherService.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble
8080
return static_cast<Pinetime::Controllers::SimpleWeatherService*>(arg)->OnCommand(ctxt);
8181
}
8282

83-
SimpleWeatherService::SimpleWeatherService(const DateTime& dateTimeController) : dateTimeController(dateTimeController) {
83+
SimpleWeatherService::SimpleWeatherService(DateTime& dateTimeController) : dateTimeController(dateTimeController) {
8484
}
8585

8686
void SimpleWeatherService::Init() {

src/components/ble/SimpleWeatherService.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace Pinetime {
4040

4141
class SimpleWeatherService {
4242
public:
43-
explicit SimpleWeatherService(const DateTime& dateTimeController);
43+
explicit SimpleWeatherService(DateTime& dateTimeController);
4444

4545
void Init();
4646

@@ -140,7 +140,7 @@ namespace Pinetime {
140140

141141
uint16_t eventHandle {};
142142

143-
const Pinetime::Controllers::DateTime& dateTimeController;
143+
Pinetime::Controllers::DateTime& dateTimeController;
144144

145145
std::optional<CurrentWeather> currentWeather;
146146
std::optional<Forecast> forecast;

src/components/datetime/DateTimeController.cpp

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "components/datetime/DateTimeController.h"
22
#include <libraries/log/nrf_log.h>
33
#include <systemtask/SystemTask.h>
4+
#include <hal/nrf_rtc.h>
45

56
using namespace Pinetime::Controllers;
67

@@ -12,11 +13,16 @@ namespace {
1213
}
1314

1415
DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} {
16+
mutex = xSemaphoreCreateMutex();
17+
ASSERT(mutex != nullptr);
18+
xSemaphoreGive(mutex);
1519
}
1620

1721
void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
22+
xSemaphoreTake(mutex, portMAX_DELAY);
1823
this->currentDateTime = t;
19-
UpdateTime(previousSystickCounter); // Update internal state without updating the time
24+
UpdateTime(previousSystickCounter, true); // Update internal state without updating the time
25+
xSemaphoreGive(mutex);
2026
}
2127

2228
void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
@@ -29,13 +35,15 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
2935
/* .tm_year = */ year - 1900,
3036
};
3137

32-
tm.tm_isdst = -1; // Use DST value from local time zone
33-
currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
34-
3538
NRF_LOG_INFO("%d %d %d ", day, month, year);
3639
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
3740

38-
UpdateTime(previousSystickCounter);
41+
tm.tm_isdst = -1; // Use DST value from local time zone
42+
43+
xSemaphoreTake(mutex, portMAX_DELAY);
44+
currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
45+
UpdateTime(previousSystickCounter, true);
46+
xSemaphoreGive(mutex);
3947

4048
systemTask->PushMessage(System::Messages::OnNewTime);
4149
}
@@ -45,25 +53,34 @@ void DateTime::SetTimeZone(int8_t timezone, int8_t dst) {
4553
dstOffset = dst;
4654
}
4755

48-
void DateTime::UpdateTime(uint32_t systickCounter) {
56+
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> DateTime::CurrentDateTime() {
57+
xSemaphoreTake(mutex, portMAX_DELAY);
58+
UpdateTime(nrf_rtc_counter_get(portNRF_RTC_REG), false);
59+
xSemaphoreGive(mutex);
60+
return currentDateTime;
61+
}
62+
63+
void DateTime::UpdateTime(uint32_t systickCounter, bool forceUpdate) {
4964
// Handle systick counter overflow
5065
uint32_t systickDelta = 0;
5166
if (systickCounter < previousSystickCounter) {
52-
systickDelta = 0xffffff - previousSystickCounter;
67+
systickDelta = static_cast<uint32_t>(portNRF_RTC_MAXTICKS) - previousSystickCounter;
5368
systickDelta += systickCounter + 1;
5469
} else {
5570
systickDelta = systickCounter - previousSystickCounter;
5671
}
5772

58-
/*
59-
* 1000 ms = 1024 ticks
60-
*/
61-
auto correctedDelta = systickDelta / 1024;
62-
auto rest = systickDelta % 1024;
73+
auto correctedDelta = systickDelta / configTICK_RATE_HZ;
74+
// If a second hasn't passed, there is nothing to do
75+
// If the time has been changed, set forceUpdate to trigger internal state updates
76+
if (correctedDelta == 0 && !forceUpdate) {
77+
return;
78+
}
79+
auto rest = systickDelta % configTICK_RATE_HZ;
6380
if (systickCounter >= rest) {
6481
previousSystickCounter = systickCounter - rest;
6582
} else {
66-
previousSystickCounter = 0xffffff - (rest - systickCounter);
83+
previousSystickCounter = static_cast<uint32_t>(portNRF_RTC_MAXTICKS) - (rest - systickCounter - 1);
6784
}
6885

6986
currentDateTime += std::chrono::seconds(correctedDelta);

src/components/datetime/DateTimeController.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <ctime>
66
#include <string>
77
#include "components/settings/Settings.h"
8+
#include <FreeRTOS.h>
9+
#include <semphr.h>
810

911
namespace Pinetime {
1012
namespace System {
@@ -45,8 +47,6 @@ namespace Pinetime {
4547
*/
4648
void SetTimeZone(int8_t timezone, int8_t dst);
4749

48-
void UpdateTime(uint32_t systickCounter);
49-
5050
uint16_t Year() const {
5151
return 1900 + localTime.tm_year;
5252
}
@@ -124,12 +124,10 @@ namespace Pinetime {
124124
static const char* MonthShortToStringLow(Months month);
125125
static const char* DayOfWeekShortToStringLow(Days day);
126126

127-
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> CurrentDateTime() const {
128-
return currentDateTime;
129-
}
127+
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> CurrentDateTime();
130128

131-
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> UTCDateTime() const {
132-
return currentDateTime - std::chrono::seconds((tzOffset + dstOffset) * 15 * 60);
129+
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> UTCDateTime() {
130+
return CurrentDateTime() - std::chrono::seconds((tzOffset + dstOffset) * 15 * 60);
133131
}
134132

135133
std::chrono::seconds Uptime() const {
@@ -141,10 +139,14 @@ namespace Pinetime {
141139
std::string FormattedTime();
142140

143141
private:
142+
void UpdateTime(uint32_t systickCounter, bool forceUpdate);
143+
144144
std::tm localTime;
145145
int8_t tzOffset = 0;
146146
int8_t dstOffset = 0;
147147

148+
SemaphoreHandle_t mutex = nullptr;
149+
148150
uint32_t previousSystickCounter = 0;
149151
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> currentDateTime;
150152
std::chrono::seconds uptime {0};

src/displayapp/screens/WatchFaceAnalog.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace Pinetime {
7575

7676
BatteryIcon batteryIcon;
7777

78-
const Controllers::DateTime& dateTimeController;
78+
Controllers::DateTime& dateTimeController;
7979
const Controllers::Battery& batteryController;
8080
const Controllers::Ble& bleController;
8181
Controllers::NotificationManager& notificationManager;

src/systemtask/SystemTask.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ void SystemTask::Work() {
410410
}
411411

412412
monitor.Process();
413-
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
414-
dateTimeController.UpdateTime(systick_counter);
415413
NoInit_BackUpTime = dateTimeController.CurrentDateTime();
416414
if (nrf_gpio_pin_read(PinMap::Button) == 0) {
417415
watchdog.Reload();

0 commit comments

Comments
 (0)