@@ -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
107106StopWatch::~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+
111152bool 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
207163void 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
218176void 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}
0 commit comments