diff options
| author | Michele Bini <michele.bini@gmail.com> | 2022-04-10 02:40:16 (GMT) |
|---|---|---|
| committer | Michele Bini <michele.bini@gmail.com> | 2022-06-10 10:45:54 (GMT) |
| commit | b72a3ed87d9797f7746d6fbdb348972401e4414b (patch) | |
| tree | 20833456e4f57f8c8317cb104174da7c08238583 /src/components | |
| parent | ede8cb64d51025aad616622aebeb4144bbcdc786 (diff) | |
Integrate hatmajster's timer code
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/timer/TimerController.cpp | 32 | ||||
| -rw-r--r-- | src/components/timer/TimerController.h | 24 |
2 files changed, 39 insertions, 17 deletions
diff --git a/src/components/timer/TimerController.cpp b/src/components/timer/TimerController.cpp index eeee61f..55b7d56 100644 --- a/src/components/timer/TimerController.cpp +++ b/src/components/timer/TimerController.cpp @@ -3,9 +3,11 @@ using namespace Pinetime::Controllers; -void TimerCallback(TimerHandle_t xTimer) { - auto* controller = static_cast<TimerController*>(pvTimerGetTimerID(xTimer)); - controller->OnTimerEnd(); +namespace { + void TimerCallback(TimerHandle_t xTimer) { + auto* controller = static_cast<TimerController*>(pvTimerGetTimerID(xTimer)); + controller->OnTimerEnd(); + } } void TimerController::Init(Pinetime::System::SystemTask* systemTask) { @@ -16,25 +18,37 @@ void TimerController::Init(Pinetime::System::SystemTask* systemTask) { void TimerController::StartTimer(uint32_t duration) { xTimerChangePeriod(timer, pdMS_TO_TICKS(duration), 0); xTimerStart(timer, 0); + timerRunning = true; + overtime = false; } -uint32_t TimerController::GetTimeRemaining() { +int32_t TimerController::GetSecondsRemaining() { if (IsRunning()) { - TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount(); - return (remainingTime * 1000 / configTICK_RATE_HZ); + int32_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount(); + return remainingTime / configTICK_RATE_HZ; } return 0; } void TimerController::StopTimer() { xTimerStop(timer, 0); + timerRunning = false; + if (overtime) { + StopAlerting(); + overtime = false; + } } -bool TimerController::IsRunning() { - return (xTimerIsTimerActive(timer) == pdTRUE); +void TimerController::StopAlerting() { + if (systemTask != nullptr) { + systemTask->PushMessage(System::Messages::StopRinging); + } } void TimerController::OnTimerEnd() { - systemTask->PushMessage(System::Messages::OnTimerDone); + overtime = true; + if (systemTask != nullptr) { + systemTask->PushMessage(System::Messages::OnTimerDone); + } } diff --git a/src/components/timer/TimerController.h b/src/components/timer/TimerController.h index 93d8afc..c3bdc2a 100644 --- a/src/components/timer/TimerController.h +++ b/src/components/timer/TimerController.h @@ -2,6 +2,7 @@ #include <FreeRTOS.h> #include <timers.h> +#include <cstdint> namespace Pinetime { namespace System { @@ -13,21 +14,28 @@ namespace Pinetime { public: TimerController() = default; - void Init(System::SystemTask* systemTask); - void StartTimer(uint32_t duration); - void StopTimer(); - - uint32_t GetTimeRemaining(); - - bool IsRunning(); + void StopAlerting(); + int32_t GetSecondsRemaining(); + inline bool IsOvertime() { + return overtime; + } + inline bool IsRunning() { + return timerRunning; + } void OnTimerEnd(); + protected: + friend class Pinetime::System::SystemTask; + void Init(System::SystemTask* systemTask); + private: System::SystemTask* systemTask = nullptr; TimerHandle_t timer; + bool timerRunning = false; + bool overtime = false; }; } -} +}
\ No newline at end of file |
