diff options
| author | Michele Bini <michele.bini@gmail.com> | 2022-04-17 11:06:18 (GMT) |
|---|---|---|
| committer | Michele Bini <michele.bini@gmail.com> | 2022-04-17 13:11:09 (GMT) |
| commit | 94db51bba103c6b70475209c05a10911f4a005c7 (patch) | |
| tree | 5a12dbe8cd6347ffe45072bcb2588805d9094dff | |
| parent | 2607c3d79947e900ce4c5ded296f649677511a34 (diff) | |
Use FreeRTOS timer for AlarmController
| -rw-r--r-- | src/components/alarm/AlarmController.cpp | 27 | ||||
| -rw-r--r-- | src/components/alarm/AlarmController.h | 9 | ||||
| -rw-r--r-- | src/systemtask/SystemTask.cpp | 9 |
3 files changed, 29 insertions, 16 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp index 28b328d..c86ff73 100644 --- a/src/components/alarm/AlarmController.cpp +++ b/src/components/alarm/AlarmController.cpp @@ -27,20 +27,16 @@ using namespace std::chrono_literals; AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} { } -APP_TIMER_DEF(alarmAppTimer); - namespace { - void SetOffAlarm(void* p_context) { - auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context); - if (controller != nullptr) { - controller->SetOffAlarmNow(); - } + void SetOffAlarm(TimerHandle_t xTimer) { + auto controller = static_cast<Pinetime::Controllers::AlarmController*>(pvTimerGetTimerID(xTimer)); + controller->SetOffAlarmNow(); } } void AlarmController::Init(System::SystemTask* systemTask) { - app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm); this->systemTask = systemTask; + alarmAppTimer = xTimerCreate("alarmAppTm", 1, pdFALSE, this, SetOffAlarm); } void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { @@ -49,8 +45,8 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { } void AlarmController::ScheduleAlarm() { - // Determine the next time the alarm needs to go off and set the app_timer - app_timer_stop(alarmAppTimer); + // Determine the next time the alarm needs to go off and set the timer + xTimerStop(alarmAppTimer, 0); auto now = dateTimeController.CurrentDateTime(); alarmTime = now; @@ -81,7 +77,8 @@ 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(); - app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this); + xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0); + xTimerStart(alarmAppTimer, 0); state = AlarmState::Set; } @@ -91,7 +88,7 @@ uint32_t AlarmController::SecondsToAlarm() { } void AlarmController::DisableAlarm() { - app_timer_stop(alarmAppTimer); + xTimerStop(alarmAppTimer, 0); state = AlarmState::Not_Set; } @@ -102,6 +99,12 @@ void AlarmController::SetOffAlarmNow() { void AlarmController::StopAlerting() { systemTask->PushMessage(System::Messages::StopRinging); +} + +void AlarmController::OnStopRinging() { + if (state != AlarmState::Alerting) { + return; + } // Alarm state is off unless this is a recurring alarm if (recurrence == RecurType::None) { diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h index f39fbde..099de8b 100644 --- a/src/components/alarm/AlarmController.h +++ b/src/components/alarm/AlarmController.h @@ -17,6 +17,8 @@ */ #pragma once +#include <FreeRTOS.h> +#include <timers.h> #include <cstdint> #include "components/datetime/DateTimeController.h" @@ -29,7 +31,6 @@ namespace Pinetime { public: AlarmController(Controllers::DateTime& dateTimeController); - void Init(System::SystemTask* systemTask); void SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin); void ScheduleAlarm(); void DisableAlarm(); @@ -54,9 +55,15 @@ namespace Pinetime { recurrence = recurType; } + protected: + friend class Pinetime::System::SystemTask; + void Init(System::SystemTask* systemTask); + void OnStopRinging(); + private: Controllers::DateTime& dateTimeController; System::SystemTask* systemTask = nullptr; + TimerHandle_t alarmAppTimer; uint8_t hours = 7; uint8_t minutes = 0; std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 38c728f..9105d89 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -319,6 +319,7 @@ void SystemTask::Work() { break; case Messages::StopRinging: motorController.StopRinging(); + alarmController.OnStopRinging(); break; case Messages::BleConnected: ReloadIdleTimer(); @@ -402,7 +403,8 @@ void SystemTask::Work() { break; case Messages::OnNewHour: using Pinetime::Controllers::AlarmController; - if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::Hours && alarmController.State() != AlarmController::AlarmState::Alerting) { + if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::Hours && + alarmController.State() != AlarmController::AlarmState::Alerting) { if (isSleeping && !isWakingUp) { GoToRunning(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::Clock); @@ -412,7 +414,8 @@ void SystemTask::Work() { break; case Messages::OnNewHalfHour: using Pinetime::Controllers::AlarmController; - if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::HalfHours && alarmController.State() != AlarmController::AlarmState::Alerting) { + if (settingsController.GetChimeOption() == Controllers::Settings::ChimesOption::HalfHours && + alarmController.State() != AlarmController::AlarmState::Alerting) { if (isSleeping && !isWakingUp) { GoToRunning(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::Clock); @@ -442,7 +445,7 @@ void SystemTask::Work() { displayApp.PushMessage(Pinetime::Applications::Display::Messages::ShowPairingKey); break; case Messages::BleRadioEnableToggle: - if(settingsController.GetBleRadioEnabled()) { + if (settingsController.GetBleRadioEnabled()) { nimbleController.EnableRadio(); } else { nimbleController.DisableRadio(); |
