From c7897d09ee9ad5347fa978fefdbf078791511e81 Mon Sep 17 00:00:00 2001 From: Maxim Leshchenko Date: Fri, 30 Sep 2022 16:33:37 +0200 Subject: [PATCH] Alarm: Display the time until alarm, even if it is off Previously, if the alarm was turned off, when you pressed the information button, a message was displayed that the alarm was not set. The amount of time until the alarm is now displayed as well as if it is on. What for? Allows you to save two clicks on a button if you just need to see how much time is left before the alarm --- src/components/alarm/AlarmController.cpp | 21 ++++++++++------- src/components/alarm/AlarmController.h | 3 ++- src/displayapp/screens/Alarm.cpp | 30 ++++++++++-------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp index d97e1cffcf..62a4adbaae 100644 --- a/src/components/alarm/AlarmController.cpp +++ b/src/components/alarm/AlarmController.cpp @@ -43,13 +43,9 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { minutes = alarmMin; } -void AlarmController::ScheduleAlarm() { - // Determine the next time the alarm needs to go off and set the timer - xTimerStop(alarmTimer, 0); - +std::chrono::time_point AlarmController::CalculateAlarmTimePoint() const { auto now = dateTimeController.CurrentDateTime(); - alarmTime = now; - time_t ttAlarmTime = std::chrono::system_clock::to_time_t(std::chrono::time_point_cast(alarmTime)); + time_t ttAlarmTime = std::chrono::system_clock::to_time_t(std::chrono::time_point_cast(now)); tm* tmAlarmTime = std::localtime(&ttAlarmTime); // If the time being set has already passed today,the alarm should be set for tomorrow @@ -74,8 +70,15 @@ void AlarmController::ScheduleAlarm() { tmAlarmTime->tm_isdst = -1; // use system timezone setting to determine DST // now can convert back to a time_point - alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime)); - auto secondsToAlarm = std::chrono::duration_cast(alarmTime - now).count(); + return std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime)); +} + +void AlarmController::ScheduleAlarm() { + // Determine the next time the alarm needs to go off and set the timer + xTimerStop(alarmTimer, 0); + + auto secondsToAlarm = + std::chrono::duration_cast(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count(); xTimerChangePeriod(alarmTimer, secondsToAlarm * configTICK_RATE_HZ, 0); xTimerStart(alarmTimer, 0); @@ -83,7 +86,7 @@ void AlarmController::ScheduleAlarm() { } uint32_t AlarmController::SecondsToAlarm() const { - return std::chrono::duration_cast(alarmTime - dateTimeController.CurrentDateTime()).count(); + return std::chrono::duration_cast(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count(); } void AlarmController::DisableAlarm() { diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h index 91f60f5ab2..234597e3db 100644 --- a/src/components/alarm/AlarmController.h +++ b/src/components/alarm/AlarmController.h @@ -57,12 +57,13 @@ namespace Pinetime { } private: + std::chrono::time_point CalculateAlarmTimePoint() const; + Controllers::DateTime& dateTimeController; System::SystemTask* systemTask = nullptr; TimerHandle_t alarmTimer; uint8_t hours = 7; uint8_t minutes = 0; - std::chrono::time_point alarmTime; AlarmState state = AlarmState::Not_Set; RecurType recurrence = RecurType::None; }; diff --git a/src/displayapp/screens/Alarm.cpp b/src/displayapp/screens/Alarm.cpp index d6371ce66f..fe8f346d76 100644 --- a/src/displayapp/screens/Alarm.cpp +++ b/src/displayapp/screens/Alarm.cpp @@ -241,23 +241,19 @@ void Alarm::ShowInfo() { txtMessage = lv_label_create(btnMessage, nullptr); lv_obj_set_style_local_bg_color(btnMessage, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_NAVY); - if (alarmController.State() == AlarmController::AlarmState::Set) { - auto timeToAlarm = alarmController.SecondsToAlarm(); - - auto daysToAlarm = timeToAlarm / 86400; - auto hrsToAlarm = (timeToAlarm % 86400) / 3600; - auto minToAlarm = (timeToAlarm % 3600) / 60; - auto secToAlarm = timeToAlarm % 60; - - lv_label_set_text_fmt(txtMessage, - "Time to\nalarm:\n%2lu Days\n%2lu Hours\n%2lu Minutes\n%2lu Seconds", - daysToAlarm, - hrsToAlarm, - minToAlarm, - secToAlarm); - } else { - lv_label_set_text_static(txtMessage, "Alarm\nis not\nset."); - } + auto timeToAlarm = alarmController.SecondsToAlarm(); + + auto daysToAlarm = timeToAlarm / 86400; + auto hrsToAlarm = (timeToAlarm % 86400) / 3600; + auto minToAlarm = (timeToAlarm % 3600) / 60; + auto secToAlarm = timeToAlarm % 60; + + lv_label_set_text_fmt(txtMessage, + "Time to\nalarm:\n%2lu Days\n%2lu Hours\n%2lu Minutes\n%2lu Seconds", + daysToAlarm, + hrsToAlarm, + minToAlarm, + secToAlarm); } void Alarm::HideInfo() {