Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
45d4fab
StopWatch: add persistence
codingjourney Oct 20, 2024
3a772a3
minor fixes:
codingjourney Oct 23, 2024
f867614
lap storage as CircularBuffer, minor fixes
codingjourney Oct 24, 2024
ab51b9a
improved naming of lap-related fields and methods
codingjourney Oct 26, 2024
81e6272
removed superfluous default values in controller
codingjourney Oct 26, 2024
1218232
render accurate time at pause
codingjourney Oct 27, 2024
767ed72
fixed issues found by the test-format CI job
codingjourney Oct 28, 2024
1581196
common method for entering the Paused state
codingjourney Oct 30, 2024
3327140
added missing newline
codingjourney Oct 30, 2024
4f77163
fixed an integer overflow bug in time rendering
codingjourney Nov 18, 2024
e265287
upper bound for lap numbers
codingjourney Nov 28, 2024
fd2a112
upper bound for elapsed time
codingjourney Nov 28, 2024
bf85920
fixed layout of lap data
codingjourney Nov 28, 2024
5f85072
improved layout, improved re-alignment of time fields
codingjourney Nov 30, 2024
6431b40
length of lap list adapting to available space
codingjourney Nov 30, 2024
fe88cad
tweaked some margins to improve aesthetics
codingjourney Dec 4, 2024
026be75
fixed issues found by the test-format CI job
codingjourney Dec 7, 2024
9595b94
elapsedTimeBoundary as constexpr
codingjourney Dec 12, 2024
da265da
prevent unnecessary redrawing of the time label
codingjourney Dec 12, 2024
5461ddb
tightened declarations of integer fields
codingjourney Dec 14, 2024
fdf0653
lap times without leading zeroes
codingjourney Dec 14, 2024
d7d336e
fixed issues found by the test-format CI job
codingjourney Dec 21, 2024
88ba670
fixed a type declaration
codingjourney Jan 14, 2025
931723c
fixed irregular pause mode blinking at clock wraparound
codingjourney Jan 20, 2025
8ef295d
Fix formatting
JF002 Nov 4, 2025
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 @@ -469,6 +469,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 @@ -538,6 +539,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 @@ -657,6 +659,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 @@ -82,6 +82,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 @@ -98,6 +99,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
settingsController {settingsController},
motorController {motorController},
motionController {motionController},
stopWatchController {stopWatchController},
alarmController {alarmController},
brightnessController {brightnessController},
touchHandler {touchHandler},
Expand All @@ -113,6 +115,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