|
| 1 | +// |
| 2 | +// Created by mrussell on 30.08.21. |
| 3 | +// |
| 4 | +// Copied from Florian's Timer app |
| 5 | + |
| 6 | +#include "AlarmController.h" |
| 7 | +#include "systemtask/SystemTask.h" |
| 8 | +#include "app_timer.h" |
| 9 | +#include "task.h" |
| 10 | +#include <chrono> |
| 11 | + |
| 12 | +using namespace Pinetime::Controllers; |
| 13 | +using namespace std::chrono_literals; |
| 14 | + |
| 15 | +AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} { |
| 16 | +} |
| 17 | + |
| 18 | +APP_TIMER_DEF(alarmAppTimer); |
| 19 | + |
| 20 | +namespace { |
| 21 | + void SetOffAlarm(void* p_context) { |
| 22 | + auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context); |
| 23 | + if (controller != nullptr) |
| 24 | + controller->SetOffAlarmNow(); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +void AlarmController::Init() { |
| 29 | + app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm); |
| 30 | +} |
| 31 | + |
| 32 | +void AlarmController::SetAlarm(uint8_t alarmHr, uint8_t alarmMin) { |
| 33 | + hours = alarmHr; |
| 34 | + minutes = alarmMin; |
| 35 | + state = AlarmState::Set; |
| 36 | + scheduleAlarm(); |
| 37 | +} |
| 38 | + |
| 39 | +void AlarmController::scheduleAlarm() { |
| 40 | + // Determine the next time the alarm needs to go off and set the app_timer |
| 41 | + app_timer_stop(alarmAppTimer); |
| 42 | + |
| 43 | + auto now = dateTimeController.CurrentDateTime(); |
| 44 | + alarmTime = now; |
| 45 | + time_t ttAlarmTime = std::chrono::system_clock::to_time_t(alarmTime); |
| 46 | + tm* tmAlarmTime = std::localtime(&ttAlarmTime); |
| 47 | + |
| 48 | + // If the time being set has already passed today,the alarm should be set for tomorrow |
| 49 | + if (hours < dateTimeController.Hours() || (hours == dateTimeController.Hours() && minutes <= dateTimeController.Minutes())) { |
| 50 | + tmAlarmTime->tm_mday += 1; |
| 51 | + // tm_wday doesn't update automatically |
| 52 | + tmAlarmTime->tm_wday = (tmAlarmTime->tm_wday + 1) % 7; |
| 53 | + } |
| 54 | + |
| 55 | + tmAlarmTime->tm_hour = hours; |
| 56 | + tmAlarmTime->tm_min = minutes; |
| 57 | + tmAlarmTime->tm_sec = 0; |
| 58 | + |
| 59 | + // if alarm is in weekday-only mode, make sure it shifts to the next weekday |
| 60 | + if (recurrence == RecurType::Weekdays) { |
| 61 | + if (tmAlarmTime->tm_wday == 0) { // Sunday, shift 1 day |
| 62 | + tmAlarmTime->tm_mday += 1; |
| 63 | + } else if (tmAlarmTime->tm_wday == 6) { // Saturday, shift 2 days |
| 64 | + tmAlarmTime->tm_mday += 2; |
| 65 | + } |
| 66 | + } |
| 67 | + tmAlarmTime->tm_isdst = -1; // use system timezone setting to determine DST |
| 68 | + |
| 69 | + // now can convert back to a time_point |
| 70 | + alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime)); |
| 71 | + auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count(); |
| 72 | + app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this); |
| 73 | +} |
| 74 | + |
| 75 | +uint32_t AlarmController::SecondsToAlarm() { |
| 76 | + return std::chrono::duration_cast<std::chrono::seconds>(alarmTime - dateTimeController.CurrentDateTime()).count(); |
| 77 | +} |
| 78 | + |
| 79 | +void AlarmController::DisableAlarm() { |
| 80 | + app_timer_stop(alarmAppTimer); |
| 81 | + state = AlarmState::Not_Set; |
| 82 | +} |
| 83 | + |
| 84 | +void AlarmController::SetOffAlarmNow() { |
| 85 | + state = AlarmState::Alerting; |
| 86 | + if (systemTask != nullptr) { |
| 87 | + systemTask->PushMessage(System::Messages::SetOffAlarm); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void AlarmController::StopAlerting() { |
| 92 | + if (systemTask != nullptr) { |
| 93 | + systemTask->PushMessage(System::Messages::StopRinging); |
| 94 | + } |
| 95 | + |
| 96 | + // Alarm state is off unless this is a recurring alarm |
| 97 | + if (recurrence == RecurType::None) { |
| 98 | + state = AlarmState::Not_Set; |
| 99 | + } else { |
| 100 | + state = AlarmState::Set; |
| 101 | + // set next instance |
| 102 | + scheduleAlarm(); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +void AlarmController::ToggleRecurrence() { |
| 107 | + if (recurrence == AlarmController::RecurType::None) { |
| 108 | + recurrence = AlarmController::RecurType::Daily; |
| 109 | + } else if (recurrence == AlarmController::RecurType::Daily) { |
| 110 | + recurrence = AlarmController::RecurType::Weekdays; |
| 111 | + } else { |
| 112 | + recurrence = AlarmController::RecurType::None; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void AlarmController::Register(Pinetime::System::SystemTask* systemTask) { |
| 117 | + this->systemTask = systemTask; |
| 118 | +} |
0 commit comments