Skip to content

Commit def5162

Browse files
Merge pull request #21 from vkareh/timer-ringing
Timer ringing
2 parents 728da0f + 684acc3 commit def5162

File tree

7 files changed

+78
-13
lines changed

7 files changed

+78
-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

+39-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, System::SystemTask& systemTask)
21+
: timer {timerController}, motorController {motorController}, wakeLock(systemTask) {
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,23 @@ void Timer::UpdateMask() {
103106
}
104107

105108
void Timer::Refresh() {
106-
if (timer.IsRunning()) {
109+
if (isRinging) {
110+
DisplayTime();
111+
if (motorController.IsRinging()) {
112+
if (displaySeconds.Get().count() > 10) {
113+
// Stop buzzing after 10 seconds, but continue the counter
114+
motorController.StopRinging();
115+
wakeLock.Release();
116+
} else {
117+
// Keep the screen awake during the first 10 seconds
118+
wakeLock.Lock();
119+
}
120+
}
121+
// Reset timer after 1 minute
122+
if (displaySeconds.Get().count() > 60) {
123+
Reset();
124+
}
125+
} else if (timer.IsRunning()) {
107126
DisplayTime();
108127
} else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) {
109128
lv_label_set_text_static(txtPlayPause, "Reset");
@@ -129,16 +148,31 @@ void Timer::SetTimerRunning() {
129148
minuteCounter.HideControls();
130149
secondCounter.HideControls();
131150
lv_label_set_text_static(txtPlayPause, "Pause");
151+
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
132152
}
133153

134154
void Timer::SetTimerStopped() {
155+
isRinging = false;
135156
minuteCounter.ShowControls();
136157
secondCounter.ShowControls();
137158
lv_label_set_text_static(txtPlayPause, "Start");
159+
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
160+
}
161+
162+
void Timer::SetTimerRinging() {
163+
isRinging = true;
164+
minuteCounter.HideControls();
165+
secondCounter.HideControls();
166+
lv_label_set_text_static(txtPlayPause, "Reset");
167+
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
168+
timer.SetExpiredTime();
138169
}
139170

140171
void Timer::ToggleRunning() {
141-
if (timer.IsRunning()) {
172+
if (isRinging) {
173+
motorController.StopRinging();
174+
Reset();
175+
} else if (timer.IsRunning()) {
142176
DisplayTime();
143177
timer.StopTimer();
144178
SetTimerStopped();
@@ -151,6 +185,7 @@ void Timer::ToggleRunning() {
151185
}
152186

153187
void Timer::Reset() {
188+
timer.ResetExpiredTime();
154189
DisplayTime();
155190
SetTimerStopped();
156191
}

src/displayapp/screens/Timer.h

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

33
#include "displayapp/screens/Screen.h"
4+
#include "components/motor/MotorController.h"
45
#include "systemtask/SystemTask.h"
6+
#include "systemtask/WakeLock.h"
57
#include "displayapp/LittleVgl.h"
68
#include "displayapp/widgets/Counter.h"
79
#include "utility/DirtyValue.h"
@@ -14,20 +16,24 @@ namespace Pinetime::Applications {
1416
namespace Screens {
1517
class Timer : public Screen {
1618
public:
17-
Timer(Controllers::Timer& timerController);
19+
Timer(Controllers::Timer& timerController, Controllers::MotorController& motorController, System::SystemTask& systemTask);
1820
~Timer() override;
1921
void Refresh() override;
2022
void Reset();
2123
void ToggleRunning();
2224
void ButtonPressed();
2325
void MaskReset();
26+
void SetTimerRinging();
2427

2528
private:
2629
void SetTimerRunning();
2730
void SetTimerStopped();
2831
void UpdateMask();
2932
void DisplayTime();
3033
Pinetime::Controllers::Timer& timer;
34+
Pinetime::Controllers::MotorController& motorController;
35+
36+
Pinetime::System::WakeLock wakeLock;
3137

3238
lv_obj_t* btnPlayPause;
3339
lv_obj_t* txtPlayPause;
@@ -42,6 +48,7 @@ namespace Pinetime::Applications {
4248
Widgets::Counter secondCounter = Widgets::Counter(0, 59, jetbrains_mono_76);
4349

4450
bool buttonPressing = false;
51+
bool isRinging = false;
4552
lv_coord_t maskPosition = 0;
4653
TickType_t pressTime = 0;
4754
Utility::DirtyValue<std::chrono::seconds> displaySeconds;
@@ -54,7 +61,7 @@ namespace Pinetime::Applications {
5461
static constexpr const char* icon = Screens::Symbols::hourGlass;
5562

5663
static Screens::Screen* Create(AppControllers& controllers) {
57-
return new Screens::Timer(controllers.timer);
64+
return new Screens::Timer(controllers.timer, controllers.motorController, *controllers.systemTask);
5865
};
5966
};
6067
}

0 commit comments

Comments
 (0)