From 183337b6d3a3c3eb955a07551d04fb0990966516 Mon Sep 17 00:00:00 2001 From: codingjourney Date: Thu, 28 Nov 2024 05:20:03 +0100 Subject: upper bound for elapsed time --- src/components/stopwatch/StopWatchController.cpp | 5 +++-- src/components/stopwatch/StopWatchController.h | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/components/stopwatch') diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp index 311bccc7..64261055 100644 --- a/src/components/stopwatch/StopWatchController.cpp +++ b/src/components/stopwatch/StopWatchController.cpp @@ -14,8 +14,8 @@ void StopWatchController::Start() { } void StopWatchController::Pause() { + timeElapsedPreviously = GetElapsedTime(); currentState = StopWatchStates::Paused; - timeElapsedPreviously += xTaskGetTickCount() - startTime; } void StopWatchController::Clear() { @@ -55,7 +55,8 @@ TickType_t StopWatchController::GetElapsedTime() { if (!IsRunning()) { return timeElapsedPreviously; } - return timeElapsedPreviously + (xTaskGetTickCount() - startTime); + TickType_t delta = xTaskGetTickCount() - startTime; + return (timeElapsedPreviously + delta) % elapsedTimeBoundary; } bool StopWatchController::IsRunning() { diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h index 0791701c..6aaec2b2 100644 --- a/src/components/stopwatch/StopWatchController.h +++ b/src/components/stopwatch/StopWatchController.h @@ -46,6 +46,8 @@ namespace Pinetime { bool IsPaused(); private: + // Time at which stopwatch wraps around to zero (1000 hours) + TickType_t elapsedTimeBoundary = configTICK_RATE_HZ * 60 * 60 * 1000; // Current state of stopwatch StopWatchStates currentState = StopWatchStates::Cleared; // Start time of current duration @@ -55,6 +57,7 @@ namespace Pinetime { // Maximum number of stored laps static constexpr int histSize = 2; + // Value at which lap numbers wrap around to zero static constexpr int lapNumberBoundary = 1000; // Lap storage Utility::CircularBuffer history; -- cgit v1.2.3-70-g09d2