Skip to content

Commit 61a4642

Browse files
authored
Improve stopwatch (#432)
* Improve stopwatch more * Make sure sleep gets reenabled * Cleanup and clang-format
1 parent ab59b9b commit 61a4642

3 files changed

Lines changed: 95 additions & 120 deletions

File tree

src/displayapp/DisplayApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
337337
ReturnApp(Apps::Clock, FullRefreshDirections::Down, TouchEvents::None);
338338
break;
339339
case Apps::StopWatch:
340-
currentScreen = std::make_unique<Screens::StopWatch>(this);
340+
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask);
341341
break;
342342
case Apps::Twos:
343343
currentScreen = std::make_unique<Screens::Twos>(this);

src/displayapp/screens/StopWatch.cpp

Lines changed: 84 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,16 @@ 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)
48+
StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask)
4949
: Screen(app),
50+
systemTask {systemTask},
5051
running {true},
5152
currentState {States::Init},
52-
currentEvent {Events::Stop},
5353
startTime {},
5454
oldTimeElapsed {},
5555
currentTimeSeparated {},
5656
lapBuffer {},
57-
lapNr {},
58-
lapPressed {false} {
57+
lapNr {} {
5958

6059
time = lv_label_create(lv_scr_act(), nullptr);
6160
lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
@@ -105,128 +104,100 @@ StopWatch::StopWatch(DisplayApp* app)
105104
}
106105

107106
StopWatch::~StopWatch() {
107+
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
108108
lv_obj_clean(lv_scr_act());
109109
}
110110

111+
void StopWatch::reset() {
112+
currentState = States::Init;
113+
oldTimeElapsed = 0;
114+
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
115+
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
116+
117+
lv_label_set_text(time, "00:00");
118+
lv_label_set_text(msecTime, "00");
119+
120+
lv_label_set_text(lapOneText, "");
121+
lv_label_set_text(lapTwoText, "");
122+
lapBuffer.clearBuffer();
123+
lapNr = 0;
124+
lv_obj_set_state(btnStopLap, LV_STATE_DISABLED);
125+
lv_obj_set_state(txtStopLap, LV_STATE_DISABLED);
126+
}
127+
128+
void StopWatch::start() {
129+
lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT);
130+
lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT);
131+
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
132+
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
133+
lv_label_set_text(txtPlayPause, Symbols::pause);
134+
lv_label_set_text(txtStopLap, Symbols::lapsFlag);
135+
startTime = xTaskGetTickCount();
136+
currentState = States::Running;
137+
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
138+
}
139+
140+
void StopWatch::pause() {
141+
startTime = 0;
142+
// Store the current time elapsed in cache
143+
oldTimeElapsed += timeElapsed;
144+
currentState = States::Halted;
145+
lv_label_set_text(txtPlayPause, Symbols::play);
146+
lv_label_set_text(txtStopLap, Symbols::stop);
147+
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
148+
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
149+
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
150+
}
151+
111152
bool StopWatch::Refresh() {
112-
// @startuml CHIP8_state
113-
// State "Init" as init
114-
// State "Running" as run
115-
// State "Halted" as halt
116-
117-
// [*] --> init
118-
// init -> run : press play
119-
// run -> run : press lap
120-
// run --> halt : press pause
121-
// halt --> run : press play
122-
// halt --> init : press stop
123-
// @enduml
124-
// Copy paste the above plantuml text to visualize the state diagram
125-
switch (currentState) {
126-
// Init state when an user first opens the app
127-
// and when a stop/reset button is pressed
128-
case States::Init: {
129-
// The initial default value
130-
lv_label_set_text(time, "00:00");
131-
lv_label_set_text(msecTime, "00");
132-
133-
lv_label_set_text(lapOneText, "");
134-
lv_label_set_text(lapTwoText, "");
135-
lapBuffer.clearBuffer();
136-
lapNr = 0;
137-
138-
if (currentEvent == Events::Play) {
139-
lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT);
140-
lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT);
141-
142-
startTime = xTaskGetTickCount();
143-
currentState = States::Running;
144-
} else {
145-
lv_obj_set_state(btnStopLap, LV_STATE_DISABLED);
146-
lv_obj_set_state(txtStopLap, LV_STATE_DISABLED);
147-
}
148-
break;
149-
}
150-
case States::Running: {
151-
lv_label_set_text(txtPlayPause, Symbols::pause);
152-
lv_label_set_text(txtStopLap, Symbols::lapsFlag);
153-
154-
const auto timeElapsed = calculateDelta(startTime, xTaskGetTickCount());
155-
currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed));
156-
157-
lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs);
158-
lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths);
159-
160-
if (lapPressed == true) {
161-
if (lapBuffer[1]) {
162-
lv_label_set_text_fmt(
163-
lapOneText, "#%2d %2d:%02d.%02d", (lapNr - 1), lapBuffer[1]->mins, lapBuffer[1]->secs, lapBuffer[1]->hundredths);
164-
}
165-
if (lapBuffer[0]) {
166-
lv_label_set_text_fmt(
167-
lapTwoText, "#%2d %2d:%02d.%02d", lapNr, lapBuffer[0]->mins, lapBuffer[0]->secs, lapBuffer[0]->hundredths);
168-
}
169-
// Reset the bool to avoid setting the text in each cycle until there is a change
170-
lapPressed = false;
171-
}
172-
173-
if (currentEvent == Events::Pause) {
174-
// Reset the start time
175-
startTime = 0;
176-
// Store the current time elapsed in cache
177-
oldTimeElapsed += timeElapsed;
178-
currentState = States::Halted;
179-
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
180-
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
181-
} else {
182-
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
183-
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
184-
}
185-
break;
186-
}
187-
case States::Halted: {
188-
lv_label_set_text(txtPlayPause, Symbols::play);
189-
lv_label_set_text(txtStopLap, Symbols::stop);
190-
191-
if (currentEvent == Events::Play) {
192-
startTime = xTaskGetTickCount();
193-
currentState = States::Running;
194-
}
195-
if (currentEvent == Events::Stop) {
196-
currentState = States::Init;
197-
oldTimeElapsed = 0;
198-
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
199-
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
200-
}
201-
break;
202-
}
153+
if (currentState == States::Running) {
154+
timeElapsed = calculateDelta(startTime, xTaskGetTickCount());
155+
currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed));
156+
157+
lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs);
158+
lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths);
203159
}
204160
return running;
205161
}
206162

207163
void StopWatch::playPauseBtnEventHandler(lv_event_t event) {
208-
if (event == LV_EVENT_CLICKED) {
209-
if (currentState == States::Init) {
210-
currentEvent = Events::Play;
211-
} else {
212-
// Simple Toggle for play/pause
213-
currentEvent = (currentEvent == Events::Play ? Events::Pause : Events::Play);
214-
}
164+
if (event != LV_EVENT_PRESSED) {
165+
return;
166+
}
167+
if (currentState == States::Init) {
168+
start();
169+
} else if (currentState == States::Running) {
170+
pause();
171+
} else if (currentState == States::Halted) {
172+
start();
215173
}
216174
}
217175

218176
void StopWatch::stopLapBtnEventHandler(lv_event_t event) {
219-
if (event == LV_EVENT_CLICKED) {
220-
// If running, then this button is used to save laps
221-
if (currentState == States::Running) {
222-
lapBuffer.addLaps(currentTimeSeparated);
223-
lapNr++;
224-
lapPressed = true;
225-
226-
} else if (currentState == States::Halted) {
227-
currentEvent = Events::Stop;
228-
} else {
229-
// Not possible to reach here. Do nothing.
177+
if (event != LV_EVENT_PRESSED) {
178+
return;
179+
}
180+
// If running, then this button is used to save laps
181+
if (currentState == States::Running) {
182+
lapBuffer.addLaps(currentTimeSeparated);
183+
lapNr++;
184+
if (lapBuffer[1]) {
185+
lv_label_set_text_fmt(
186+
lapOneText, "#%2d %2d:%02d.%02d", (lapNr - 1), lapBuffer[1]->mins, lapBuffer[1]->secs, lapBuffer[1]->hundredths);
187+
}
188+
if (lapBuffer[0]) {
189+
lv_label_set_text_fmt(lapTwoText, "#%2d %2d:%02d.%02d", lapNr, lapBuffer[0]->mins, lapBuffer[0]->secs, lapBuffer[0]->hundredths);
230190
}
191+
} else if (currentState == States::Halted) {
192+
reset();
193+
}
194+
}
195+
196+
bool StopWatch::OnButtonPushed() {
197+
if (currentState == States::Running) {
198+
pause();
199+
} else {
200+
running = false;
231201
}
202+
return true;
232203
}

src/displayapp/screens/StopWatch.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
#include "portmacro_cmsis.h"
99

1010
#include <array>
11+
#include "systemtask/SystemTask.h"
1112

1213
namespace Pinetime::Applications::Screens {
1314

1415
enum class States { Init, Running, Halted };
1516

16-
enum class Events { Play, Pause, Stop };
17-
1817
struct TimeSeparated_t {
1918
int mins;
2019
int secs;
@@ -63,23 +62,28 @@ namespace Pinetime::Applications::Screens {
6362

6463
class StopWatch : public Screen {
6564
public:
66-
StopWatch(DisplayApp* app);
65+
StopWatch(DisplayApp* app, System::SystemTask& systemTask);
6766
~StopWatch() override;
6867
bool Refresh() override;
6968

7069
void playPauseBtnEventHandler(lv_event_t event);
7170
void stopLapBtnEventHandler(lv_event_t event);
71+
bool OnButtonPushed() override;
72+
73+
void reset();
74+
void start();
75+
void pause();
7276

7377
private:
78+
Pinetime::System::SystemTask& systemTask;
79+
TickType_t timeElapsed;
7480
bool running;
7581
States currentState;
76-
Events currentEvent;
7782
TickType_t startTime;
7883
TickType_t oldTimeElapsed;
7984
TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs
8085
LapTextBuffer_t<2> lapBuffer;
81-
int lapNr;
82-
bool lapPressed;
86+
int lapNr = 0;
8387
lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
8488
lv_obj_t *lapOneText, *lapTwoText;
8589
};

0 commit comments

Comments
 (0)