forked from InfiniTimeOrg/InfiniTime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 lap storage as CircularBuffer, minor fixes improved naming of lap-related fields and methods removed superfluous default values in controller render accurate time at pause fixed issues found by the test-format CI job common method for entering the Paused state added missing newline fixed an integer overflow bug in time rendering upper bound for lap numbers upper bound for elapsed time fixed layout of lap data improved layout, improved re-alignment of time fields length of lap list adapting to available space tweaked some margins to improve aesthetics reduced heap size to fix a build error fixed issues found by the test-format CI job elapsedTimeBoundary as constexpr prevent unnecessary redrawing of the time label tightened declarations of integer fields lap times without leading zeroes fixed issues found by the test-format CI job fixed a type declaration fixed irregular pause mode blinking at clock wraparound
- Loading branch information
1 parent
b3fb896
commit ab059c4
Showing
14 changed files
with
358 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "components/stopwatch/StopWatchController.h" | ||
|
||
using namespace Pinetime::Controllers; | ||
|
||
StopWatchController::StopWatchController() { | ||
Clear(); | ||
} | ||
|
||
// State Change | ||
|
||
void StopWatchController::Start() { | ||
currentState = StopWatchStates::Running; | ||
startTime = xTaskGetTickCount(); | ||
} | ||
|
||
void StopWatchController::Pause() { | ||
timeElapsedPreviously = GetElapsedTime(); | ||
currentState = StopWatchStates::Paused; | ||
} | ||
|
||
void StopWatchController::Clear() { | ||
currentState = StopWatchStates::Cleared; | ||
timeElapsedPreviously = 0; | ||
|
||
for (uint8_t i = 0; i < histSize; i++) { | ||
history[i].number = 0; | ||
history[i].timeSinceStart = 0; | ||
} | ||
maxLapNumber = 0; | ||
} | ||
|
||
// Lap | ||
|
||
void StopWatchController::AddLapToHistory() { | ||
TickType_t lapEnd = GetElapsedTime(); | ||
history--; | ||
history[0].timeSinceStart = lapEnd; | ||
history[0].number = ++maxLapNumber % lapNumberBoundary; | ||
} | ||
|
||
uint16_t StopWatchController::GetMaxLapNumber() { | ||
return maxLapNumber; | ||
} | ||
|
||
std::optional<LapInfo> StopWatchController::GetLapFromHistory(uint8_t index) { | ||
if (index >= histSize || history[index].number == 0) { | ||
return {}; | ||
} | ||
return history[index]; | ||
} | ||
|
||
// Data / State acess | ||
|
||
TickType_t StopWatchController::GetElapsedTime() { | ||
if (!IsRunning()) { | ||
return timeElapsedPreviously; | ||
} | ||
TickType_t delta = xTaskGetTickCount() - startTime; | ||
return (timeElapsedPreviously + delta) % elapsedTimeBoundary; | ||
} | ||
|
||
bool StopWatchController::IsRunning() { | ||
return currentState == StopWatchStates::Running; | ||
} | ||
|
||
bool StopWatchController::IsCleared() { | ||
return currentState == StopWatchStates::Cleared; | ||
} | ||
|
||
bool StopWatchController::IsPaused() { | ||
return currentState == StopWatchStates::Paused; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#pragma once | ||
|
||
#include <FreeRTOS.h> | ||
#include <optional> | ||
#include <timers.h> | ||
#include "utility/CircularBuffer.h" | ||
|
||
namespace Pinetime { | ||
namespace System { | ||
class SystemTask; | ||
} | ||
|
||
namespace Controllers { | ||
|
||
enum class StopWatchStates { Cleared, Running, Paused }; | ||
|
||
struct LapInfo { | ||
uint16_t number = 0; // Used to label the lap | ||
TickType_t timeSinceStart = 0; // Excluding pauses | ||
}; | ||
|
||
class StopWatchController { | ||
public: | ||
StopWatchController(); | ||
|
||
// StopWatch functionality and data | ||
void Start(); | ||
void Pause(); | ||
void Clear(); | ||
|
||
TickType_t GetElapsedTime(); | ||
|
||
// Lap functionality | ||
|
||
/// Only the latest histSize laps are stored | ||
void AddLapToHistory(); | ||
|
||
/// Returns maxLapNumber | ||
uint16_t GetMaxLapNumber(); | ||
|
||
/// Indexes into lap history, with 0 being the latest lap. | ||
std::optional<LapInfo> GetLapFromHistory(uint8_t index); | ||
|
||
bool IsRunning(); | ||
bool IsCleared(); | ||
bool IsPaused(); | ||
|
||
private: | ||
// Time at which stopwatch wraps around to zero (1000 hours) | ||
static constexpr TickType_t elapsedTimeBoundary = static_cast<TickType_t>(configTICK_RATE_HZ) * 60 * 60 * 1000; | ||
// Current state of stopwatch | ||
StopWatchStates currentState = StopWatchStates::Cleared; | ||
// Start time of current duration | ||
TickType_t startTime; | ||
// How much time was elapsed before current duration | ||
TickType_t timeElapsedPreviously; | ||
|
||
// Maximum number of stored laps | ||
static constexpr uint8_t histSize = 4; | ||
// Value at which lap numbers wrap around to zero | ||
static constexpr uint16_t lapNumberBoundary = 1000; | ||
// Lap storage | ||
Utility::CircularBuffer<LapInfo, histSize> history; | ||
// Highest lap number; less than lapNumberBoundary, may exceed histSize | ||
uint16_t maxLapNumber; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.