Skip to content

Commit 710b48b

Browse files
author
Louis Pearson
committed
This may have been a bad idea
1 parent ed50ab2 commit 710b48b

8 files changed

Lines changed: 124 additions & 32 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "StopWatchController.h"
2+
#include <cstdlib>
3+
#include <cstring>
4+
5+
using namespace Pinetime::Controllers;
6+
7+
StopWatch::StopWatch() {}
8+
9+
// StopWatch::init() {}
10+
11+
void StopWatch::start(uint32_t start) {
12+
currentState = StopWatchStates::Running;
13+
startTime = start;
14+
}
15+
16+
// void StopWatch::lap(uint32_t lapEnd);
17+
18+
void StopWatch::pause(uint32_t end) {
19+
currentState = StopWatchStates::Paused;
20+
timeElapsedPreviously += end - startTime;
21+
}
22+
23+
void StopWatch::clear() {
24+
currentState = StopWatchStates::Cleared;
25+
timeElapsedPreviously = 0;
26+
}
27+
28+
uint32_t StopWatch::getStart() {
29+
return startTime;
30+
}
31+
32+
uint32_t StopWatch::getElapsedPreviously() {
33+
return timeElapsedPreviously;
34+
}
35+
36+
bool StopWatch::isRunning() {
37+
return currentState == StopWatchStates::Running;
38+
}
39+
40+
bool StopWatch::isClear() {
41+
return currentState == StopWatchStates::Cleared;
42+
}
43+
44+
bool StopWatch::isPaused() {
45+
return currentState == StopWatchStates::Paused;
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include "portmacro_cmsis.h"
5+
6+
namespace Pinetime {
7+
namespace System {
8+
class SystemTask;
9+
}
10+
namespace Controllers {
11+
12+
enum class StopWatchStates { Cleared, Running, Paused };
13+
14+
class StopWatch {
15+
public:
16+
StopWatch() = default;
17+
18+
// void init();
19+
void start(uint32_t start);
20+
// void lap(uint32_t lapEnd);
21+
void pause(uint32_t end);
22+
void clear();
23+
24+
uint32_t getStart();
25+
uint32_t getElapsedPreviously();
26+
27+
bool isRunning();
28+
bool isCleared();
29+
bool isPaused();
30+
31+
private:
32+
StopWatchStates currentState = StopWatchStates::Cleared;
33+
// Start time of current duration
34+
TickType_t startTime;
35+
// How much time was elapsed before current duration
36+
TickType_t timeElapsedPreviously = 0;
37+
};
38+
}
39+
}

src/displayapp/DisplayApp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "components/ble/NotificationManager.h"
1111
#include "components/motion/MotionController.h"
1212
#include "components/motor/MotorController.h"
13+
#include "components/stopwatch/StopWatchController.h"
1314
#include "displayapp/screens/ApplicationList.h"
1415
#include "displayapp/screens/Brightness.h"
1516
#include "displayapp/screens/Clock.h"
@@ -95,6 +96,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
9596
Pinetime::Controllers::MotionController& motionController,
9697
Pinetime::Controllers::TimerController& timerController,
9798
Pinetime::Controllers::AlarmController& alarmController,
99+
Pinetime::Controllers::StopWatchController& stopWatchController,
98100
Pinetime::Controllers::TouchHandler& touchHandler)
99101
: lcd {lcd},
100102
lvgl {lvgl},
@@ -110,6 +112,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
110112
motionController {motionController},
111113
timerController {timerController},
112114
alarmController {alarmController},
115+
stopWatchController {stopWatchController},
113116
touchHandler {touchHandler} {
114117
}
115118

@@ -404,7 +407,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
404407
ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
405408
break;
406409
case Apps::StopWatch:
407-
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask, dateTimeController);
410+
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask, dateTimeController, stopWatchController);
408411
break;
409412
case Apps::Twos:
410413
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 "Messages.h"
2122
#include "BootErrors.h"
@@ -36,6 +37,7 @@ namespace Pinetime {
3637
class HeartRateController;
3738
class MotionController;
3839
class TouchHandler;
40+
class StopWatchController;
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);
@@ -88,6 +91,7 @@ namespace Pinetime {
8891
Pinetime::Controllers::TimerController& timerController;
8992
Pinetime::Controllers::AlarmController& alarmController;
9093
Pinetime::Controllers::TouchHandler& touchHandler;
94+
Pinetime::Controllers::StopWatchController& stopWatchController;
9195

9296
Pinetime::Controllers::FirmwareValidator validator;
9397
Controllers::BrightnessController brightnessController;

src/displayapp/screens/StopWatch.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ static void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) {
4545
stopWatch->stopLapBtnEventHandler(event);
4646
}
4747

48-
StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController)
48+
StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController, Controllers::StopWatch& stopWatchController)
4949
: Screen(app),
5050
systemTask {systemTask},
5151
dateTimeController {dateTimeController},
52-
currentState {States::Init},
53-
startTime {},
54-
oldTimeElapsed {},
52+
stopWatchController {stopWatchController},
53+
timeElapsed {},
5554
currentTimeSeparated {},
5655
lapBuffer {},
5756
lapNr {} {
@@ -120,8 +119,8 @@ StopWatch::~StopWatch() {
120119
}
121120

122121
void StopWatch::reset() {
123-
currentState = States::Init;
124-
oldTimeElapsed = 0;
122+
stopWatchController.clear();
123+
125124
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
126125
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
127126

@@ -137,34 +136,34 @@ void StopWatch::reset() {
137136
}
138137

139138
void StopWatch::start() {
139+
stopWatchController.start(xTaskGetTickCount());
140+
140141
lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT);
141142
lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT);
142143
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
143144
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
144145
lv_label_set_text(txtPlayPause, Symbols::pause);
145146
lv_label_set_text(txtStopLap, Symbols::lapsFlag);
146-
startTime = xTaskGetTickCount();
147-
currentState = States::Running;
147+
148148
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
149149
}
150150

151151
void StopWatch::pause() {
152-
startTime = 0;
153-
// Store the current time elapsed in cache
154-
oldTimeElapsed += timeElapsed;
155-
currentState = States::Halted;
152+
stopWatchController.pause(xTaskGetTickCount());
153+
156154
lv_label_set_text(txtPlayPause, Symbols::play);
157155
lv_label_set_text(txtStopLap, Symbols::stop);
158156
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
159157
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
158+
160159
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
161160
}
162161

163162
void StopWatch::Refresh() {
164163
lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes());
165-
if (currentState == States::Running) {
166-
timeElapsed = calculateDelta(startTime, xTaskGetTickCount());
167-
currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed));
164+
if (stopWatchController.isRunning()) {
165+
timeElapsed = calculateDelta(stopWatchController.getStart(), xTaskGetTickCount());
166+
currentTimeSeparated = convertTicksToTimeSegments((stopWatchController.getElapsedPreviously() + timeElapsed));
168167

169168
lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs);
170169
lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths);
@@ -175,12 +174,10 @@ void StopWatch::playPauseBtnEventHandler(lv_event_t event) {
175174
if (event != LV_EVENT_CLICKED) {
176175
return;
177176
}
178-
if (currentState == States::Init) {
179-
start();
180-
} else if (currentState == States::Running) {
181-
pause();
182-
} else if (currentState == States::Halted) {
183-
start();
177+
if (stopWatchController.isCleared() || stopWatchController.isPaused()) {
178+
stopWatchController.start(xTaskGetTickCount());
179+
} else if (stopWatchController.isRunning()) {
180+
stopWatchController.pause(xTaskGetTickCount());
184181
}
185182
}
186183

@@ -189,7 +186,7 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) {
189186
return;
190187
}
191188
// If running, then this button is used to save laps
192-
if (currentState == States::Running) {
189+
if (stopWatchController.isRunning()) {
193190
lapBuffer.addLaps(currentTimeSeparated);
194191
lapNr++;
195192
if (lapBuffer[1]) {
@@ -199,14 +196,14 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) {
199196
if (lapBuffer[0]) {
200197
lv_label_set_text_fmt(lapTwoText, "#%2d %2d:%02d.%02d", lapNr, lapBuffer[0]->mins, lapBuffer[0]->secs, lapBuffer[0]->hundredths);
201198
}
202-
} else if (currentState == States::Halted) {
199+
} else if (stopWatchController.isPaused()) {
203200
reset();
204201
}
205202
}
206203

207204
bool StopWatch::OnButtonPushed() {
208-
if (currentState == States::Running) {
209-
pause();
205+
if (stopWatchController.isRunning()) {
206+
stopWatchController.pause(xTaskGetTickCount());
210207
return true;
211208
}
212209
return false;

src/displayapp/screens/StopWatch.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ namespace Pinetime::Applications::Screens {
6464
public:
6565
StopWatch(DisplayApp* app,
6666
System::SystemTask& systemTask,
67-
Controllers::DateTime& dateTimeController);
67+
Controllers::DateTime& dateTimeController,
68+
Controllers::StopWatch& stopWatchController);
6869
~StopWatch() override;
6970
void Refresh() override;
7071

@@ -79,17 +80,14 @@ namespace Pinetime::Applications::Screens {
7980
private:
8081
Pinetime::System::SystemTask& systemTask;
8182
Controllers::DateTime& dateTimeController;
83+
Controllers::StopWatch& stopWatchController;
8284

8385
TickType_t timeElapsed;
84-
States currentState;
85-
TickType_t startTime;
86-
TickType_t oldTimeElapsed;
8786
TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs
8887
LapTextBuffer_t<2> lapBuffer;
8988
int lapNr = 0;
90-
lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
89+
lv_obj_t *dateTime, *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
9190
lv_obj_t *lapOneText, *lapTwoText;
92-
lv_obj_t *dateTime;
9391

9492
lv_task_t* taskRefresh;
9593
};

src/systemtask/SystemTask.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
6666
Controllers::DateTime& dateTimeController,
6767
Controllers::TimerController& timerController,
6868
Controllers::AlarmController& alarmController,
69+
Controllers::StopWatch& stopWatchController,
6970
Drivers::Watchdog& watchdog,
7071
Pinetime::Controllers::NotificationManager& notificationManager,
7172
Pinetime::Controllers::MotorController& motorController,
@@ -89,6 +90,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
8990
dateTimeController {dateTimeController},
9091
timerController {timerController},
9192
alarmController {alarmController},
93+
stopWatchController {stopWatchController},
9294
watchdog {watchdog},
9395
notificationManager {notificationManager},
9496
motorController {motorController},

src/systemtask/SystemTask.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "components/motor/MotorController.h"
1919
#include "components/timer/TimerController.h"
2020
#include "components/alarm/AlarmController.h"
21+
#include "components/stopwatch/StopWatchController.h"
2122
#include "components/fs/FS.h"
2223
#include "touchhandler/TouchHandler.h"
2324

@@ -60,6 +61,7 @@ namespace Pinetime {
6061
Controllers::DateTime& dateTimeController,
6162
Controllers::TimerController& timerController,
6263
Controllers::AlarmController& alarmController,
64+
Controllers::StopWatch& stopWatchController,
6365
Drivers::Watchdog& watchdog,
6466
Pinetime::Controllers::NotificationManager& notificationManager,
6567
Pinetime::Controllers::MotorController& motorController,
@@ -118,6 +120,7 @@ namespace Pinetime {
118120
Pinetime::Controllers::Settings& settingsController;
119121
Pinetime::Controllers::HeartRateController& heartRateController;
120122
Pinetime::Controllers::MotionController& motionController;
123+
Pinetime::Controllers::StopWatch& stopWatchController;
121124

122125
Pinetime::Applications::DisplayApp& displayApp;
123126
Pinetime::Applications::HeartRateTask& heartRateApp;

0 commit comments

Comments
 (0)