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 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
764a8fe
StopWatch: add persistence
codingjourney Oct 20, 2024
4db6a09
minor fixes:
codingjourney Oct 23, 2024
0a730c0
lap storage as CircularBuffer, minor fixes
codingjourney Oct 24, 2024
277b329
improved naming of lap-related fields and methods
codingjourney Oct 26, 2024
0fda703
removed superfluous default values in controller
codingjourney Oct 26, 2024
9a30b18
render accurate time at pause
codingjourney Oct 27, 2024
81dc7d6
fixed issues found by the test-format CI job
codingjourney Oct 28, 2024
8a64388
common method for entering the Paused state
codingjourney Oct 30, 2024
0dfae72
added missing newline
codingjourney Oct 30, 2024
0d0af6e
fixed an integer overflow bug in time rendering
codingjourney Nov 18, 2024
d3820f3
upper bound for lap numbers
codingjourney Nov 28, 2024
41da56e
upper bound for elapsed time
codingjourney Nov 28, 2024
a715ff3
fixed layout of lap data
codingjourney Nov 28, 2024
a6122a2
improved layout, improved re-alignment of time fields
codingjourney Nov 30, 2024
87be94e
length of lap list adapting to available space
codingjourney Nov 30, 2024
a6edd41
tweaked some margins to improve aesthetics
codingjourney Dec 4, 2024
b9def19
reduced heap size to fix a build error
codingjourney Dec 7, 2024
42c5913
fixed issues found by the test-format CI job
codingjourney Dec 7, 2024
1b665bf
elapsedTimeBoundary as constexpr
codingjourney Dec 12, 2024
42729f8
prevent unnecessary redrawing of the time label
codingjourney Dec 12, 2024
135e5f8
tightened declarations of integer fields
codingjourney Dec 14, 2024
5c2dba1
lap times without leading zeroes
codingjourney Dec 14, 2024
4aa51f3
Merge branch 'main' into stopwatch-persistence
codingjourney Dec 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 72 additions & 0 deletions src/components/stopwatch/StopWatchController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#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() {
timeElapsedPreviously = GetElapsedTime();
currentState = StopWatchStates::Paused;
}

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

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

// Lap

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

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

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

// Data / State acess

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

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

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

bool StopWatchController::IsPaused() {
return currentState == StopWatchStates::Paused;
}
68 changes: 68 additions & 0 deletions src/components/stopwatch/StopWatchController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#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 {
uint16_t 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
uint16_t GetMaxLapNumber();

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

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

private:
// Time at which stopwatch wraps around to zero (1000 hours)
static constexpr TickType_t elapsedTimeBoundary = static_cast<TickType_t>(configTICK_RATE_HZ) * 60 * 60 * 1000;
// 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 uint8_t histSize = 4;
// Value at which lap numbers wrap around to zero
static constexpr uint16_t lapNumberBoundary = 1000;
// Lap storage
Utility::CircularBuffer<LapInfo, histSize> history;
// Highest lap number; less than lapNumberBoundary, may exceed histSize
uint16_t 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