Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/components/timer/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,33 @@ Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunctio
void Timer::StartTimer(std::chrono::milliseconds duration) {
xTimerChangePeriod(timer, pdMS_TO_TICKS(duration.count()), 0);
xTimerStart(timer, 0);
expiry = xTimerGetExpiryTime(timer);
triggered = true;
}

std::chrono::milliseconds Timer::GetTimeRemaining() {
// nullopt if timer stopped (StopTimer called / StartTimer not yet called)
// otherwise TimerStatus with the ticks until/since expiry (depending on state of expired flag)
std::optional<Timer::TimerStatus> Timer::GetTimerState() {
if (IsRunning()) {
TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
TickType_t remainingTime = expiry - xTaskGetTickCount();
return std::make_optional<Timer::TimerStatus>(
{.distanceToExpiry = std::chrono::seconds(remainingTime / configTICK_RATE_HZ) +
std::chrono::milliseconds((remainingTime % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ),
.expired = false});
}
return std::chrono::milliseconds(0);
if (triggered) {
TickType_t timeSinceExpiry = xTaskGetTickCount() - expiry;
return std::make_optional<Timer::TimerStatus>(
{.distanceToExpiry = std::chrono::seconds(timeSinceExpiry / configTICK_RATE_HZ) +
std::chrono::milliseconds((timeSinceExpiry % configTICK_RATE_HZ) * 1000 / configTICK_RATE_HZ),
.expired = true});
}
return std::nullopt;
}

void Timer::StopTimer() {
xTimerStop(timer, 0);
triggered = false;
}

bool Timer::IsRunning() {
Expand Down
10 changes: 9 additions & 1 deletion src/components/timer/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
#include <timers.h>

#include <chrono>
#include <optional>

namespace Pinetime {
namespace Controllers {
class Timer {
public:
struct TimerStatus {
std::chrono::milliseconds distanceToExpiry;
bool expired;
};

Timer(void* timerData, TimerCallbackFunction_t timerCallbackFunction);

void StartTimer(std::chrono::milliseconds duration);

void StopTimer();

std::chrono::milliseconds GetTimeRemaining();
std::optional<TimerStatus> GetTimerState();

bool IsRunning();

private:
TimerHandle_t timer;
TickType_t expiry;
bool triggered = false;
};
}
}
3 changes: 2 additions & 1 deletion src/displayapp/screens/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ void Timer::Refresh() {
}

void Timer::DisplayTime() {
displaySeconds = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
displaySeconds =
std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimerState().value_or(Controllers::Timer::TimerStatus {}).distanceToExpiry);
if (displaySeconds.IsUpdated()) {
minuteCounter.SetValue(displaySeconds.Get().count() / 60);
secondCounter.SetValue(displaySeconds.Get().count() % 60);
Expand Down