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.
PR InfiniTimeOrg#783: Stopwatch Persistence
Squashed commit of the following: commit ec72f56 Author: Louis Pearson <louispearson@librem.one> Date: Tue Oct 26 19:03:46 2021 -0600 Add battery icon to stopwatch screen commit 8fbc164 Author: Louis Pearson <louispearson@librem.one> Date: Tue Oct 26 18:23:05 2021 -0600 Make `make all` complete successfully - Added StopWatchController to DisplayAppRecovery - Added StopWatchController to everywhere needed in CMakeLists - Fixed the reorder warning commit a4a0a4c Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 07:13:40 2021 -0600 Prevent lap count check when watch is stopped commit c7c6309 Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 06:53:05 2021 -0600 Run clang-format on files changed in branch commit 858edfc Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 06:43:16 2021 -0600 Make function names more descriptive Also removes OnButtonPressed. I prefer the button's function to stay the same when possible, but I'll change it if needed for the PR. commit 9ea6bc3 Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 06:11:27 2021 -0600 Removed unsigned ints These appear to have been the cause of my issues. Not sure why, I'd have to know the nrf52832 better to diagnose it. commit 2fd2cdb Author: Louis Pearson <louispearson@librem.one> Date: Sun Oct 24 20:41:02 2021 -0600 Implement lap tracking commit 04a6b70 Author: Louis Pearson <louispearson@librem.one> Date: Sun Oct 24 10:29:34 2021 -0600 Fix stopwatch display issues commit ec2b673 Author: Louis Pearson <louispearson@librem.one> Date: Sun Oct 24 08:46:38 2021 -0600 Implement stop watch controller commit c75102f Author: Louis Pearson <louispearson@librem.one> Date: Sat Oct 23 23:28:07 2021 -0600 This may have been a bad idea commit 3a51035 Author: Louis Pearson <louispearson@librem.one> Date: Sat Oct 23 21:18:45 2021 -0600 Add time of day to stopwatch app
- Loading branch information
1 parent
9777107
commit 140a157
Showing
12 changed files
with
307 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include "StopWatchController.h" | ||
#include <cstdlib> | ||
#include <cstring> | ||
|
||
using namespace Pinetime::Controllers; | ||
|
||
namespace { | ||
TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) { | ||
TickType_t delta = 0; | ||
// Take care of overflow | ||
if (startTime > currentTime) { | ||
delta = 0xffffffff - startTime; | ||
delta += (currentTime + 1); | ||
} else { | ||
delta = currentTime - startTime; | ||
} | ||
return delta; | ||
} | ||
} | ||
|
||
StopWatch::StopWatch() { | ||
clear(); | ||
} | ||
|
||
// State Change | ||
|
||
void StopWatch::start(TickType_t start) { | ||
currentState = StopWatchStates::Running; | ||
startTime = start; | ||
} | ||
|
||
void StopWatch::pause(TickType_t end) { | ||
currentState = StopWatchStates::Paused; | ||
timeElapsedPreviously += calculateDelta(startTime, end); | ||
} | ||
|
||
void StopWatch::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 StopWatch::pushLap(TickType_t lapEnd) { | ||
laps[lapHead].time = lapEnd; | ||
laps[lapHead].count = lapCount + 1; | ||
lapCount += 1; | ||
lapHead = lapCount % LAP_CAPACITY; | ||
} | ||
|
||
int StopWatch::getLapNum() { | ||
if (lapCount < LAP_CAPACITY) | ||
return lapCount; | ||
else | ||
return LAP_CAPACITY; | ||
} | ||
|
||
int StopWatch::getLapCount() { | ||
return lapCount; | ||
} | ||
|
||
int wrap(int index) { | ||
return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY; | ||
} | ||
|
||
LapInfo_t* StopWatch::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 acess | ||
|
||
TickType_t StopWatch::getStart() { | ||
return startTime; | ||
} | ||
|
||
TickType_t StopWatch::getElapsedPreviously() { | ||
return timeElapsedPreviously; | ||
} | ||
|
||
bool StopWatch::isRunning() { | ||
return currentState == StopWatchStates::Running; | ||
} | ||
|
||
bool StopWatch::isCleared() { | ||
return currentState == StopWatchStates::Cleared; | ||
} | ||
|
||
bool StopWatch::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,67 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include "FreeRTOS.h" | ||
|
||
#define LAP_CAPACITY 2 | ||
|
||
namespace Pinetime { | ||
namespace System { | ||
class SystemTask; | ||
} | ||
namespace Controllers { | ||
|
||
enum class StopWatchStates { Cleared, Running, Paused }; | ||
|
||
struct LapInfo_t { | ||
int count = 0; // Used to label the lap | ||
TickType_t time = 0; // delta time from beginning of stopwatch | ||
}; | ||
|
||
class StopWatch { | ||
public: | ||
StopWatch(); | ||
|
||
// StopWatch functionality and data | ||
void start(TickType_t start); | ||
void pause(TickType_t end); | ||
void clear(); | ||
|
||
TickType_t getStart(); | ||
TickType_t getElapsedPreviously(); | ||
|
||
// Lap functionality | ||
|
||
/// Only the latest laps are stored, the lap count is saved until reset | ||
void pushLap(TickType_t lapEnd); | ||
|
||
/// 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 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.