diff options
Diffstat (limited to 'src/components/alarm/AlarmController.cpp')
| -rw-r--r-- | src/components/alarm/AlarmController.cpp | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp index cd011f6..4131f3f 100644 --- a/src/components/alarm/AlarmController.cpp +++ b/src/components/alarm/AlarmController.cpp @@ -20,6 +20,12 @@ #include "app_timer.h" #include "task.h" #include <chrono> +#include <ratio> +#include <cstdint> + +namespace { + typedef std::chrono::duration< int64_t, std::ratio<1, configTICK_RATE_HZ> > ticks_t; +} using namespace Pinetime::Controllers; using namespace std::chrono_literals; @@ -36,7 +42,7 @@ namespace { void AlarmController::Init(System::SystemTask* systemTask) { this->systemTask = systemTask; - alarmAppTimer = xTimerCreate("alarmAppTm", 1, pdFALSE, this, SetOffAlarm); + alarmTimer = xTimerCreate("alarmAppTm", 1, pdFALSE, this, SetOffAlarm); } void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { @@ -45,25 +51,26 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { } void AlarmController::OnAdjustTime() { + if (state != AlarmState::Set) { + return; + } - if (state != AlarmState::Set) { return; } - - xTimerStop(alarmAppTimer, 0); + xTimerStop(alarmTimer, 0); auto now = dateTimeController.CurrentDateTime(); - if (now > alarmTime) { - xTimerChangePeriod(alarmAppTimer, 1, 0); + if (now >= alarmTime) { + xTimerChangePeriod(alarmTimer, 1, 0); } else { - auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count(); - xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0); + auto ticksToAlarm = std::chrono::duration_cast<ticks_t>(alarmTime - now).count(); + xTimerChangePeriod(alarmTimer, ++ticksToAlarm, 0); } - xTimerStart(alarmAppTimer, 0); + xTimerStart(alarmTimer, 0); } void AlarmController::ScheduleAlarm() { // Determine the next time the alarm needs to go off and set the timer - xTimerStop(alarmAppTimer, 0); + xTimerStop(alarmTimer, 0); auto now = dateTimeController.CurrentDateTime(); alarmTime = now; @@ -94,9 +101,9 @@ void AlarmController::ScheduleAlarm() { // 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<std::chrono::milliseconds>(alarmTime - now).count(); - xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0); - xTimerStart(alarmAppTimer, 0); + auto ticksToAlarm = std::chrono::duration_cast<ticks_t>(alarmTime - now).count(); + xTimerChangePeriod(alarmTimer, ticksToAlarm, 0); + xTimerStart(alarmTimer, 0); state = AlarmState::Set; } @@ -106,7 +113,7 @@ uint32_t AlarmController::SecondsToAlarm() { } void AlarmController::DisableAlarm() { - xTimerStop(alarmAppTimer, 0); + xTimerStop(alarmTimer, 0); state = AlarmState::Not_Set; } |
