diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp index 67ca05a968..70a461a4a0 100644 --- a/src/components/alarm/AlarmController.cpp +++ b/src/components/alarm/AlarmController.cpp @@ -19,7 +19,6 @@ #include "systemtask/SystemTask.h" #include "app_timer.h" #include "task.h" -#include using namespace Pinetime::Controllers; using namespace std::chrono_literals; @@ -48,13 +47,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 app_timer - app_timer_stop(alarmAppTimer); - +std::chrono::time_point AlarmController::CalculateAlarmTimePoint() { auto now = dateTimeController.CurrentDateTime(); - alarmTime = now; - time_t ttAlarmTime = std::chrono::system_clock::to_time_t(alarmTime); + time_t ttAlarmTime = std::chrono::system_clock::to_time_t(now); tm* tmAlarmTime = std::localtime(&ttAlarmTime); // If the time being set has already passed today,the alarm should be set for tomorrow @@ -79,15 +74,21 @@ 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 mSecToAlarm = 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 app_timer + app_timer_stop(alarmAppTimer); + + auto mSecToAlarm = std::chrono::duration_cast(CalculateAlarmTimePoint() - dateTimeController.CurrentDateTime()).count(); app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this); state = AlarmState::Set; } uint32_t AlarmController::SecondsToAlarm() { - 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 bf85d43140..4676ec401c 100644 --- a/src/components/alarm/AlarmController.h +++ b/src/components/alarm/AlarmController.h @@ -20,6 +20,7 @@ #include #include "app_timer.h" #include "components/datetime/DateTimeController.h" +#include namespace Pinetime { namespace System { @@ -56,11 +57,12 @@ namespace Pinetime { } private: + std::chrono::time_point CalculateAlarmTimePoint(); + Controllers::DateTime& dateTimeController; System::SystemTask* systemTask = nullptr; 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 959cb0b26d..961ff21546 100644 --- a/src/displayapp/screens/Alarm.cpp +++ b/src/displayapp/screens/Alarm.cpp @@ -209,19 +209,15 @@ 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%2d Days\n%2d Hours\n%2d Minutes\n%2d Seconds", daysToAlarm, hrsToAlarm, minToAlarm, secToAlarm); - } else { - lv_label_set_text(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%2d Days\n%2d Hours\n%2d Minutes\n%2d Seconds", daysToAlarm, hrsToAlarm, minToAlarm, secToAlarm); } void Alarm::SetRecurButtonState() {