@@ -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
122121void 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
139138void 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
151151void 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
163162void 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
207204bool StopWatch::OnButtonPushed () {
208- if (currentState == States::Running ) {
209- pause ();
205+ if (stopWatchController. isRunning () ) {
206+ stopWatchController. pause (xTaskGetTickCount () );
210207 return true ;
211208 }
212209 return false ;
0 commit comments