|
| 1 | +#include "StopWatchController.h" |
| 2 | +#include <cstdlib> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +using namespace Pinetime::Controllers; |
| 6 | + |
| 7 | +namespace { |
| 8 | + TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) { |
| 9 | + TickType_t delta = 0; |
| 10 | + // Take care of overflow |
| 11 | + if (startTime > currentTime) { |
| 12 | + delta = 0xffffffff - startTime; |
| 13 | + delta += (currentTime + 1); |
| 14 | + } else { |
| 15 | + delta = currentTime - startTime; |
| 16 | + } |
| 17 | + return delta; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +StopWatch::StopWatch() { |
| 22 | + clear(); |
| 23 | +} |
| 24 | + |
| 25 | +// State Change |
| 26 | + |
| 27 | +void StopWatch::start(TickType_t start) { |
| 28 | + currentState = StopWatchStates::Running; |
| 29 | + startTime = start; |
| 30 | +} |
| 31 | + |
| 32 | +void StopWatch::pause(TickType_t end) { |
| 33 | + currentState = StopWatchStates::Paused; |
| 34 | + timeElapsedPreviously += calculateDelta(startTime, end); |
| 35 | +} |
| 36 | + |
| 37 | +void StopWatch::clear() { |
| 38 | + currentState = StopWatchStates::Cleared; |
| 39 | + timeElapsedPreviously = 0; |
| 40 | + |
| 41 | + for (int i = 0; i < LAP_CAPACITY; i++) { |
| 42 | + laps[i].count = 0; |
| 43 | + laps[i].time = 0; |
| 44 | + } |
| 45 | + lapCount = 0; |
| 46 | + lapHead = 0; |
| 47 | +} |
| 48 | + |
| 49 | +// Lap |
| 50 | + |
| 51 | +void StopWatch::pushLap(TickType_t lapEnd) { |
| 52 | + laps[lapHead].time = lapEnd; |
| 53 | + laps[lapHead].count = lapCount + 1; |
| 54 | + lapCount += 1; |
| 55 | + lapHead = lapCount % LAP_CAPACITY; |
| 56 | +} |
| 57 | + |
| 58 | +int StopWatch::getLapNum() { |
| 59 | + if (lapCount < LAP_CAPACITY) |
| 60 | + return lapCount; |
| 61 | + else |
| 62 | + return LAP_CAPACITY; |
| 63 | +} |
| 64 | + |
| 65 | +int StopWatch::getLapCount() { |
| 66 | + return lapCount; |
| 67 | +} |
| 68 | + |
| 69 | +int wrap(int index) { |
| 70 | + return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY; |
| 71 | +} |
| 72 | + |
| 73 | +LapInfo_t* StopWatch::lastLap(int lap) { |
| 74 | + if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) { |
| 75 | + // Return "empty" LapInfo_t |
| 76 | + return &emptyLapInfo; |
| 77 | + } |
| 78 | + // Index backwards |
| 79 | + int index = wrap(lapHead - lap); |
| 80 | + return &laps[index]; |
| 81 | +} |
| 82 | + |
| 83 | +// Data acess |
| 84 | + |
| 85 | +TickType_t StopWatch::getStart() { |
| 86 | + return startTime; |
| 87 | +} |
| 88 | + |
| 89 | +TickType_t StopWatch::getElapsedPreviously() { |
| 90 | + return timeElapsedPreviously; |
| 91 | +} |
| 92 | + |
| 93 | +bool StopWatch::isRunning() { |
| 94 | + return currentState == StopWatchStates::Running; |
| 95 | +} |
| 96 | + |
| 97 | +bool StopWatch::isCleared() { |
| 98 | + return currentState == StopWatchStates::Cleared; |
| 99 | +} |
| 100 | + |
| 101 | +bool StopWatch::isPaused() { |
| 102 | + return currentState == StopWatchStates::Paused; |
| 103 | +} |
0 commit comments