Skip to content

Commit 4bc164e

Browse files
committed
lap storage as CircularBuffer, minor fixes
1 parent 95ea1d6 commit 4bc164e

3 files changed

Lines changed: 13 additions & 21 deletions

File tree

src/components/stopwatch/StopWatchController.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,26 @@ void StopWatchController::Clear() {
2727
laps[i].time = 0;
2828
}
2929
lapCount = 0;
30-
lapHead = 0;
3130
}
3231

3332
// Lap
3433

3534
void StopWatchController::PushLap() {
3635
TickType_t lapEnd = GetElapsedTime();
37-
laps[lapHead].time = lapEnd;
38-
laps[lapHead].count = lapCount + 1;
39-
lapCount += 1;
40-
lapHead = lapCount % lapCapacity;
36+
laps[0].time = lapEnd;
37+
laps[0].count = ++lapCount;
38+
laps--;
4139
}
4240

4341
int StopWatchController::GetLapCount() {
4442
return lapCount;
4543
}
4644

47-
int Wrap(int index) {
48-
return ((index % lapCapacity) + lapCapacity) % lapCapacity;
49-
}
50-
5145
std::optional<LapInfo> StopWatchController::LastLap(int lap) {
52-
if (lap >= lapCapacity || lap >= lapCount) {
46+
if (lap < 0 || lap >= lapCapacity || laps[lap].count == 0) {
5347
return {};
5448
}
55-
// Index backwards
56-
int mostRecentLap = lapHead - 1;
57-
int index = Wrap(mostRecentLap - lap);
58-
return laps[index];
49+
return laps[lap];
5950
}
6051

6152
// Data / State acess

src/components/stopwatch/StopWatchController.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <FreeRTOS.h>
44
#include <optional>
55
#include <timers.h>
6+
#include "utility/CircularBuffer.h"
67

78
namespace Pinetime {
89
namespace System {
@@ -17,7 +18,6 @@ namespace Pinetime {
1718
TickType_t time = 0; // Delta time from beginning of stopwatch
1819
};
1920

20-
constexpr int lapCapacity = 2;
2121

2222
class StopWatchController {
2323
public:
@@ -45,19 +45,20 @@ namespace Pinetime {
4545
bool IsCleared();
4646
bool IsPaused();
4747

48-
// private:
48+
private:
4949
// Current state of stopwatch
5050
StopWatchStates currentState = StopWatchStates::Cleared;
5151
// Start time of current duration
5252
TickType_t startTime = 0;
5353
// How much time was elapsed before current duration
5454
TickType_t timeElapsedPreviously = 0;
55-
// Stores lap times
56-
LapInfo laps[lapCapacity];
55+
56+
// Number of stored laps
57+
static constexpr int lapCapacity = 2;
58+
// Lap storage
59+
Utility::CircularBuffer<LapInfo, lapCapacity> laps;
5760
// Total lap count; may exceed lapCapacity
5861
int lapCount = 0;
59-
// Index for next lap time; must be lower than lapCapacity
60-
int lapHead = 0;
6162
};
6263
}
6364
}

src/displayapp/screens/StopWatch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void StopWatch::RenderPause() {
176176

177177
void StopWatch::RenderLaps() {
178178
lv_label_set_text(lapText, "");
179-
for (int i = displayedLaps - 1; i >= 0; i--) {
179+
for (int i = 0; i < displayedLaps; i++) {
180180
std::optional<LapInfo> lap = stopWatchController.LastLap(i);
181181

182182
if (lap) {

0 commit comments

Comments
 (0)