Skip to content

Commit

Permalink
timer: Add ringing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vkareh committed Jun 19, 2024
1 parent f8f8993 commit 77f465d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/components/motor/MotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions src/components/motor/MotorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Pinetime {
void RunForDuration(uint8_t motorDuration);
void StartRinging();
void StopRinging();
bool IsRinging();

private:
static void Ring(TimerHandle_t xTimer);
Expand Down
8 changes: 5 additions & 3 deletions src/components/timer/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
remainingTime = xTaskGetTickCount() - xTimerGetExpiryTime(timer);
}
return std::chrono::milliseconds(0);
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
}

void Timer::StopTimer() {
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void DisplayApp::Refresh() {
} else {
LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up);
}
motorController.RunForDuration(35);
motorController.StartRinging();
break;
case Messages::AlarmTriggered:
if (currentApp == Apps::Alarm) {
Expand Down
31 changes: 27 additions & 4 deletions src/displayapp/screens/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -103,7 +106,12 @@ void Timer::UpdateMask() {
}

void Timer::Refresh() {
if (timer.IsRunning()) {
if (motorController.IsRinging()) {
SetTimerRinging();
auto secondsElapsed = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
minuteCounter.SetValue(secondsElapsed.count() / 60);
secondCounter.SetValue(secondsElapsed.count() % 60);
} else if (timer.IsRunning()) {
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
minuteCounter.SetValue(secondsRemaining.count() / 60);
secondCounter.SetValue(secondsRemaining.count() % 60);
Expand All @@ -123,16 +131,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() {
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() {
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);
if (ringTime == 0) {
ringTime = xTaskGetTickCount();
}
}

void Timer::ToggleRunning() {
if (timer.IsRunning()) {
if (motorController.IsRinging()) {
motorController.StopRinging();
Reset();
} else if (timer.IsRunning()) {
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
minuteCounter.SetValue(secondsRemaining.count() / 60);
secondCounter.SetValue(secondsRemaining.count() % 60);
Expand Down
8 changes: 6 additions & 2 deletions src/displayapp/screens/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,7 +15,7 @@ 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();
Expand All @@ -25,8 +26,10 @@ namespace Pinetime::Applications {
private:
void SetTimerRunning();
void SetTimerStopped();
void SetTimerRinging();
void UpdateMask();
Pinetime::Controllers::Timer& timer;
Pinetime::Controllers::MotorController& motorController;

lv_obj_t* btnPlayPause;
lv_obj_t* txtPlayPause;
Expand All @@ -43,6 +46,7 @@ namespace Pinetime::Applications {
bool buttonPressing = false;
lv_coord_t maskPosition = 0;
TickType_t pressTime = 0;
TickType_t ringTime = 0;
};
}

Expand All @@ -52,7 +56,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);
};
};
}

0 comments on commit 77f465d

Please sign in to comment.