Skip to content

Commit 3e76fbf

Browse files
committed
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.
1 parent 7b39d81 commit 3e76fbf

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

src/components/motor/MotorController.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ void MotorController::StopRinging() {
3434
nrf_gpio_pin_set(PinMap::Motor);
3535
}
3636

37+
bool MotorController::IsRinging() {
38+
return (xTimerIsTimerActive(longVib) == pdTRUE);
39+
}
40+
3741
void MotorController::StopMotor(TimerHandle_t /*xTimer*/) {
3842
nrf_gpio_pin_set(PinMap::Motor);
3943
}

src/components/motor/MotorController.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Pinetime {
1515
void RunForDuration(uint8_t motorDuration);
1616
void StartRinging();
1717
void StopRinging();
18+
bool IsRinging();
1819

1920
private:
2021
static void Ring(TimerHandle_t xTimer);

src/components/timer/Timer.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ void Timer::StartTimer(std::chrono::milliseconds duration) {
1212
}
1313

1414
std::chrono::milliseconds Timer::GetTimeRemaining() {
15+
TickType_t remainingTime = 0;
1516
if (IsRunning()) {
16-
TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
17-
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
17+
remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
18+
} else if (expired > 0) {
19+
remainingTime = xTaskGetTickCount() - expired;
1820
}
19-
return std::chrono::milliseconds(0);
21+
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
2022
}
2123

2224
void Timer::StopTimer() {
@@ -26,3 +28,11 @@ void Timer::StopTimer() {
2628
bool Timer::IsRunning() {
2729
return (xTimerIsTimerActive(timer) == pdTRUE);
2830
}
31+
32+
void Timer::SetExpiredTime() {
33+
expired = xTimerGetExpiryTime(timer);
34+
}
35+
36+
void Timer::ResetExpiredTime() {
37+
expired = 0;
38+
}

src/components/timer/Timer.h

+5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ namespace Pinetime {
1919

2020
bool IsRunning();
2121

22+
void SetExpiredTime();
23+
24+
void ResetExpiredTime();
25+
2226
private:
2327
TimerHandle_t timer;
28+
TickType_t expired = 0;
2429
};
2530
}
2631
}

src/displayapp/DisplayApp.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,17 @@ void DisplayApp::Refresh() {
365365
if (state != States::Running) {
366366
PushMessageToSystemTask(System::Messages::GoToRunning);
367367
}
368+
// Load timer app if not loaded
369+
if (currentApp != Apps::Timer) {
370+
LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up);
371+
}
372+
// Once loaded, set the timer to ringing mode
368373
if (currentApp == Apps::Timer) {
369374
lv_disp_trig_activity(nullptr);
370375
auto* timer = static_cast<Screens::Timer*>(currentScreen.get());
371-
timer->Reset();
372-
} else {
373-
LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up);
376+
timer->SetTimerRinging();
374377
}
375-
motorController.RunForDuration(35);
378+
motorController.StartRinging();
376379
break;
377380
case Messages::AlarmTriggered:
378381
if (currentApp == Apps::Alarm) {

src/displayapp/screens/Timer.cpp

+33-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
1717
}
1818
}
1919

20-
Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {
20+
Timer::Timer(Controllers::Timer& timerController, Controllers::MotorController& motorController)
21+
: timer {timerController}, motorController {motorController} {
2122

2223
lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr);
2324
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} {
6263
txtPlayPause = lv_label_create(lv_scr_act(), nullptr);
6364
lv_obj_align(txtPlayPause, btnPlayPause, LV_ALIGN_CENTER, 0, 0);
6465

65-
if (timer.IsRunning()) {
66+
if (motorController.IsRinging()) {
67+
SetTimerRinging();
68+
} else if (timer.IsRunning()) {
6669
SetTimerRunning();
6770
} else {
6871
SetTimerStopped();
@@ -103,7 +106,17 @@ void Timer::UpdateMask() {
103106
}
104107

105108
void Timer::Refresh() {
106-
if (timer.IsRunning()) {
109+
if (isRinging) {
110+
DisplayTime();
111+
// Stop buzzing after 10 seconds, but continue the counter
112+
if (motorController.IsRinging() && displaySeconds.Get().count() > 10) {
113+
motorController.StopRinging();
114+
}
115+
// Reset timer after 1 minute
116+
if (displaySeconds.Get().count() > 60) {
117+
Reset();
118+
}
119+
} else if (timer.IsRunning()) {
107120
DisplayTime();
108121
} else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) {
109122
lv_label_set_text_static(txtPlayPause, "Reset");
@@ -129,16 +142,31 @@ void Timer::SetTimerRunning() {
129142
minuteCounter.HideControls();
130143
secondCounter.HideControls();
131144
lv_label_set_text_static(txtPlayPause, "Pause");
145+
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
132146
}
133147

134148
void Timer::SetTimerStopped() {
149+
isRinging = false;
135150
minuteCounter.ShowControls();
136151
secondCounter.ShowControls();
137152
lv_label_set_text_static(txtPlayPause, "Start");
153+
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
154+
}
155+
156+
void Timer::SetTimerRinging() {
157+
isRinging = true;
158+
minuteCounter.HideControls();
159+
secondCounter.HideControls();
160+
lv_label_set_text_static(txtPlayPause, "Reset");
161+
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
162+
timer.SetExpiredTime();
138163
}
139164

140165
void Timer::ToggleRunning() {
141-
if (timer.IsRunning()) {
166+
if (isRinging) {
167+
motorController.StopRinging();
168+
Reset();
169+
} else if (timer.IsRunning()) {
142170
DisplayTime();
143171
timer.StopTimer();
144172
SetTimerStopped();
@@ -151,6 +179,7 @@ void Timer::ToggleRunning() {
151179
}
152180

153181
void Timer::Reset() {
182+
timer.ResetExpiredTime();
154183
DisplayTime();
155184
SetTimerStopped();
156185
}

src/displayapp/screens/Timer.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "displayapp/screens/Screen.h"
4+
#include "components/motor/MotorController.h"
45
#include "systemtask/SystemTask.h"
56
#include "displayapp/LittleVgl.h"
67
#include "displayapp/widgets/Counter.h"
@@ -14,20 +15,22 @@ namespace Pinetime::Applications {
1415
namespace Screens {
1516
class Timer : public Screen {
1617
public:
17-
Timer(Controllers::Timer& timerController);
18+
Timer(Controllers::Timer& timerController, Controllers::MotorController& motorController);
1819
~Timer() override;
1920
void Refresh() override;
2021
void Reset();
2122
void ToggleRunning();
2223
void ButtonPressed();
2324
void MaskReset();
25+
void SetTimerRinging();
2426

2527
private:
2628
void SetTimerRunning();
2729
void SetTimerStopped();
2830
void UpdateMask();
2931
void DisplayTime();
3032
Pinetime::Controllers::Timer& timer;
33+
Pinetime::Controllers::MotorController& motorController;
3134

3235
lv_obj_t* btnPlayPause;
3336
lv_obj_t* txtPlayPause;
@@ -42,6 +45,7 @@ namespace Pinetime::Applications {
4245
Widgets::Counter secondCounter = Widgets::Counter(0, 59, jetbrains_mono_76);
4346

4447
bool buttonPressing = false;
48+
bool isRinging = false;
4549
lv_coord_t maskPosition = 0;
4650
TickType_t pressTime = 0;
4751
Utility::DirtyValue<std::chrono::seconds> displaySeconds;
@@ -54,7 +58,7 @@ namespace Pinetime::Applications {
5458
static constexpr const char* icon = Screens::Symbols::hourGlass;
5559

5660
static Screens::Screen* Create(AppControllers& controllers) {
57-
return new Screens::Timer(controllers.timer);
61+
return new Screens::Timer(controllers.timer, controllers.motorController);
5862
};
5963
};
6064
}

0 commit comments

Comments
 (0)