diff options
| author | codingjourney <coding@journey.sk> | 2024-10-24 21:49:42 +0200 |
|---|---|---|
| committer | JF <JF002@users.noreply.github.com> | 2025-11-04 21:25:31 +0100 |
| commit | d927a228477519bd3784650cefdceecbc500e75d (patch) | |
| tree | 95e27234040175824286f40f20c8b8dca274c32c /src | |
| parent | f28aca75419ebaae55b34b688f4d3b9d2f913246 (diff) | |
lap storage as CircularBuffer, minor fixes
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/stopwatch/StopWatchController.cpp | 19 | ||||
| -rw-r--r-- | src/components/stopwatch/StopWatchController.h | 13 | ||||
| -rw-r--r-- | src/displayapp/screens/StopWatch.cpp | 2 |
3 files changed, 13 insertions, 21 deletions
diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp index f9709e6b..d8e48588 100644 --- a/src/components/stopwatch/StopWatchController.cpp +++ b/src/components/stopwatch/StopWatchController.cpp @@ -27,35 +27,26 @@ void StopWatchController::Clear() { laps[i].time = 0; } lapCount = 0; - lapHead = 0; } // Lap void StopWatchController::PushLap() { TickType_t lapEnd = GetElapsedTime(); - laps[lapHead].time = lapEnd; - laps[lapHead].count = lapCount + 1; - lapCount += 1; - lapHead = lapCount % lapCapacity; + laps[0].time = lapEnd; + laps[0].count = ++lapCount; + laps--; } int StopWatchController::GetLapCount() { return lapCount; } -int Wrap(int index) { - return ((index % lapCapacity) + lapCapacity) % lapCapacity; -} - std::optional<LapInfo> StopWatchController::LastLap(int lap) { - if (lap >= lapCapacity || lap >= lapCount) { + if (lap < 0 || lap >= lapCapacity || laps[lap].count == 0) { return {}; } - // Index backwards - int mostRecentLap = lapHead - 1; - int index = Wrap(mostRecentLap - lap); - return laps[index]; + return laps[lap]; } // Data / State acess diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h index c549a71e..626f214f 100644 --- a/src/components/stopwatch/StopWatchController.h +++ b/src/components/stopwatch/StopWatchController.h @@ -3,6 +3,7 @@ #include <FreeRTOS.h> #include <optional> #include <timers.h> +#include "utility/CircularBuffer.h" namespace Pinetime { namespace System { @@ -17,7 +18,6 @@ namespace Pinetime { TickType_t time = 0; // Delta time from beginning of stopwatch }; - constexpr int lapCapacity = 2; class StopWatchController { public: @@ -45,19 +45,20 @@ namespace Pinetime { bool IsCleared(); bool IsPaused(); - // private: + private: // Current state of stopwatch StopWatchStates currentState = StopWatchStates::Cleared; // Start time of current duration TickType_t startTime = 0; // How much time was elapsed before current duration TickType_t timeElapsedPreviously = 0; - // Stores lap times - LapInfo laps[lapCapacity]; + + // Number of stored laps + static constexpr int lapCapacity = 2; + // Lap storage + Utility::CircularBuffer<LapInfo, lapCapacity> laps; // Total lap count; may exceed lapCapacity int lapCount = 0; - // Index for next lap time; must be lower than lapCapacity - int lapHead = 0; }; } } diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index b17518d5..cc11f885 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -176,7 +176,7 @@ void StopWatch::RenderPause() { void StopWatch::RenderLaps() { lv_label_set_text(lapText, ""); - for (int i = displayedLaps - 1; i >= 0; i--) { + for (int i = 0; i < displayedLaps; i++) { std::optional<LapInfo> lap = stopWatchController.LastLap(i); if (lap) { |
