Skip to content

Commit 95ea1d6

Browse files
committed
minor fixes:
* more consistent function names * lapCapacity as constexpr * LastLap returns std::optional * simplified handling of TickType_t values * removed unused methods * minor fix in lap rendering
1 parent 97245cc commit 95ea1d6

3 files changed

Lines changed: 31 additions & 53 deletions

File tree

src/components/stopwatch/StopWatchController.cpp

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
using namespace Pinetime::Controllers;
44

5-
namespace {
6-
TickType_t CalculateDelta(const TickType_t startTime, const TickType_t currentTime) {
7-
TickType_t delta = 0;
8-
// Take care of overflow
9-
if (startTime > currentTime) {
10-
delta = 0xffffffff - startTime;
11-
delta += (currentTime + 1);
12-
} else {
13-
delta = currentTime - startTime;
14-
}
15-
return delta;
16-
}
17-
}
18-
195
StopWatchController::StopWatchController() {
206
Clear();
217
}
@@ -29,14 +15,14 @@ void StopWatchController::Start() {
2915

3016
void StopWatchController::Pause() {
3117
currentState = StopWatchStates::Paused;
32-
timeElapsedPreviously += CalculateDelta(startTime, xTaskGetTickCount());
18+
timeElapsedPreviously += xTaskGetTickCount() - startTime;
3319
}
3420

3521
void StopWatchController::Clear() {
3622
currentState = StopWatchStates::Cleared;
3723
timeElapsedPreviously = 0;
3824

39-
for (int i = 0; i < LAP_CAPACITY; i++) {
25+
for (int i = 0; i < lapCapacity; i++) {
4026
laps[i].count = 0;
4127
laps[i].time = 0;
4228
}
@@ -51,32 +37,25 @@ void StopWatchController::PushLap() {
5137
laps[lapHead].time = lapEnd;
5238
laps[lapHead].count = lapCount + 1;
5339
lapCount += 1;
54-
lapHead = lapCount % LAP_CAPACITY;
55-
}
56-
57-
int StopWatchController::GetLapNum() {
58-
if (lapCount < LAP_CAPACITY)
59-
return lapCount;
60-
else
61-
return LAP_CAPACITY;
40+
lapHead = lapCount % lapCapacity;
6241
}
6342

6443
int StopWatchController::GetLapCount() {
6544
return lapCount;
6645
}
6746

6847
int Wrap(int index) {
69-
return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY;
48+
return ((index % lapCapacity) + lapCapacity) % lapCapacity;
7049
}
7150

72-
LapInfo* StopWatchController::LastLap(int lap) {
73-
if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) {
74-
// Return "empty" LapInfo_t
75-
return &emptyLapInfo;
51+
std::optional<LapInfo> StopWatchController::LastLap(int lap) {
52+
if (lap >= lapCapacity || lap >= lapCount) {
53+
return {};
7654
}
7755
// Index backwards
78-
int index = Wrap(lapHead - lap);
79-
return &laps[index];
56+
int mostRecentLap = lapHead - 1;
57+
int index = Wrap(mostRecentLap - lap);
58+
return laps[index];
8059
}
8160

8261
// Data / State acess
@@ -85,7 +64,7 @@ TickType_t StopWatchController::GetElapsedTime() {
8564
if (!IsRunning()) {
8665
return timeElapsedPreviously;
8766
}
88-
return timeElapsedPreviously + CalculateDelta(startTime, xTaskGetTickCount());
67+
return timeElapsedPreviously + (xTaskGetTickCount() - startTime);
8968
}
9069

9170
bool StopWatchController::IsRunning() {

src/components/stopwatch/StopWatchController.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#pragma once
22

33
#include <FreeRTOS.h>
4+
#include <optional>
45
#include <timers.h>
56

6-
#define LAP_CAPACITY 2
7-
87
namespace Pinetime {
98
namespace System {
109
class SystemTask;
@@ -18,6 +17,8 @@ namespace Pinetime {
1817
TickType_t time = 0; // Delta time from beginning of stopwatch
1918
};
2019

20+
constexpr int lapCapacity = 2;
21+
2122
class StopWatchController {
2223
public:
2324
StopWatchController();
@@ -34,32 +35,28 @@ namespace Pinetime {
3435
/// Only the latest laps are stored, the lap count is saved until reset
3536
void PushLap();
3637

37-
/// Returns actual count of stored laps
38-
int GetLapNum();
39-
4038
/// Returns lapCount
4139
int GetLapCount();
4240

4341
/// Indexes into lap history, with 0 being the latest lap.
44-
/// If the lap is unavailable, count and time will be 0. If there is a
45-
/// real value, count should be above 0
46-
LapInfo* LastLap(int lap = 0);
42+
std::optional<LapInfo> LastLap(int lap = 0);
4743

4844
bool IsRunning();
4945
bool IsCleared();
5046
bool IsPaused();
5147

52-
private:
48+
// private:
5349
// Current state of stopwatch
5450
StopWatchStates currentState = StopWatchStates::Cleared;
5551
// Start time of current duration
5652
TickType_t startTime = 0;
5753
// How much time was elapsed before current duration
5854
TickType_t timeElapsedPreviously = 0;
5955
// Stores lap times
60-
LapInfo laps[LAP_CAPACITY];
61-
LapInfo emptyLapInfo = {.count = 0, .time = 0};
56+
LapInfo laps[lapCapacity];
57+
// Total lap count; may exceed lapCapacity
6258
int lapCount = 0;
59+
// Index for next lap time; must be lower than lapCapacity
6360
int lapHead = 0;
6461
};
6562
}

src/displayapp/screens/StopWatch.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using namespace Pinetime::Applications::Screens;
77
using namespace Pinetime::Controllers;
88

99
namespace {
10-
TimeSeparated convertTicksToTimeSegments(const TickType_t timeElapsed) {
10+
TimeSeparated ConvertTicksToTimeSegments(const TickType_t timeElapsed) {
1111
// Centiseconds
1212
const int timeElapsedCentis = timeElapsed * 100 / configTICK_RATE_HZ;
1313

@@ -18,14 +18,14 @@ namespace {
1818
return TimeSeparated {hours, mins, secs, hundredths};
1919
}
2020

21-
void play_pause_event_handler(lv_obj_t* obj, lv_event_t event) {
21+
void PlayPauseEventHandler(lv_obj_t* obj, lv_event_t event) {
2222
auto* stopWatch = static_cast<StopWatch*>(obj->user_data);
2323
if (event == LV_EVENT_CLICKED) {
2424
stopWatch->PlayPauseBtnEventHandler();
2525
}
2626
}
2727

28-
void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) {
28+
void StopLapEventHandler(lv_obj_t* obj, lv_event_t event) {
2929
auto* stopWatch = static_cast<StopWatch*>(obj->user_data);
3030
if (event == LV_EVENT_CLICKED) {
3131
stopWatch->StopLapBtnEventHandler();
@@ -42,14 +42,14 @@ StopWatch::StopWatch(System::SystemTask& systemTask,
4242
static constexpr uint8_t btnHeight = 80;
4343
btnPlayPause = lv_btn_create(lv_scr_act(), nullptr);
4444
btnPlayPause->user_data = this;
45-
lv_obj_set_event_cb(btnPlayPause, play_pause_event_handler);
45+
lv_obj_set_event_cb(btnPlayPause, PlayPauseEventHandler);
4646
lv_obj_set_size(btnPlayPause, btnWidth, btnHeight);
4747
lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
4848
txtPlayPause = lv_label_create(btnPlayPause, nullptr);
4949

5050
btnStopLap = lv_btn_create(lv_scr_act(), nullptr);
5151
btnStopLap->user_data = this;
52-
lv_obj_set_event_cb(btnStopLap, stop_lap_event_handler);
52+
lv_obj_set_event_cb(btnStopLap, StopLapEventHandler);
5353
lv_obj_set_size(btnStopLap, btnWidth, btnHeight);
5454
lv_obj_align(btnStopLap, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
5555
txtStopLap = lv_label_create(btnStopLap, nullptr);
@@ -146,7 +146,7 @@ void StopWatch::DisplayCleared() {
146146
}
147147

148148
void StopWatch::RenderTime() {
149-
TimeSeparated currentTimeSeparated = convertTicksToTimeSegments(stopWatchController.GetElapsedTime());
149+
TimeSeparated currentTimeSeparated = ConvertTicksToTimeSegments(stopWatchController.GetElapsedTime());
150150
if (currentTimeSeparated.hours == 0) {
151151
lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs);
152152
} else {
@@ -176,14 +176,16 @@ void StopWatch::RenderPause() {
176176

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

182-
if (lap->count != 0) {
183-
TimeSeparated laptime = convertTicksToTimeSegments(lap->time);
182+
if (lap) {
183+
TimeSeparated laptime = ConvertTicksToTimeSegments(lap->time);
184184
char buffer[16];
185185
sprintf(buffer, "#%2d %2d:%02d.%02d\n", lap->count, laptime.mins, laptime.secs, laptime.hundredths);
186186
lv_label_ins_text(lapText, LV_LABEL_POS_LAST, buffer);
187+
} else {
188+
lv_label_ins_text(lapText, LV_LABEL_POS_LAST, "\n");
187189
}
188190
}
189191
}

0 commit comments

Comments
 (0)