summaryrefslogtreecommitdiff
path: root/src/components/alarm
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/alarm')
-rw-r--r--src/components/alarm/AlarmController.cpp28
-rw-r--r--src/components/alarm/AlarmController.h6
2 files changed, 31 insertions, 3 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp
index 9f4e910..94914bd 100644
--- a/src/components/alarm/AlarmController.cpp
+++ b/src/components/alarm/AlarmController.cpp
@@ -19,6 +19,12 @@
#include "systemtask/SystemTask.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;
@@ -43,6 +49,24 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
minutes = alarmMin;
}
+void AlarmController::OnAdjustTime() {
+ if (state != AlarmState::Set) {
+ return;
+ }
+
+ xTimerStop(alarmTimer, 0);
+ auto now = dateTimeController.CurrentDateTime();
+
+ if (now >= alarmTime) {
+ xTimerChangePeriod(alarmTimer, 1, 0);
+ } else {
+ auto ticksToAlarm = std::chrono::duration_cast<ticks_t>(alarmTime - now).count();
+ xTimerChangePeriod(alarmTimer, ++ticksToAlarm, 0);
+ }
+
+ xTimerStart(alarmTimer, 0);
+}
+
void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the timer
xTimerStop(alarmTimer, 0);
@@ -75,8 +99,8 @@ void AlarmController::ScheduleAlarm() {
// 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<std::chrono::seconds>(alarmTime - now).count();
- xTimerChangePeriod(alarmTimer, secondsToAlarm * configTICK_RATE_HZ, 0);
+ auto ticksToAlarm = std::chrono::duration_cast<ticks_t>(alarmTime - now).count();
+ xTimerChangePeriod(alarmTimer, ticksToAlarm, 0);
xTimerStart(alarmTimer, 0);
state = AlarmState::Set;
diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h
index d630a12..c3a9fec 100644
--- a/src/components/alarm/AlarmController.h
+++ b/src/components/alarm/AlarmController.h
@@ -31,9 +31,9 @@ namespace Pinetime {
public:
AlarmController(Controllers::DateTime& dateTimeController);
- void Init(System::SystemTask* systemTask);
void SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin);
void ScheduleAlarm();
+ void OnAdjustTime();
void DisableAlarm();
void SetOffAlarmNow();
uint32_t SecondsToAlarm();
@@ -56,6 +56,10 @@ namespace Pinetime {
recurrence = recurType;
}
+ protected:
+ friend class Pinetime::System::SystemTask;
+ void Init(System::SystemTask* systemTask);
+
private:
Controllers::DateTime& dateTimeController;
System::SystemTask* systemTask = nullptr;