summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorMichele Bini <michele.bini@gmail.com>2022-04-17 11:13:41 (GMT)
committerMichele Bini <michele.bini@gmail.com>2022-04-17 13:16:10 (GMT)
commitaca605d4fb4040cc80c1acf101023aa86e7694ba (patch)
tree5f83db7a5bfbf05d732121bb95ca383243cc8eaf /src/components
parentc372ba1908bb861d7f05d057c7fc25202c34bbe3 (diff)
Differentiate time adjustment (< 3h for example time zone change when crossing a border or because of daylight saving) and time setting (for example when the firmware is booted for the first time and the companion app sends the time)
Diffstat (limited to 'src/components')
-rw-r--r--src/components/alarm/AlarmController.cpp19
-rw-r--r--src/components/alarm/AlarmController.h1
-rw-r--r--src/components/datetime/DateTimeController.cpp33
3 files changed, 42 insertions, 11 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp
index c86ff73..5432e75 100644
--- a/src/components/alarm/AlarmController.cpp
+++ b/src/components/alarm/AlarmController.cpp
@@ -44,6 +44,25 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
minutes = alarmMin;
}
+void AlarmController::OnAdjustTime() {
+
+ if (state != AlarmState::Set) {
+ return;
+ }
+
+ xTimerStop(alarmAppTimer, 0);
+ auto now = dateTimeController.CurrentDateTime();
+
+ if (now > alarmTime) {
+ xTimerChangePeriod(alarmAppTimer, 1, 0);
+ } else {
+ auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count();
+ xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0);
+ }
+
+ xTimerStart(alarmAppTimer, 0);
+}
+
void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the timer
xTimerStop(alarmAppTimer, 0);
diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h
index 099de8b..6ad220f 100644
--- a/src/components/alarm/AlarmController.h
+++ b/src/components/alarm/AlarmController.h
@@ -33,6 +33,7 @@ namespace Pinetime {
void SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin);
void ScheduleAlarm();
+ void OnAdjustTime();
void DisableAlarm();
void SetOffAlarmNow();
uint32_t SecondsToAlarm();
diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp
index 3bfbdc7..2656764 100644
--- a/src/components/datetime/DateTimeController.cpp
+++ b/src/components/datetime/DateTimeController.cpp
@@ -30,6 +30,7 @@ void DateTime::SetTime(
/* .tm_year = */ year - 1900,
};
tm.tm_isdst = -1; // Use DST value from local time zone
+ auto previousDateTime = currentDateTime;
currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
NRF_LOG_INFO("%d %d %d ", day, month, year);
@@ -40,7 +41,17 @@ void DateTime::SetTime(
NRF_LOG_INFO("* %d %d %d ", this->hour, this->minute, this->second);
NRF_LOG_INFO("* %d %d %d ", this->day, this->month, this->year);
- systemTask->PushMessage(System::Messages::OnNewTime);
+ decltype(currentDateTime - previousDateTime) timeDifference;
+
+ if (currentDateTime > previousDateTime) {
+ timeDifference = currentDateTime - previousDateTime;
+ } else {
+ timeDifference = previousDateTime - currentDateTime;
+ }
+
+ using namespace std::chrono_literals;
+
+ systemTask->PushMessage(timeDifference < 3h ? System::Messages::OnAdjustTime : System::Messages::OnNewTime);
}
void DateTime::UpdateTime(uint32_t systickCounter) {
@@ -129,16 +140,16 @@ std::string DateTime::FormattedTime() {
// Return time as a string in 12- or 24-hour format
char buff[9];
if (settingsController.GetClockType() == ClockType::H12) {
- uint8_t hour12;
- const char* amPmStr;
- if (hour < 12) {
- hour12 = (hour == 0) ? 12 : hour;
- amPmStr = "AM";
- } else {
- hour12 = (hour == 12) ? 12 : hour - 12;
- amPmStr = "PM";
- }
- sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr);
+ uint8_t hour12;
+ const char* amPmStr;
+ if (hour < 12) {
+ hour12 = (hour == 0) ? 12 : hour;
+ amPmStr = "AM";
+ } else {
+ hour12 = (hour == 12) ? 12 : hour - 12;
+ amPmStr = "PM";
+ }
+ sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr);
} else {
sprintf(buff, "%02i:%02i", hour, minute);
}