Skip to content

Commit 140a157

Browse files
committed
PR InfiniTimeOrg#783: Stopwatch Persistence
Squashed commit of the following: commit ec72f56 Author: Louis Pearson <louispearson@librem.one> Date: Tue Oct 26 19:03:46 2021 -0600 Add battery icon to stopwatch screen commit 8fbc164 Author: Louis Pearson <louispearson@librem.one> Date: Tue Oct 26 18:23:05 2021 -0600 Make `make all` complete successfully - Added StopWatchController to DisplayAppRecovery - Added StopWatchController to everywhere needed in CMakeLists - Fixed the reorder warning commit a4a0a4c Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 07:13:40 2021 -0600 Prevent lap count check when watch is stopped commit c7c6309 Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 06:53:05 2021 -0600 Run clang-format on files changed in branch commit 858edfc Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 06:43:16 2021 -0600 Make function names more descriptive Also removes OnButtonPressed. I prefer the button's function to stay the same when possible, but I'll change it if needed for the PR. commit 9ea6bc3 Author: Louis Pearson <louispearson@librem.one> Date: Mon Oct 25 06:11:27 2021 -0600 Removed unsigned ints These appear to have been the cause of my issues. Not sure why, I'd have to know the nrf52832 better to diagnose it. commit 2fd2cdb Author: Louis Pearson <louispearson@librem.one> Date: Sun Oct 24 20:41:02 2021 -0600 Implement lap tracking commit 04a6b70 Author: Louis Pearson <louispearson@librem.one> Date: Sun Oct 24 10:29:34 2021 -0600 Fix stopwatch display issues commit ec2b673 Author: Louis Pearson <louispearson@librem.one> Date: Sun Oct 24 08:46:38 2021 -0600 Implement stop watch controller commit c75102f Author: Louis Pearson <louispearson@librem.one> Date: Sat Oct 23 23:28:07 2021 -0600 This may have been a bad idea commit 3a51035 Author: Louis Pearson <louispearson@librem.one> Date: Sat Oct 23 21:18:45 2021 -0600 Add time of day to stopwatch app
1 parent 9777107 commit 140a157

File tree

12 files changed

+307
-108
lines changed

12 files changed

+307
-108
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ list(APPEND SOURCE_FILES
496496
components/motor/MotorController.cpp
497497
components/settings/Settings.cpp
498498
components/timer/TimerController.cpp
499+
components/stopwatch/StopWatchController.cpp
499500
components/alarm/AlarmController.cpp
500501
components/fs/FS.cpp
501502
drivers/Cst816s.cpp
@@ -568,6 +569,7 @@ list(APPEND RECOVERY_SOURCE_FILES
568569
components/settings/Settings.cpp
569570
components/timer/TimerController.cpp
570571
components/alarm/AlarmController.cpp
572+
components/stopwatch/StopWatchController.cpp
571573
drivers/Cst816s.cpp
572574
FreeRTOS/port.c
573575
FreeRTOS/port_cmsis_systick.c
@@ -681,6 +683,7 @@ set(INCLUDE_FILES
681683
components/settings/Settings.h
682684
components/timer/TimerController.h
683685
components/alarm/AlarmController.h
686+
components/stopwatch/StopWatchController.h
684687
drivers/Cst816s.h
685688
FreeRTOS/portmacro.h
686689
FreeRTOS/portmacro_cmsis.h
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "StopWatchController.h"
2+
#include <cstdlib>
3+
#include <cstring>
4+
5+
using namespace Pinetime::Controllers;
6+
7+
namespace {
8+
TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) {
9+
TickType_t delta = 0;
10+
// Take care of overflow
11+
if (startTime > currentTime) {
12+
delta = 0xffffffff - startTime;
13+
delta += (currentTime + 1);
14+
} else {
15+
delta = currentTime - startTime;
16+
}
17+
return delta;
18+
}
19+
}
20+
21+
StopWatch::StopWatch() {
22+
clear();
23+
}
24+
25+
// State Change
26+
27+
void StopWatch::start(TickType_t start) {
28+
currentState = StopWatchStates::Running;
29+
startTime = start;
30+
}
31+
32+
void StopWatch::pause(TickType_t end) {
33+
currentState = StopWatchStates::Paused;
34+
timeElapsedPreviously += calculateDelta(startTime, end);
35+
}
36+
37+
void StopWatch::clear() {
38+
currentState = StopWatchStates::Cleared;
39+
timeElapsedPreviously = 0;
40+
41+
for (int i = 0; i < LAP_CAPACITY; i++) {
42+
laps[i].count = 0;
43+
laps[i].time = 0;
44+
}
45+
lapCount = 0;
46+
lapHead = 0;
47+
}
48+
49+
// Lap
50+
51+
void StopWatch::pushLap(TickType_t lapEnd) {
52+
laps[lapHead].time = lapEnd;
53+
laps[lapHead].count = lapCount + 1;
54+
lapCount += 1;
55+
lapHead = lapCount % LAP_CAPACITY;
56+
}
57+
58+
int StopWatch::getLapNum() {
59+
if (lapCount < LAP_CAPACITY)
60+
return lapCount;
61+
else
62+
return LAP_CAPACITY;
63+
}
64+
65+
int StopWatch::getLapCount() {
66+
return lapCount;
67+
}
68+
69+
int wrap(int index) {
70+
return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY;
71+
}
72+
73+
LapInfo_t* StopWatch::lastLap(int lap) {
74+
if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) {
75+
// Return "empty" LapInfo_t
76+
return &emptyLapInfo;
77+
}
78+
// Index backwards
79+
int index = wrap(lapHead - lap);
80+
return &laps[index];
81+
}
82+
83+
// Data acess
84+
85+
TickType_t StopWatch::getStart() {
86+
return startTime;
87+
}
88+
89+
TickType_t StopWatch::getElapsedPreviously() {
90+
return timeElapsedPreviously;
91+
}
92+
93+
bool StopWatch::isRunning() {
94+
return currentState == StopWatchStates::Running;
95+
}
96+
97+
bool StopWatch::isCleared() {
98+
return currentState == StopWatchStates::Cleared;
99+
}
100+
101+
bool StopWatch::isPaused() {
102+
return currentState == StopWatchStates::Paused;
103+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include "FreeRTOS.h"
5+
6+
#define LAP_CAPACITY 2
7+
8+
namespace Pinetime {
9+
namespace System {
10+
class SystemTask;
11+
}
12+
namespace Controllers {
13+
14+
enum class StopWatchStates { Cleared, Running, Paused };
15+
16+
struct LapInfo_t {
17+
int count = 0; // Used to label the lap
18+
TickType_t time = 0; // delta time from beginning of stopwatch
19+
};
20+
21+
class StopWatch {
22+
public:
23+
StopWatch();
24+
25+
// StopWatch functionality and data
26+
void start(TickType_t start);
27+
void pause(TickType_t end);
28+
void clear();
29+
30+
TickType_t getStart();
31+
TickType_t getElapsedPreviously();
32+
33+
// Lap functionality
34+
35+
/// Only the latest laps are stored, the lap count is saved until reset
36+
void pushLap(TickType_t lapEnd);
37+
38+
/// Returns actual count of stored laps
39+
int getLapNum();
40+
41+
/// Returns lapCount
42+
int getLapCount();
43+
44+
/// Indexes into lap history, with 0 being the latest lap.
45+
/// If the lap is unavailable, count and time will be 0. If there is a
46+
/// real value, count should be above 0
47+
LapInfo_t* lastLap(int lap = 0);
48+
49+
bool isRunning();
50+
bool isCleared();
51+
bool isPaused();
52+
53+
private:
54+
// Current state of stopwatch
55+
StopWatchStates currentState = StopWatchStates::Cleared;
56+
// Start time of current duration
57+
TickType_t startTime = 0;
58+
// How much time was elapsed before current duration
59+
TickType_t timeElapsedPreviously = 0;
60+
// Stores lap times
61+
LapInfo_t laps[LAP_CAPACITY];
62+
LapInfo_t emptyLapInfo = {.count = 0, .time = 0};
63+
int lapCount = 0;
64+
int lapHead = 0;
65+
};
66+
}
67+
}

src/displayapp/DisplayApp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "components/ble/NotificationManager.h"
1414
#include "components/motion/MotionController.h"
1515
#include "components/motor/MotorController.h"
16+
#include "components/stopwatch/StopWatchController.h"
1617
#include "displayapp/screens/ApplicationList.h"
1718
#include "displayapp/screens/Brightness.h"
1819
#include "displayapp/screens/Clock.h"
@@ -99,6 +100,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
99100
Pinetime::Controllers::MotionController& motionController,
100101
Pinetime::Controllers::TimerController& timerController,
101102
Pinetime::Controllers::AlarmController& alarmController,
103+
Pinetime::Controllers::StopWatch& stopWatchController,
102104
Pinetime::Controllers::TouchHandler& touchHandler)
103105
: lcd {lcd},
104106
lvgl {lvgl},
@@ -114,6 +116,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
114116
motionController {motionController},
115117
timerController {timerController},
116118
alarmController {alarmController},
119+
stopWatchController {stopWatchController},
117120
touchHandler {touchHandler} {
118121
}
119122

@@ -437,7 +440,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
437440
ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
438441
break;
439442
case Apps::StopWatch:
440-
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask);
443+
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask, dateTimeController, stopWatchController, batteryController);
441444
break;
442445
case Apps::Twos:
443446
currentScreen = std::make_unique<Screens::Twos>(this);

src/displayapp/DisplayApp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "components/timer/TimerController.h"
1717
#include "components/alarm/AlarmController.h"
1818
#include "touchhandler/TouchHandler.h"
19+
#include "components/stopwatch/StopWatchController.h"
1920

2021
#include "displayapp/Messages.h"
2122
#include "BootErrors.h"
@@ -36,6 +37,7 @@ namespace Pinetime {
3637
class HeartRateController;
3738
class MotionController;
3839
class TouchHandler;
40+
class StopWatch;
3941
}
4042

4143
namespace System {
@@ -61,6 +63,7 @@ namespace Pinetime {
6163
Pinetime::Controllers::MotionController& motionController,
6264
Pinetime::Controllers::TimerController& timerController,
6365
Pinetime::Controllers::AlarmController& alarmController,
66+
Pinetime::Controllers::StopWatch& stopWatchController,
6467
Pinetime::Controllers::TouchHandler& touchHandler);
6568
void Start(System::BootErrors error);
6669
void PushMessage(Display::Messages msg);
@@ -87,6 +90,7 @@ namespace Pinetime {
8790
Pinetime::Controllers::MotionController& motionController;
8891
Pinetime::Controllers::TimerController& timerController;
8992
Pinetime::Controllers::AlarmController& alarmController;
93+
Pinetime::Controllers::StopWatch& stopWatchController;
9094
Pinetime::Controllers::TouchHandler& touchHandler;
9195

9296
Pinetime::Controllers::FirmwareValidator validator;

src/displayapp/DisplayAppRecovery.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
2323
Pinetime::Controllers::MotionController& motionController,
2424
Pinetime::Controllers::TimerController& timerController,
2525
Pinetime::Controllers::AlarmController& alarmController,
26+
Pinetime::Controllers::StopWatch& stopWatchController,
2627
Pinetime::Controllers::TouchHandler& touchHandler)
2728
: lcd {lcd}, bleController {bleController} {
2829
}

src/displayapp/DisplayAppRecovery.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace Pinetime {
3434
class MotorController;
3535
class TimerController;
3636
class AlarmController;
37+
class StopWatch;
3738
}
3839

3940
namespace System {
@@ -57,6 +58,7 @@ namespace Pinetime {
5758
Pinetime::Controllers::MotionController& motionController,
5859
Pinetime::Controllers::TimerController& timerController,
5960
Pinetime::Controllers::AlarmController& alarmController,
61+
Pinetime::Controllers::StopWatch& stopWatchController,
6062
Pinetime::Controllers::TouchHandler& touchHandler);
6163
void Start();
6264
void Start(Pinetime::System::BootErrors) {

0 commit comments

Comments
 (0)