-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Stopwatch: add persistence #1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
pptime02
wants to merge
10
commits into
InfiniTimeOrg:main
from
pptime02:add_stopwatch-persistence_squashed
Closed
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fd2e21b
Stopwatch: add persistence
pptime02 071f09d
StopWatch: rename some methods
pptime02 051745a
Fix build
pptime02 6eb1958
Apply code style
pptime02 4efc7c6
Apply code style
pptime02 4e7c101
Apply code style
pptime02 ac57f0f
Apply code style
pptime02 e3ebbf4
Apply code style
pptime02 0610959
Apply code style
pptime02 2999376
Rename methods for more clarity
pptime02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,101 @@ | ||
| #include "StopWatchController.h" | ||
|
|
||
| using namespace Pinetime::Controllers; | ||
|
|
||
| namespace { | ||
| TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) { | ||
|
pptime02 marked this conversation as resolved.
Outdated
|
||
| TickType_t delta = 0; | ||
| // Take care of overflow | ||
| if (startTime > currentTime) { | ||
| delta = 0xffffffff - startTime; | ||
| delta += (currentTime + 1); | ||
| } else { | ||
| delta = currentTime - startTime; | ||
| } | ||
| return delta; | ||
| } | ||
| } | ||
|
|
||
| StopWatchController::StopWatchController() { | ||
| Clear(); | ||
| } | ||
|
|
||
| // State Change | ||
|
|
||
| void StopWatchController::Start() { | ||
| currentState = StopWatchStates::Running; | ||
| startTime = xTaskGetTickCount(); | ||
| } | ||
|
|
||
| void StopWatchController::Pause() { | ||
| currentState = StopWatchStates::Paused; | ||
| timeElapsedPreviously += calculateDelta(startTime, xTaskGetTickCount()); | ||
| } | ||
|
|
||
| void StopWatchController::Clear() { | ||
| currentState = StopWatchStates::Cleared; | ||
| timeElapsedPreviously = 0; | ||
|
|
||
| for (int i = 0; i < LAP_CAPACITY; i++) { | ||
| laps[i].count = 0; | ||
| 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 % LAP_CAPACITY; | ||
| } | ||
|
|
||
| int StopWatchController::GetLapNum() { | ||
| if (lapCount < LAP_CAPACITY) | ||
| return lapCount; | ||
| else | ||
| return LAP_CAPACITY; | ||
| } | ||
|
|
||
| int StopWatchController::GetLapCount() { | ||
| return lapCount; | ||
| } | ||
|
|
||
| int wrap(int index) { | ||
|
pptime02 marked this conversation as resolved.
Outdated
|
||
| return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY; | ||
| } | ||
|
|
||
| LapInfo_t* StopWatchController::LastLap(int lap) { | ||
| if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) { | ||
| // Return "empty" LapInfo_t | ||
| return &emptyLapInfo; | ||
| } | ||
| // Index backwards | ||
| int index = wrap(lapHead - lap); | ||
| return &laps[index]; | ||
| } | ||
|
|
||
| // Data / State acess | ||
|
|
||
| TickType_t StopWatchController::GetElapsedTime() { | ||
| if (!IsRunning()) { | ||
| return timeElapsedPreviously; | ||
| } | ||
| return timeElapsedPreviously + calculateDelta(startTime, xTaskGetTickCount()); | ||
| } | ||
|
|
||
| bool StopWatchController::IsRunning() { | ||
| return currentState == StopWatchStates::Running; | ||
| } | ||
|
|
||
| bool StopWatchController::IsCleared() { | ||
| return currentState == StopWatchStates::Cleared; | ||
| } | ||
|
|
||
| bool StopWatchController::IsPaused() { | ||
| return currentState == StopWatchStates::Paused; | ||
| } | ||
This file contains hidden or 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,66 @@ | ||
| #pragma once | ||
|
|
||
| #include <FreeRTOS.h> | ||
| #include <timers.h> | ||
|
|
||
| #define LAP_CAPACITY 2 | ||
|
|
||
| namespace Pinetime { | ||
| namespace System { | ||
| class SystemTask; | ||
| } | ||
| namespace Controllers { | ||
|
|
||
| enum class StopWatchStates { Cleared, Running, Paused }; | ||
|
|
||
| struct LapInfo_t { | ||
|
pptime02 marked this conversation as resolved.
Outdated
|
||
| int count = 0; // Used to label the lap | ||
| TickType_t time = 0; // Delta time from beginning of stopwatch | ||
| }; | ||
|
|
||
| class StopWatchController { | ||
| public: | ||
| StopWatchController(); | ||
|
|
||
| // StopWatch functionality and data | ||
| void Start(); | ||
| void Pause(); | ||
| void Clear(); | ||
|
|
||
| TickType_t GetElapsedTime(); | ||
|
|
||
| // Lap functionality | ||
|
|
||
| /// Only the latest laps are stored, the lap count is saved until reset | ||
| void PushLap(); | ||
|
|
||
| /// Returns actual count of stored laps | ||
| int GetLapNum(); | ||
|
|
||
| /// Returns lapCount | ||
| int GetLapCount(); | ||
|
|
||
| /// Indexes into lap history, with 0 being the latest lap. | ||
| /// If the lap is unavailable, count and time will be 0. If there is a | ||
| /// real value, count should be above 0 | ||
| LapInfo_t* LastLap(int lap = 0); | ||
|
|
||
| bool IsRunning(); | ||
| bool IsCleared(); | ||
| bool IsPaused(); | ||
|
|
||
| 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_t laps[LAP_CAPACITY]; | ||
| LapInfo_t emptyLapInfo = {.count = 0, .time = 0}; | ||
| int lapCount = 0; | ||
| int lapHead = 0; | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.