|
| 1 | +#include "components/stopwatch/StopWatchController.h" |
| 2 | + |
| 3 | +using namespace Pinetime::Controllers; |
| 4 | + |
| 5 | +StopWatchController::StopWatchController() { |
| 6 | + Clear(); |
| 7 | +} |
| 8 | + |
| 9 | +// State Change |
| 10 | + |
| 11 | +void StopWatchController::Start() { |
| 12 | + currentState = StopWatchStates::Running; |
| 13 | + startTime = xTaskGetTickCount(); |
| 14 | +} |
| 15 | + |
| 16 | +void StopWatchController::Pause() { |
| 17 | + timeElapsedPreviously = GetElapsedTime(); |
| 18 | + currentState = StopWatchStates::Paused; |
| 19 | +} |
| 20 | + |
| 21 | +void StopWatchController::Clear() { |
| 22 | + currentState = StopWatchStates::Cleared; |
| 23 | + timeElapsedPreviously = 0; |
| 24 | + |
| 25 | + for (uint8_t i = 0; i < histSize; i++) { |
| 26 | + history[i].number = 0; |
| 27 | + history[i].timeSinceStart = 0; |
| 28 | + } |
| 29 | + maxLapNumber = 0; |
| 30 | +} |
| 31 | + |
| 32 | +// Lap |
| 33 | + |
| 34 | +void StopWatchController::AddLapToHistory() { |
| 35 | + TickType_t lapEnd = GetElapsedTime(); |
| 36 | + history--; |
| 37 | + history[0].timeSinceStart = lapEnd; |
| 38 | + history[0].number = ++maxLapNumber % lapNumberBoundary; |
| 39 | +} |
| 40 | + |
| 41 | +uint16_t StopWatchController::GetMaxLapNumber() { |
| 42 | + return maxLapNumber; |
| 43 | +} |
| 44 | + |
| 45 | +std::optional<LapInfo> StopWatchController::GetLapFromHistory(uint8_t index) { |
| 46 | + if (index >= histSize || history[index].number == 0) { |
| 47 | + return {}; |
| 48 | + } |
| 49 | + return history[index]; |
| 50 | +} |
| 51 | + |
| 52 | +// Data / State acess |
| 53 | + |
| 54 | +TickType_t StopWatchController::GetElapsedTime() { |
| 55 | + if (!IsRunning()) { |
| 56 | + return timeElapsedPreviously; |
| 57 | + } |
| 58 | + TickType_t delta = xTaskGetTickCount() - startTime; |
| 59 | + return (timeElapsedPreviously + delta) % elapsedTimeBoundary; |
| 60 | +} |
| 61 | + |
| 62 | +bool StopWatchController::IsRunning() { |
| 63 | + return currentState == StopWatchStates::Running; |
| 64 | +} |
| 65 | + |
| 66 | +bool StopWatchController::IsCleared() { |
| 67 | + return currentState == StopWatchStates::Cleared; |
| 68 | +} |
| 69 | + |
| 70 | +bool StopWatchController::IsPaused() { |
| 71 | + return currentState == StopWatchStates::Paused; |
| 72 | +} |
0 commit comments