|
| 1 | +/* Copyright (C) 2021 mruss77, Florian |
| 2 | +
|
| 3 | + This file is part of InfiniTime. |
| 4 | +
|
| 5 | + InfiniTime is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published |
| 7 | + by the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + InfiniTime is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | +#include "AlarmController.h" |
| 19 | +#include "systemtask/SystemTask.h" |
| 20 | +#include "app_timer.h" |
| 21 | +#include "task.h" |
| 22 | +#include <chrono> |
| 23 | + |
| 24 | +using namespace Pinetime::Controllers; |
| 25 | +using namespace std::chrono_literals; |
| 26 | + |
| 27 | +AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} { |
| 28 | +} |
| 29 | + |
| 30 | +APP_TIMER_DEF(alarmAppTimer); |
| 31 | + |
| 32 | +namespace { |
| 33 | + void SetOffAlarm(void* p_context) { |
| 34 | + auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context); |
| 35 | + if (controller != nullptr) { |
| 36 | + controller->SetOffAlarmNow(); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void AlarmController::Init(System::SystemTask* systemTask) { |
| 42 | + app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm); |
| 43 | + this->systemTask = systemTask; |
| 44 | +} |
| 45 | + |
| 46 | +void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { |
| 47 | + hours = alarmHr; |
| 48 | + minutes = alarmMin; |
| 49 | +} |
| 50 | + |
| 51 | +void AlarmController::ScheduleAlarm() { |
| 52 | + // Determine the next time the alarm needs to go off and set the app_timer |
| 53 | + app_timer_stop(alarmAppTimer); |
| 54 | + |
| 55 | + auto now = dateTimeController.CurrentDateTime(); |
| 56 | + alarmTime = now; |
| 57 | + time_t ttAlarmTime = std::chrono::system_clock::to_time_t(alarmTime); |
| 58 | + tm* tmAlarmTime = std::localtime(&ttAlarmTime); |
| 59 | + |
| 60 | + // If the time being set has already passed today,the alarm should be set for tomorrow |
| 61 | + if (hours < dateTimeController.Hours() || (hours == dateTimeController.Hours() && minutes <= dateTimeController.Minutes())) { |
| 62 | + tmAlarmTime->tm_mday += 1; |
| 63 | + // tm_wday doesn't update automatically |
| 64 | + tmAlarmTime->tm_wday = (tmAlarmTime->tm_wday + 1) % 7; |
| 65 | + } |
| 66 | + |
| 67 | + tmAlarmTime->tm_hour = hours; |
| 68 | + tmAlarmTime->tm_min = minutes; |
| 69 | + tmAlarmTime->tm_sec = 0; |
| 70 | + |
| 71 | + // if alarm is in weekday-only mode, make sure it shifts to the next weekday |
| 72 | + if (recurrence == RecurType::Weekdays) { |
| 73 | + if (tmAlarmTime->tm_wday == 0) { // Sunday, shift 1 day |
| 74 | + tmAlarmTime->tm_mday += 1; |
| 75 | + } else if (tmAlarmTime->tm_wday == 6) { // Saturday, shift 2 days |
| 76 | + tmAlarmTime->tm_mday += 2; |
| 77 | + } |
| 78 | + } |
| 79 | + tmAlarmTime->tm_isdst = -1; // use system timezone setting to determine DST |
| 80 | + |
| 81 | + // now can convert back to a time_point |
| 82 | + alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime)); |
| 83 | + auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count(); |
| 84 | + app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this); |
| 85 | + |
| 86 | + state = AlarmState::Set; |
| 87 | +} |
| 88 | + |
| 89 | +uint32_t AlarmController::SecondsToAlarm() { |
| 90 | + return std::chrono::duration_cast<std::chrono::seconds>(alarmTime - dateTimeController.CurrentDateTime()).count(); |
| 91 | +} |
| 92 | + |
| 93 | +void AlarmController::DisableAlarm() { |
| 94 | + app_timer_stop(alarmAppTimer); |
| 95 | + state = AlarmState::Not_Set; |
| 96 | +} |
| 97 | + |
| 98 | +void AlarmController::SetOffAlarmNow() { |
| 99 | + state = AlarmState::Alerting; |
| 100 | + systemTask->PushMessage(System::Messages::SetOffAlarm); |
| 101 | +} |
| 102 | + |
| 103 | +void AlarmController::StopAlerting() { |
| 104 | + systemTask->PushMessage(System::Messages::StopRinging); |
| 105 | + |
| 106 | + // Alarm state is off unless this is a recurring alarm |
| 107 | + if (recurrence == RecurType::None) { |
| 108 | + state = AlarmState::Not_Set; |
| 109 | + } else { |
| 110 | + state = AlarmState::Set; |
| 111 | + // set next instance |
| 112 | + ScheduleAlarm(); |
| 113 | + } |
| 114 | +} |
0 commit comments