|
| 1 | +#include "components/stopwatch/StopWatchController.h" |
| 2 | + |
| 3 | +using namespace Pinetime::Controllers; |
| 4 | + |
| 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 | + |
| 19 | +StopWatchController::StopWatchController() { |
| 20 | + Clear(); |
| 21 | +} |
| 22 | + |
| 23 | +// State Change |
| 24 | + |
| 25 | +void StopWatchController::Start() { |
| 26 | + currentState = StopWatchStates::Running; |
| 27 | + startTime = xTaskGetTickCount(); |
| 28 | +} |
| 29 | + |
| 30 | +void StopWatchController::Pause() { |
| 31 | + currentState = StopWatchStates::Paused; |
| 32 | + timeElapsedPreviously += CalculateDelta(startTime, xTaskGetTickCount()); |
| 33 | +} |
| 34 | + |
| 35 | +void StopWatchController::Clear() { |
| 36 | + currentState = StopWatchStates::Cleared; |
| 37 | + timeElapsedPreviously = 0; |
| 38 | + |
| 39 | + for (int i = 0; i < LAP_CAPACITY; i++) { |
| 40 | + laps[i].count = 0; |
| 41 | + laps[i].time = 0; |
| 42 | + } |
| 43 | + lapCount = 0; |
| 44 | + lapHead = 0; |
| 45 | +} |
| 46 | + |
| 47 | +// Lap |
| 48 | + |
| 49 | +void StopWatchController::PushLap() { |
| 50 | + TickType_t lapEnd = GetElapsedTime(); |
| 51 | + laps[lapHead].time = lapEnd; |
| 52 | + laps[lapHead].count = lapCount + 1; |
| 53 | + 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; |
| 62 | +} |
| 63 | + |
| 64 | +int StopWatchController::GetLapCount() { |
| 65 | + return lapCount; |
| 66 | +} |
| 67 | + |
| 68 | +int Wrap(int index) { |
| 69 | + return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY; |
| 70 | +} |
| 71 | + |
| 72 | +LapInfo* StopWatchController::LastLap(int lap) { |
| 73 | + if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) { |
| 74 | + // Return "empty" LapInfo_t |
| 75 | + return &emptyLapInfo; |
| 76 | + } |
| 77 | + // Index backwards |
| 78 | + int index = Wrap(lapHead - lap); |
| 79 | + return &laps[index]; |
| 80 | +} |
| 81 | + |
| 82 | +// Data / State acess |
| 83 | + |
| 84 | +TickType_t StopWatchController::GetElapsedTime() { |
| 85 | + if (!IsRunning()) { |
| 86 | + return timeElapsedPreviously; |
| 87 | + } |
| 88 | + return timeElapsedPreviously + CalculateDelta(startTime, xTaskGetTickCount()); |
| 89 | +} |
| 90 | + |
| 91 | +bool StopWatchController::IsRunning() { |
| 92 | + return currentState == StopWatchStates::Running; |
| 93 | +} |
| 94 | + |
| 95 | +bool StopWatchController::IsCleared() { |
| 96 | + return currentState == StopWatchStates::Cleared; |
| 97 | +} |
| 98 | + |
| 99 | +bool StopWatchController::IsPaused() { |
| 100 | + return currentState == StopWatchStates::Paused; |
| 101 | +} |
0 commit comments