From 693030322b8385bc311da3ea2017786e8bbb4a7c Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Mon, 8 Jan 2024 10:02:45 -0500 Subject: [PATCH] timer: Add ringing and counter The timer app issues a short buzz once and then disappears. There is no trace left that the timer finished or how long ago. This change makes the motor start ringing and presents a timer counter. The timer stops buzzing after 10 seconds, and finally resets after 1 minute. --- src/components/motor/MotorController.cpp | 4 +++ src/components/motor/MotorController.h | 1 + src/components/timer/Timer.cpp | 16 ++++++++-- src/components/timer/Timer.h | 5 ++++ src/displayapp/DisplayApp.cpp | 11 ++++--- src/displayapp/screens/Timer.cpp | 37 +++++++++++++++++++++--- src/displayapp/screens/Timer.h | 8 +++-- 7 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp index 4e392416ae..d3bd2cf3a9 100644 --- a/src/components/motor/MotorController.cpp +++ b/src/components/motor/MotorController.cpp @@ -34,6 +34,10 @@ void MotorController::StopRinging() { nrf_gpio_pin_set(PinMap::Motor); } +bool MotorController::IsRinging() { + return (xTimerIsTimerActive(longVib) == pdTRUE); +} + void MotorController::StopMotor(TimerHandle_t /*xTimer*/) { nrf_gpio_pin_set(PinMap::Motor); } diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h index 6dea6d1f9e..ab8c956e41 100644 --- a/src/components/motor/MotorController.h +++ b/src/components/motor/MotorController.h @@ -15,6 +15,7 @@ namespace Pinetime { void RunForDuration(uint8_t motorDuration); void StartRinging(); void StopRinging(); + bool IsRinging(); private: static void Ring(TimerHandle_t xTimer); diff --git a/src/components/timer/Timer.cpp b/src/components/timer/Timer.cpp index 279178cd33..7d8335828c 100644 --- a/src/components/timer/Timer.cpp +++ b/src/components/timer/Timer.cpp @@ -12,11 +12,13 @@ void Timer::StartTimer(std::chrono::milliseconds duration) { } std::chrono::milliseconds Timer::GetTimeRemaining() { + TickType_t remainingTime = 0; if (IsRunning()) { - TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount(); - return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ); + remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount(); + } else if (expired > 0) { + remainingTime = xTaskGetTickCount() - expired; } - return std::chrono::milliseconds(0); + return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ); } void Timer::StopTimer() { @@ -26,3 +28,11 @@ void Timer::StopTimer() { bool Timer::IsRunning() { return (xTimerIsTimerActive(timer) == pdTRUE); } + +void Timer::SetExpiredTime() { + expired = xTimerGetExpiryTime(timer); +} + +void Timer::ResetExpiredTime() { + expired = 0; +} diff --git a/src/components/timer/Timer.h b/src/components/timer/Timer.h index 2469666f4f..3d87c7191d 100644 --- a/src/components/timer/Timer.h +++ b/src/components/timer/Timer.h @@ -19,8 +19,13 @@ namespace Pinetime { bool IsRunning(); + void SetExpiredTime(); + + void ResetExpiredTime(); + private: TimerHandle_t timer; + TickType_t expired = 0; }; } } diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 076b4f8a26..cfce3c9414 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -343,14 +343,17 @@ void DisplayApp::Refresh() { if (state != States::Running) { PushMessageToSystemTask(System::Messages::GoToRunning); } + // Load timer app if not loaded + if (currentApp != Apps::Timer) { + LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up); + } + // Once loaded, set the timer to ringing mode if (currentApp == Apps::Timer) { lv_disp_trig_activity(nullptr); auto* timer = static_cast(currentScreen.get()); - timer->Reset(); - } else { - LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up); + timer->SetTimerRinging(); } - motorController.RunForDuration(35); + motorController.StartRinging(); break; case Messages::AlarmTriggered: if (currentApp == Apps::Alarm) { diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index f6d5e73bdc..dd491eb765 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -17,7 +17,8 @@ static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { } } -Timer::Timer(Controllers::Timer& timerController) : timer {timerController} { +Timer::Timer(Controllers::Timer& timerController, Controllers::MotorController& motorController) + : timer {timerController}, motorController {motorController} { lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_font(colonLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); @@ -62,7 +63,9 @@ Timer::Timer(Controllers::Timer& timerController) : timer {timerController} { txtPlayPause = lv_label_create(lv_scr_act(), nullptr); lv_obj_align(txtPlayPause, btnPlayPause, LV_ALIGN_CENTER, 0, 0); - if (timer.IsRunning()) { + if (motorController.IsRinging()) { + SetTimerRinging(); + } else if (timer.IsRunning()) { SetTimerRunning(); } else { SetTimerStopped(); @@ -103,7 +106,17 @@ void Timer::UpdateMask() { } void Timer::Refresh() { - if (timer.IsRunning()) { + if (isRinging) { + DisplayTime(); + // Stop buzzing after 10 seconds, but continue the counter + if (motorController.IsRinging() && displaySeconds.Get().count() > 10) { + motorController.StopRinging(); + } + // Reset timer after 1 minute + if (displaySeconds.Get().count() > 60) { + Reset(); + } + } else if (timer.IsRunning()) { DisplayTime(); } else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) { lv_label_set_text_static(txtPlayPause, "Reset"); @@ -129,16 +142,31 @@ void Timer::SetTimerRunning() { minuteCounter.HideControls(); secondCounter.HideControls(); lv_label_set_text_static(txtPlayPause, "Pause"); + lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt); } void Timer::SetTimerStopped() { + isRinging = false; minuteCounter.ShowControls(); secondCounter.ShowControls(); lv_label_set_text_static(txtPlayPause, "Start"); + lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN); +} + +void Timer::SetTimerRinging() { + isRinging = true; + minuteCounter.HideControls(); + secondCounter.HideControls(); + lv_label_set_text_static(txtPlayPause, "Reset"); + lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); + timer.SetExpiredTime(); } void Timer::ToggleRunning() { - if (timer.IsRunning()) { + if (isRinging) { + motorController.StopRinging(); + Reset(); + } else if (timer.IsRunning()) { DisplayTime(); timer.StopTimer(); SetTimerStopped(); @@ -151,6 +179,7 @@ void Timer::ToggleRunning() { } void Timer::Reset() { + timer.ResetExpiredTime(); DisplayTime(); SetTimerStopped(); } diff --git a/src/displayapp/screens/Timer.h b/src/displayapp/screens/Timer.h index 0cb2bb0802..b623d5b573 100644 --- a/src/displayapp/screens/Timer.h +++ b/src/displayapp/screens/Timer.h @@ -2,6 +2,7 @@ #include "displayapp/screens/Screen.h" #include "components/datetime/DateTimeController.h" +#include "components/motor/MotorController.h" #include "systemtask/SystemTask.h" #include "displayapp/LittleVgl.h" #include "displayapp/widgets/Counter.h" @@ -15,13 +16,14 @@ namespace Pinetime::Applications { namespace Screens { class Timer : public Screen { public: - Timer(Controllers::Timer& timerController); + Timer(Controllers::Timer& timerController, Controllers::MotorController& motorController); ~Timer() override; void Refresh() override; void Reset(); void ToggleRunning(); void ButtonPressed(); void MaskReset(); + void SetTimerRinging(); private: void SetTimerRunning(); @@ -29,6 +31,7 @@ namespace Pinetime::Applications { void UpdateMask(); void DisplayTime(); Pinetime::Controllers::Timer& timer; + Pinetime::Controllers::MotorController& motorController; lv_obj_t* btnPlayPause; lv_obj_t* txtPlayPause; @@ -43,6 +46,7 @@ namespace Pinetime::Applications { Widgets::Counter secondCounter = Widgets::Counter(0, 59, jetbrains_mono_76); bool buttonPressing = false; + bool isRinging = false; lv_coord_t maskPosition = 0; TickType_t pressTime = 0; Utility::DirtyValue displaySeconds; @@ -55,7 +59,7 @@ namespace Pinetime::Applications { static constexpr const char* icon = Screens::Symbols::hourGlass; static Screens::Screen* Create(AppControllers& controllers) { - return new Screens::Timer(controllers.timer); + return new Screens::Timer(controllers.timer, controllers.motorController); }; }; }