Skip to content
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

StopWatch: add persistence #2141

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ list(APPEND SOURCE_FILES
components/motor/MotorController.cpp
components/settings/Settings.cpp
components/timer/Timer.cpp
components/stopwatch/StopWatchController.cpp
components/alarm/AlarmController.cpp
components/fs/FS.cpp
drivers/Cst816s.cpp
Expand Down Expand Up @@ -535,6 +536,7 @@ list(APPEND RECOVERY_SOURCE_FILES
components/firmwarevalidator/FirmwareValidator.cpp
components/settings/Settings.cpp
components/timer/Timer.cpp
components/stopwatch/StopWatchController.cpp
components/alarm/AlarmController.cpp
drivers/Cst816s.cpp
FreeRTOS/port.c
Expand Down Expand Up @@ -654,6 +656,7 @@ set(INCLUDE_FILES
components/ble/SimpleWeatherService.h
components/settings/Settings.h
components/timer/Timer.h
components/stopwatch/StopWatchController.h
components/alarm/AlarmController.h
drivers/Cst816s.h
FreeRTOS/portmacro.h
Expand Down
71 changes: 71 additions & 0 deletions src/components/stopwatch/StopWatchController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "components/stopwatch/StopWatchController.h"

using namespace Pinetime::Controllers;

StopWatchController::StopWatchController() {
Clear();
}

// State Change

void StopWatchController::Start() {
currentState = StopWatchStates::Running;
startTime = xTaskGetTickCount();
}

void StopWatchController::Pause() {
currentState = StopWatchStates::Paused;
timeElapsedPreviously += xTaskGetTickCount() - startTime;
}

void StopWatchController::Clear() {
currentState = StopWatchStates::Cleared;
timeElapsedPreviously = 0;

for (int i = 0; i < histSize; i++) {
history[i].number = 0;
history[i].timeSinceStart = 0;
}
maxLapNumber = 0;
}

// Lap

void StopWatchController::AddLapToHistory() {
TickType_t lapEnd = GetElapsedTime();
history[0].timeSinceStart = lapEnd;
history[0].number = ++maxLapNumber;
history--;
}

int StopWatchController::GetMaxLapNumber() {
return maxLapNumber;
}

std::optional<LapInfo> StopWatchController::GetLapFromHistory(int index) {
if (index < 0 || index >= histSize || history[index].number == 0) {
return {};
}
return history[index];
}

// Data / State acess

TickType_t StopWatchController::GetElapsedTime() {
if (!IsRunning()) {
return timeElapsedPreviously;
}
return timeElapsedPreviously + (xTaskGetTickCount() - startTime);
}

bool StopWatchController::IsRunning() {
return currentState == StopWatchStates::Running;
}

bool StopWatchController::IsCleared() {
return currentState == StopWatchStates::Cleared;
}

bool StopWatchController::IsPaused() {
return currentState == StopWatchStates::Paused;
}
64 changes: 64 additions & 0 deletions src/components/stopwatch/StopWatchController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include <FreeRTOS.h>
#include <optional>
#include <timers.h>
#include "utility/CircularBuffer.h"

namespace Pinetime {
namespace System {
class SystemTask;
}

namespace Controllers {

enum class StopWatchStates { Cleared, Running, Paused };

struct LapInfo {
int number = 0; // Used to label the lap
TickType_t timeSinceStart = 0; // Excluding pauses
};

class StopWatchController {
public:
StopWatchController();

// StopWatch functionality and data
void Start();
void Pause();
void Clear();

TickType_t GetElapsedTime();

// Lap functionality

/// Only the latest histSize laps are stored
void AddLapToHistory();

/// Returns maxLapNumber
int GetMaxLapNumber();

/// Indexes into lap history, with 0 being the latest lap.
std::optional<LapInfo> GetLapFromHistory(int index);

bool IsRunning();
bool IsCleared();
bool IsPaused();

private:
// Current state of stopwatch
StopWatchStates currentState = StopWatchStates::Cleared;
// Start time of current duration
TickType_t startTime;
// How much time was elapsed before current duration
TickType_t timeElapsedPreviously;

// Maximum number of stored laps
static constexpr int histSize = 2;
// Lap storage
Utility::CircularBuffer<LapInfo, histSize> history;
// Highest lap number; may exceed histSize
int maxLapNumber;
};
}
}
2 changes: 2 additions & 0 deletions src/displayapp/Controllers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Pinetime {
class Settings;
class MotorController;
class MotionController;
class StopWatchController;
class AlarmController;
class BrightnessController;
class SimpleWeatherService;
Expand All @@ -41,6 +42,7 @@ namespace Pinetime {
Pinetime::Controllers::Settings& settingsController;
Pinetime::Controllers::MotorController& motorController;
Pinetime::Controllers::MotionController& motionController;
Pinetime::Controllers::StopWatchController& stopWatchController;
Pinetime::Controllers::AlarmController& alarmController;
Pinetime::Controllers::BrightnessController& brightnessController;
Pinetime::Controllers::SimpleWeatherService* weatherController;
Expand Down
3 changes: 3 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Controllers::Settings& settingsController,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Expand All @@ -94,6 +95,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
settingsController {settingsController},
motorController {motorController},
motionController {motionController},
stopWatchController {stopWatchController},
alarmController {alarmController},
brightnessController {brightnessController},
touchHandler {touchHandler},
Expand All @@ -109,6 +111,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
settingsController,
motorController,
motionController,
stopWatchController,
alarmController,
brightnessController,
nullptr,
Expand Down
3 changes: 3 additions & 0 deletions src/displayapp/DisplayApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "components/settings/Settings.h"
#include "displayapp/screens/Screen.h"
#include "components/timer/Timer.h"
#include "components/stopwatch/StopWatchController.h"
#include "components/alarm/AlarmController.h"
#include "touchhandler/TouchHandler.h"

Expand Down Expand Up @@ -63,6 +64,7 @@ namespace Pinetime {
Controllers::Settings& settingsController,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Expand Down Expand Up @@ -93,6 +95,7 @@ namespace Pinetime {
Pinetime::Controllers::Settings& settingsController;
Pinetime::Controllers::MotorController& motorController;
Pinetime::Controllers::MotionController& motionController;
Pinetime::Controllers::StopWatchController& stopWatchController;
Pinetime::Controllers::AlarmController& alarmController;
Pinetime::Controllers::BrightnessController& brightnessController;
Pinetime::Controllers::TouchHandler& touchHandler;
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/DisplayAppRecovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Controllers::Settings& /*settingsController*/,
Pinetime::Controllers::MotorController& /*motorController*/,
Pinetime::Controllers::MotionController& /*motionController*/,
Pinetime::Controllers::StopWatchController& /*stopWatchController*/,
Pinetime::Controllers::AlarmController& /*alarmController*/,
Pinetime::Controllers::BrightnessController& /*brightnessController*/,
Pinetime::Controllers::TouchHandler& /*touchHandler*/,
Expand Down
2 changes: 2 additions & 0 deletions src/displayapp/DisplayAppRecovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Pinetime {
class MotionController;
class TouchHandler;
class MotorController;
class StopWatchController;
class AlarmController;
class BrightnessController;
class FS;
Expand All @@ -57,6 +58,7 @@ namespace Pinetime {
Controllers::Settings& settingsController,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::StopWatchController& stopWatchController,
Pinetime::Controllers::AlarmController& alarmController,
Pinetime::Controllers::BrightnessController& brightnessController,
Pinetime::Controllers::TouchHandler& touchHandler,
Expand Down
Loading
Loading