Improve stopwatch (#432)
* Improve stopwatch more * Make sure sleep gets reenabled * Cleanup and clang-format
This commit is contained in:
parent
ab59b9b830
commit
61a4642221
|
@ -337,7 +337,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
|
||||||
ReturnApp(Apps::Clock, FullRefreshDirections::Down, TouchEvents::None);
|
ReturnApp(Apps::Clock, FullRefreshDirections::Down, TouchEvents::None);
|
||||||
break;
|
break;
|
||||||
case Apps::StopWatch:
|
case Apps::StopWatch:
|
||||||
currentScreen = std::make_unique<Screens::StopWatch>(this);
|
currentScreen = std::make_unique<Screens::StopWatch>(this, *systemTask);
|
||||||
break;
|
break;
|
||||||
case Apps::Twos:
|
case Apps::Twos:
|
||||||
currentScreen = std::make_unique<Screens::Twos>(this);
|
currentScreen = std::make_unique<Screens::Twos>(this);
|
||||||
|
|
|
@ -45,17 +45,16 @@ static void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) {
|
||||||
stopWatch->stopLapBtnEventHandler(event);
|
stopWatch->stopLapBtnEventHandler(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
StopWatch::StopWatch(DisplayApp* app)
|
StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask)
|
||||||
: Screen(app),
|
: Screen(app),
|
||||||
|
systemTask {systemTask},
|
||||||
running {true},
|
running {true},
|
||||||
currentState {States::Init},
|
currentState {States::Init},
|
||||||
currentEvent {Events::Stop},
|
|
||||||
startTime {},
|
startTime {},
|
||||||
oldTimeElapsed {},
|
oldTimeElapsed {},
|
||||||
currentTimeSeparated {},
|
currentTimeSeparated {},
|
||||||
lapBuffer {},
|
lapBuffer {},
|
||||||
lapNr {},
|
lapNr {} {
|
||||||
lapPressed {false} {
|
|
||||||
|
|
||||||
time = lv_label_create(lv_scr_act(), nullptr);
|
time = lv_label_create(lv_scr_act(), nullptr);
|
||||||
lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
StopWatch::~StopWatch() {
|
StopWatch::~StopWatch() {
|
||||||
|
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
|
||||||
lv_obj_clean(lv_scr_act());
|
lv_obj_clean(lv_scr_act());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StopWatch::reset() {
|
||||||
|
currentState = States::Init;
|
||||||
|
oldTimeElapsed = 0;
|
||||||
|
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
|
||||||
|
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
|
||||||
|
|
||||||
|
lv_label_set_text(time, "00:00");
|
||||||
|
lv_label_set_text(msecTime, "00");
|
||||||
|
|
||||||
|
lv_label_set_text(lapOneText, "");
|
||||||
|
lv_label_set_text(lapTwoText, "");
|
||||||
|
lapBuffer.clearBuffer();
|
||||||
|
lapNr = 0;
|
||||||
|
lv_obj_set_state(btnStopLap, LV_STATE_DISABLED);
|
||||||
|
lv_obj_set_state(txtStopLap, LV_STATE_DISABLED);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StopWatch::start() {
|
||||||
|
lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT);
|
||||||
|
lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT);
|
||||||
|
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
|
||||||
|
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
|
||||||
|
lv_label_set_text(txtPlayPause, Symbols::pause);
|
||||||
|
lv_label_set_text(txtStopLap, Symbols::lapsFlag);
|
||||||
|
startTime = xTaskGetTickCount();
|
||||||
|
currentState = States::Running;
|
||||||
|
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StopWatch::pause() {
|
||||||
|
startTime = 0;
|
||||||
|
// Store the current time elapsed in cache
|
||||||
|
oldTimeElapsed += timeElapsed;
|
||||||
|
currentState = States::Halted;
|
||||||
|
lv_label_set_text(txtPlayPause, Symbols::play);
|
||||||
|
lv_label_set_text(txtStopLap, Symbols::stop);
|
||||||
|
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
|
||||||
|
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
|
||||||
|
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
|
||||||
|
}
|
||||||
|
|
||||||
bool StopWatch::Refresh() {
|
bool StopWatch::Refresh() {
|
||||||
// @startuml CHIP8_state
|
if (currentState == States::Running) {
|
||||||
// State "Init" as init
|
timeElapsed = calculateDelta(startTime, xTaskGetTickCount());
|
||||||
// State "Running" as run
|
currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed));
|
||||||
// State "Halted" as halt
|
|
||||||
|
|
||||||
// [*] --> init
|
lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs);
|
||||||
// init -> run : press play
|
lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths);
|
||||||
// run -> run : press lap
|
|
||||||
// run --> halt : press pause
|
|
||||||
// halt --> run : press play
|
|
||||||
// halt --> init : press stop
|
|
||||||
// @enduml
|
|
||||||
// Copy paste the above plantuml text to visualize the state diagram
|
|
||||||
switch (currentState) {
|
|
||||||
// Init state when an user first opens the app
|
|
||||||
// and when a stop/reset button is pressed
|
|
||||||
case States::Init: {
|
|
||||||
// The initial default value
|
|
||||||
lv_label_set_text(time, "00:00");
|
|
||||||
lv_label_set_text(msecTime, "00");
|
|
||||||
|
|
||||||
lv_label_set_text(lapOneText, "");
|
|
||||||
lv_label_set_text(lapTwoText, "");
|
|
||||||
lapBuffer.clearBuffer();
|
|
||||||
lapNr = 0;
|
|
||||||
|
|
||||||
if (currentEvent == Events::Play) {
|
|
||||||
lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT);
|
|
||||||
lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT);
|
|
||||||
|
|
||||||
startTime = xTaskGetTickCount();
|
|
||||||
currentState = States::Running;
|
|
||||||
} else {
|
|
||||||
lv_obj_set_state(btnStopLap, LV_STATE_DISABLED);
|
|
||||||
lv_obj_set_state(txtStopLap, LV_STATE_DISABLED);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case States::Running: {
|
|
||||||
lv_label_set_text(txtPlayPause, Symbols::pause);
|
|
||||||
lv_label_set_text(txtStopLap, Symbols::lapsFlag);
|
|
||||||
|
|
||||||
const auto timeElapsed = calculateDelta(startTime, xTaskGetTickCount());
|
|
||||||
currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed));
|
|
||||||
|
|
||||||
lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs);
|
|
||||||
lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths);
|
|
||||||
|
|
||||||
if (lapPressed == true) {
|
|
||||||
if (lapBuffer[1]) {
|
|
||||||
lv_label_set_text_fmt(
|
|
||||||
lapOneText, "#%2d %2d:%02d.%02d", (lapNr - 1), lapBuffer[1]->mins, lapBuffer[1]->secs, lapBuffer[1]->hundredths);
|
|
||||||
}
|
|
||||||
if (lapBuffer[0]) {
|
|
||||||
lv_label_set_text_fmt(
|
|
||||||
lapTwoText, "#%2d %2d:%02d.%02d", lapNr, lapBuffer[0]->mins, lapBuffer[0]->secs, lapBuffer[0]->hundredths);
|
|
||||||
}
|
|
||||||
// Reset the bool to avoid setting the text in each cycle until there is a change
|
|
||||||
lapPressed = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentEvent == Events::Pause) {
|
|
||||||
// Reset the start time
|
|
||||||
startTime = 0;
|
|
||||||
// Store the current time elapsed in cache
|
|
||||||
oldTimeElapsed += timeElapsed;
|
|
||||||
currentState = States::Halted;
|
|
||||||
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
|
|
||||||
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
|
|
||||||
} else {
|
|
||||||
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
|
|
||||||
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case States::Halted: {
|
|
||||||
lv_label_set_text(txtPlayPause, Symbols::play);
|
|
||||||
lv_label_set_text(txtStopLap, Symbols::stop);
|
|
||||||
|
|
||||||
if (currentEvent == Events::Play) {
|
|
||||||
startTime = xTaskGetTickCount();
|
|
||||||
currentState = States::Running;
|
|
||||||
}
|
|
||||||
if (currentEvent == Events::Stop) {
|
|
||||||
currentState = States::Init;
|
|
||||||
oldTimeElapsed = 0;
|
|
||||||
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
|
|
||||||
lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StopWatch::playPauseBtnEventHandler(lv_event_t event) {
|
void StopWatch::playPauseBtnEventHandler(lv_event_t event) {
|
||||||
if (event == LV_EVENT_CLICKED) {
|
if (event != LV_EVENT_PRESSED) {
|
||||||
if (currentState == States::Init) {
|
return;
|
||||||
currentEvent = Events::Play;
|
}
|
||||||
} else {
|
if (currentState == States::Init) {
|
||||||
// Simple Toggle for play/pause
|
start();
|
||||||
currentEvent = (currentEvent == Events::Play ? Events::Pause : Events::Play);
|
} else if (currentState == States::Running) {
|
||||||
}
|
pause();
|
||||||
|
} else if (currentState == States::Halted) {
|
||||||
|
start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StopWatch::stopLapBtnEventHandler(lv_event_t event) {
|
void StopWatch::stopLapBtnEventHandler(lv_event_t event) {
|
||||||
if (event == LV_EVENT_CLICKED) {
|
if (event != LV_EVENT_PRESSED) {
|
||||||
// If running, then this button is used to save laps
|
return;
|
||||||
if (currentState == States::Running) {
|
}
|
||||||
lapBuffer.addLaps(currentTimeSeparated);
|
// If running, then this button is used to save laps
|
||||||
lapNr++;
|
if (currentState == States::Running) {
|
||||||
lapPressed = true;
|
lapBuffer.addLaps(currentTimeSeparated);
|
||||||
|
lapNr++;
|
||||||
} else if (currentState == States::Halted) {
|
if (lapBuffer[1]) {
|
||||||
currentEvent = Events::Stop;
|
lv_label_set_text_fmt(
|
||||||
} else {
|
lapOneText, "#%2d %2d:%02d.%02d", (lapNr - 1), lapBuffer[1]->mins, lapBuffer[1]->secs, lapBuffer[1]->hundredths);
|
||||||
// Not possible to reach here. Do nothing.
|
|
||||||
}
|
}
|
||||||
|
if (lapBuffer[0]) {
|
||||||
|
lv_label_set_text_fmt(lapTwoText, "#%2d %2d:%02d.%02d", lapNr, lapBuffer[0]->mins, lapBuffer[0]->secs, lapBuffer[0]->hundredths);
|
||||||
|
}
|
||||||
|
} else if (currentState == States::Halted) {
|
||||||
|
reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool StopWatch::OnButtonPushed() {
|
||||||
|
if (currentState == States::Running) {
|
||||||
|
pause();
|
||||||
|
} else {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -8,13 +8,12 @@
|
||||||
#include "portmacro_cmsis.h"
|
#include "portmacro_cmsis.h"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include "systemtask/SystemTask.h"
|
||||||
|
|
||||||
namespace Pinetime::Applications::Screens {
|
namespace Pinetime::Applications::Screens {
|
||||||
|
|
||||||
enum class States { Init, Running, Halted };
|
enum class States { Init, Running, Halted };
|
||||||
|
|
||||||
enum class Events { Play, Pause, Stop };
|
|
||||||
|
|
||||||
struct TimeSeparated_t {
|
struct TimeSeparated_t {
|
||||||
int mins;
|
int mins;
|
||||||
int secs;
|
int secs;
|
||||||
|
@ -63,23 +62,28 @@ namespace Pinetime::Applications::Screens {
|
||||||
|
|
||||||
class StopWatch : public Screen {
|
class StopWatch : public Screen {
|
||||||
public:
|
public:
|
||||||
StopWatch(DisplayApp* app);
|
StopWatch(DisplayApp* app, System::SystemTask& systemTask);
|
||||||
~StopWatch() override;
|
~StopWatch() override;
|
||||||
bool Refresh() override;
|
bool Refresh() override;
|
||||||
|
|
||||||
void playPauseBtnEventHandler(lv_event_t event);
|
void playPauseBtnEventHandler(lv_event_t event);
|
||||||
void stopLapBtnEventHandler(lv_event_t event);
|
void stopLapBtnEventHandler(lv_event_t event);
|
||||||
|
bool OnButtonPushed() override;
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
void start();
|
||||||
|
void pause();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Pinetime::System::SystemTask& systemTask;
|
||||||
|
TickType_t timeElapsed;
|
||||||
bool running;
|
bool running;
|
||||||
States currentState;
|
States currentState;
|
||||||
Events currentEvent;
|
|
||||||
TickType_t startTime;
|
TickType_t startTime;
|
||||||
TickType_t oldTimeElapsed;
|
TickType_t oldTimeElapsed;
|
||||||
TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs
|
TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs
|
||||||
LapTextBuffer_t<2> lapBuffer;
|
LapTextBuffer_t<2> lapBuffer;
|
||||||
int lapNr;
|
int lapNr = 0;
|
||||||
bool lapPressed;
|
|
||||||
lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
|
lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
|
||||||
lv_obj_t *lapOneText, *lapTwoText;
|
lv_obj_t *lapOneText, *lapTwoText;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue