Skip to content

Commit

Permalink
Timer: Remember last timer setting
Browse files Browse the repository at this point in the history
Remember last duration used in Timer app. Reset to zero with long press
on start button.
  • Loading branch information
tausen committed Feb 11, 2024
1 parent c2c53bc commit 685558d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void DisplayApp::Refresh() {
if (currentApp == Apps::Timer) {
lv_disp_trig_activity(nullptr);
auto* timer = static_cast<Screens::Timer*>(currentScreen.get());
timer->Reset();
timer->Stop();
} else {
LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up);
}
Expand Down
27 changes: 21 additions & 6 deletions src/displayapp/screens/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

using namespace Pinetime::Applications::Screens;

static std::chrono::seconds lastTimer;

static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<Timer*>(obj->user_data);
if (event == LV_EVENT_PRESSED) {
Expand All @@ -27,6 +29,7 @@ Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {

minuteCounter.Create();
secondCounter.Create();
SetCounters(lastTimer);
lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);
lv_obj_align(secondCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0);

Expand Down Expand Up @@ -105,8 +108,7 @@ void Timer::UpdateMask() {
void Timer::Refresh() {
if (timer.IsRunning()) {
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
minuteCounter.SetValue(secondsRemaining.count() / 60);
secondCounter.SetValue(secondsRemaining.count() % 60);
SetCounters(secondsRemaining);
} else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) {
lv_label_set_text_static(txtPlayPause, "Reset");
maskPosition += 15;
Expand All @@ -131,23 +133,36 @@ void Timer::SetTimerStopped() {
lv_label_set_text_static(txtPlayPause, "Start");
}

void Timer::SetCounters(std::chrono::seconds& duration) {
minuteCounter.SetValue(duration.count() / 60);
secondCounter.SetValue(duration.count() % 60);
}

void Timer::ToggleRunning() {
if (timer.IsRunning()) {
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
minuteCounter.SetValue(secondsRemaining.count() / 60);
secondCounter.SetValue(secondsRemaining.count() % 60);
if (secondsRemaining == std::chrono::seconds::zero()) {
secondsRemaining = lastTimer;
}
SetCounters(secondsRemaining);
timer.StopTimer();
SetTimerStopped();
lastTimer = secondsRemaining;
} else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) {
auto timerDuration = std::chrono::minutes(minuteCounter.GetValue()) + std::chrono::seconds(secondCounter.GetValue());
timer.StartTimer(timerDuration);
Refresh();
SetTimerRunning();
lastTimer = timerDuration;
}
}

void Timer::Reset() {
minuteCounter.SetValue(0);
secondCounter.SetValue(0);
lastTimer = std::chrono::seconds::zero();
Stop();
}

void Timer::Stop() {
SetCounters(lastTimer);
SetTimerStopped();
}
2 changes: 2 additions & 0 deletions src/displayapp/screens/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Pinetime::Applications {
Timer(Controllers::Timer& timerController);
~Timer() override;
void Refresh() override;
void Stop();
void Reset();
void ToggleRunning();
void ButtonPressed();
Expand All @@ -26,6 +27,7 @@ namespace Pinetime::Applications {
void SetTimerRunning();
void SetTimerStopped();
void UpdateMask();
void SetCounters(std::chrono::seconds& duration);
Pinetime::Controllers::Timer& timer;

lv_obj_t* btnPlayPause;
Expand Down

0 comments on commit 685558d

Please sign in to comment.