Skip to content

Commit 32bd5fb

Browse files
hank.milliken94@protonmail.comhank.milliken94@protonmail.com
authored andcommitted
feat: add persistance to sotpwatch
1 parent 6a6981c commit 32bd5fb

File tree

15 files changed

+362
-160
lines changed

15 files changed

+362
-160
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ list(APPEND SOURCE_FILES
467467
components/settings/Settings.cpp
468468
components/timer/Timer.cpp
469469
components/alarm/AlarmController.cpp
470+
components/stopwatch/StopWatchController.cpp
470471
components/fs/FS.cpp
471472
drivers/Cst816s.cpp
472473
FreeRTOS/port.c
@@ -536,6 +537,7 @@ list(APPEND RECOVERY_SOURCE_FILES
536537
components/settings/Settings.cpp
537538
components/timer/Timer.cpp
538539
components/alarm/AlarmController.cpp
540+
components/stopwatch/StopWatchController.cpp
539541
drivers/Cst816s.cpp
540542
FreeRTOS/port.c
541543
FreeRTOS/port_cmsis_systick.c
@@ -655,6 +657,7 @@ set(INCLUDE_FILES
655657
components/settings/Settings.h
656658
components/timer/Timer.h
657659
components/alarm/AlarmController.h
660+
components/stopwatch/StopWatchController.h
658661
drivers/Cst816s.h
659662
FreeRTOS/portmacro.h
660663
FreeRTOS/portmacro_cmsis.h
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
3+
#include <FreeRTOS.h>
4+
#include <optional>
5+
#include <timers.h>
6+
#include "utility/CircularBuffer.h"
7+
8+
namespace Pinetime {
9+
namespace System {
10+
class SystemTask;
11+
}
12+
13+
namespace Controllers {
14+
15+
enum class StopWatchStates { Cleared, Running, Paused };
16+
17+
struct LapInfo {
18+
uint16_t number = 0; // Used to label the lap
19+
TickType_t timeSinceStart = 0; // Excluding pauses
20+
};
21+
22+
class StopWatchController {
23+
public:
24+
StopWatchController();
25+
26+
// StopWatch functionality and data
27+
void Start();
28+
void Pause();
29+
void Clear();
30+
31+
TickType_t GetElapsedTime();
32+
33+
// Lap functionality
34+
35+
/// Only the latest histSize laps are stored
36+
void AddLapToHistory();
37+
38+
/// Returns maxLapNumber
39+
uint16_t GetMaxLapNumber();
40+
41+
/// Indexes into lap history, with 0 being the latest lap.
42+
std::optional<LapInfo> GetLapFromHistory(uint8_t index);
43+
44+
bool IsRunning();
45+
bool IsCleared();
46+
bool IsPaused();
47+
48+
private:
49+
// Time at which stopwatch wraps around to zero (1000 hours)
50+
static constexpr TickType_t elapsedTimeBoundary = static_cast<TickType_t>(configTICK_RATE_HZ) * 60 * 60 * 1000;
51+
// Current state of stopwatch
52+
StopWatchStates currentState = StopWatchStates::Cleared;
53+
// Start time of current duration
54+
TickType_t startTime;
55+
// How much time was elapsed before current duration
56+
TickType_t timeElapsedPreviously;
57+
58+
// Maximum number of stored laps
59+
static constexpr uint8_t histSize = 4;
60+
// Value at which lap numbers wrap around to zero
61+
static constexpr uint16_t lapNumberBoundary = 1000;
62+
// Lap storage
63+
Utility::CircularBuffer<LapInfo, histSize> history;
64+
// Highest lap number; less than lapNumberBoundary, may exceed histSize
65+
uint16_t maxLapNumber;
66+
};
67+
}
68+
}

src/displayapp/Controllers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Pinetime {
1919
class MotorController;
2020
class MotionController;
2121
class AlarmController;
22+
class StopWatchController;
2223
class BrightnessController;
2324
class SimpleWeatherService;
2425
class FS;
@@ -42,6 +43,7 @@ namespace Pinetime {
4243
Pinetime::Controllers::MotorController& motorController;
4344
Pinetime::Controllers::MotionController& motionController;
4445
Pinetime::Controllers::AlarmController& alarmController;
46+
Pinetime::Controllers::StopWatchController& stopWatchController;
4547
Pinetime::Controllers::BrightnessController& brightnessController;
4648
Pinetime::Controllers::SimpleWeatherService* weatherController;
4749
Pinetime::Controllers::FS& filesystem;

src/displayapp/DisplayApp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
7979
Pinetime::Controllers::MotorController& motorController,
8080
Pinetime::Controllers::MotionController& motionController,
8181
Pinetime::Controllers::AlarmController& alarmController,
82+
Pinetime::Controllers::StopWatchController& stopWatchController,
8283
Pinetime::Controllers::BrightnessController& brightnessController,
8384
Pinetime::Controllers::TouchHandler& touchHandler,
8485
Pinetime::Controllers::FS& filesystem,
@@ -95,6 +96,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
9596
motorController {motorController},
9697
motionController {motionController},
9798
alarmController {alarmController},
99+
stopWatchController {stopWatchController},
98100
brightnessController {brightnessController},
99101
touchHandler {touchHandler},
100102
filesystem {filesystem},
@@ -110,6 +112,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
110112
motorController,
111113
motionController,
112114
alarmController,
115+
stopWatchController,
113116
brightnessController,
114117
nullptr,
115118
filesystem,
@@ -527,6 +530,7 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
527530
batteryController,
528531
bleController,
529532
alarmController,
533+
stopWatchController,
530534
dateTimeController,
531535
filesystem,
532536
std::move(apps));

src/displayapp/DisplayApp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "displayapp/screens/Screen.h"
1515
#include "components/timer/Timer.h"
1616
#include "components/alarm/AlarmController.h"
17+
#include "components/stopwatch/StopWatchController.h"
1718
#include "touchhandler/TouchHandler.h"
1819

1920
#include "displayapp/Messages.h"
@@ -64,6 +65,7 @@ namespace Pinetime {
6465
Pinetime::Controllers::MotorController& motorController,
6566
Pinetime::Controllers::MotionController& motionController,
6667
Pinetime::Controllers::AlarmController& alarmController,
68+
Pinetime::Controllers::StopWatchController& stopWatchController,
6769
Pinetime::Controllers::BrightnessController& brightnessController,
6870
Pinetime::Controllers::TouchHandler& touchHandler,
6971
Pinetime::Controllers::FS& filesystem,
@@ -94,6 +96,7 @@ namespace Pinetime {
9496
Pinetime::Controllers::MotorController& motorController;
9597
Pinetime::Controllers::MotionController& motionController;
9698
Pinetime::Controllers::AlarmController& alarmController;
99+
Pinetime::Controllers::StopWatchController& stopWatchController;
97100
Pinetime::Controllers::BrightnessController& brightnessController;
98101
Pinetime::Controllers::TouchHandler& touchHandler;
99102
Pinetime::Controllers::FS& filesystem;

src/displayapp/DisplayAppRecovery.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
2222
Pinetime::Controllers::MotorController& /*motorController*/,
2323
Pinetime::Controllers::MotionController& /*motionController*/,
2424
Pinetime::Controllers::AlarmController& /*alarmController*/,
25+
Pinetime::Controllers::StopWatchController& /*stopWatchController*/,
2526
Pinetime::Controllers::BrightnessController& /*brightnessController*/,
2627
Pinetime::Controllers::TouchHandler& /*touchHandler*/,
2728
Pinetime::Controllers::FS& /*filesystem*/,

src/displayapp/DisplayAppRecovery.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace Pinetime {
3232
class TouchHandler;
3333
class MotorController;
3434
class AlarmController;
35+
class StopWatchController;
3536
class BrightnessController;
3637
class FS;
3738
class SimpleWeatherService;
@@ -58,6 +59,7 @@ namespace Pinetime {
5859
Pinetime::Controllers::MotorController& motorController,
5960
Pinetime::Controllers::MotionController& motionController,
6061
Pinetime::Controllers::AlarmController& alarmController,
62+
Pinetime::Controllers::StopWatchController& stopWatchController,
6163
Pinetime::Controllers::BrightnessController& brightnessController,
6264
Pinetime::Controllers::TouchHandler& touchHandler,
6365
Pinetime::Controllers::FS& filesystem,

src/displayapp/screens/ApplicationList.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ApplicationList::ApplicationList(DisplayApp* app,
2222
const Pinetime::Controllers::Battery& batteryController,
2323
const Pinetime::Controllers::Ble& bleController,
2424
const Pinetime::Controllers::AlarmController& alarmController,
25+
const Pinetime::Controllers::StopWatchController& stopWatchController,
2526
Controllers::DateTime& dateTimeController,
2627
Pinetime::Controllers::FS& filesystem,
2728
std::array<Tile::Applications, UserAppTypes::Count>&& apps)
@@ -30,6 +31,7 @@ ApplicationList::ApplicationList(DisplayApp* app,
3031
batteryController {batteryController},
3132
bleController {bleController},
3233
alarmController {alarmController},
34+
stopWatchController {stopWatchController},
3335
dateTimeController {dateTimeController},
3436
filesystem {filesystem},
3537
apps {std::move(apps)},

src/displayapp/screens/ApplicationList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Pinetime {
1919
const Pinetime::Controllers::Battery& batteryController,
2020
const Pinetime::Controllers::Ble& bleController,
2121
const Pinetime::Controllers::AlarmController& alarmController,
22+
const Pinetime::Controllers::StopWatchController& stopWatchController,
2223
Controllers::DateTime& dateTimeController,
2324
Pinetime::Controllers::FS& filesystem,
2425
std::array<Tile::Applications, UserAppTypes::Count>&& apps);
@@ -34,6 +35,7 @@ namespace Pinetime {
3435
const Pinetime::Controllers::Battery& batteryController;
3536
const Pinetime::Controllers::Ble& bleController;
3637
const Pinetime::Controllers::AlarmController& alarmController;
38+
const Pinetime::Controllers::StopWatchController& stopWatchController;
3739
Controllers::DateTime& dateTimeController;
3840
Pinetime::Controllers::FS& filesystem;
3941
std::array<Tile::Applications, UserAppTypes::Count> apps;

0 commit comments

Comments
 (0)