diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e2b69b8b02..25318d16bc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -466,6 +466,7 @@ list(APPEND SOURCE_FILES components/motor/MotorController.cpp components/settings/Settings.cpp components/timer/Timer.cpp + components/stopwatch/StopWatchController.cpp components/alarm/AlarmController.cpp components/fs/FS.cpp drivers/Cst816s.cpp @@ -535,6 +536,7 @@ list(APPEND RECOVERY_SOURCE_FILES components/firmwarevalidator/FirmwareValidator.cpp components/settings/Settings.cpp components/timer/Timer.cpp + components/stopwatch/StopWatchController.cpp components/alarm/AlarmController.cpp drivers/Cst816s.cpp FreeRTOS/port.c @@ -654,6 +656,7 @@ set(INCLUDE_FILES components/ble/SimpleWeatherService.h components/settings/Settings.h components/timer/Timer.h + components/stopwatch/StopWatchController.h components/alarm/AlarmController.h drivers/Cst816s.h FreeRTOS/portmacro.h diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp new file mode 100644 index 0000000000..d21f650817 --- /dev/null +++ b/src/components/stopwatch/StopWatchController.cpp @@ -0,0 +1,72 @@ +#include "components/stopwatch/StopWatchController.h" + +using namespace Pinetime::Controllers; + +StopWatchController::StopWatchController() { + Clear(); +} + +// State Change + +void StopWatchController::Start() { + currentState = StopWatchStates::Running; + startTime = xTaskGetTickCount(); +} + +void StopWatchController::Pause() { + timeElapsedPreviously = GetElapsedTime(); + currentState = StopWatchStates::Paused; +} + +void StopWatchController::Clear() { + currentState = StopWatchStates::Cleared; + timeElapsedPreviously = 0; + + for (uint8_t i = 0; i < histSize; i++) { + history[i].number = 0; + history[i].timeSinceStart = 0; + } + maxLapNumber = 0; +} + +// Lap + +void StopWatchController::AddLapToHistory() { + TickType_t lapEnd = GetElapsedTime(); + history--; + history[0].timeSinceStart = lapEnd; + history[0].number = ++maxLapNumber % lapNumberBoundary; +} + +uint16_t StopWatchController::GetMaxLapNumber() { + return maxLapNumber; +} + +std::optional StopWatchController::GetLapFromHistory(uint8_t index) { + if (index >= histSize || history[index].number == 0) { + return {}; + } + return history[index]; +} + +// Data / State acess + +TickType_t StopWatchController::GetElapsedTime() { + if (!IsRunning()) { + return timeElapsedPreviously; + } + TickType_t delta = xTaskGetTickCount() - startTime; + return (timeElapsedPreviously + delta) % elapsedTimeBoundary; +} + +bool StopWatchController::IsRunning() { + return currentState == StopWatchStates::Running; +} + +bool StopWatchController::IsCleared() { + return currentState == StopWatchStates::Cleared; +} + +bool StopWatchController::IsPaused() { + return currentState == StopWatchStates::Paused; +} diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h new file mode 100644 index 0000000000..5c52cf5ea5 --- /dev/null +++ b/src/components/stopwatch/StopWatchController.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include +#include "utility/CircularBuffer.h" + +namespace Pinetime { + namespace System { + class SystemTask; + } + + namespace Controllers { + + enum class StopWatchStates { Cleared, Running, Paused }; + + struct LapInfo { + uint16_t number = 0; // Used to label the lap + TickType_t timeSinceStart = 0; // Excluding pauses + }; + + class StopWatchController { + public: + StopWatchController(); + + // StopWatch functionality and data + void Start(); + void Pause(); + void Clear(); + + TickType_t GetElapsedTime(); + + // Lap functionality + + /// Only the latest histSize laps are stored + void AddLapToHistory(); + + /// Returns maxLapNumber + uint16_t GetMaxLapNumber(); + + /// Indexes into lap history, with 0 being the latest lap. + std::optional GetLapFromHistory(uint8_t index); + + bool IsRunning(); + bool IsCleared(); + bool IsPaused(); + + private: + // Time at which stopwatch wraps around to zero (1000 hours) + static constexpr TickType_t elapsedTimeBoundary = static_cast(configTICK_RATE_HZ) * 60 * 60 * 1000; + // Current state of stopwatch + StopWatchStates currentState = StopWatchStates::Cleared; + // Start time of current duration + TickType_t startTime; + // How much time was elapsed before current duration + TickType_t timeElapsedPreviously; + + // Maximum number of stored laps + static constexpr uint8_t histSize = 4; + // Value at which lap numbers wrap around to zero + static constexpr uint16_t lapNumberBoundary = 1000; + // Lap storage + Utility::CircularBuffer history; + // Highest lap number; less than lapNumberBoundary, may exceed histSize + uint16_t maxLapNumber; + }; + } +} diff --git a/src/displayapp/Controllers.h b/src/displayapp/Controllers.h index 9992426c5d..223c7c699e 100644 --- a/src/displayapp/Controllers.h +++ b/src/displayapp/Controllers.h @@ -18,6 +18,7 @@ namespace Pinetime { class Settings; class MotorController; class MotionController; + class StopWatchController; class AlarmController; class BrightnessController; class SimpleWeatherService; @@ -41,6 +42,7 @@ namespace Pinetime { Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::MotorController& motorController; Pinetime::Controllers::MotionController& motionController; + Pinetime::Controllers::StopWatchController& stopWatchController; Pinetime::Controllers::AlarmController& alarmController; Pinetime::Controllers::BrightnessController& brightnessController; Pinetime::Controllers::SimpleWeatherService* weatherController; diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index b1594f197c..a709b9edaf 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -78,6 +78,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Controllers::Settings& settingsController, Pinetime::Controllers::MotorController& motorController, Pinetime::Controllers::MotionController& motionController, + Pinetime::Controllers::StopWatchController& stopWatchController, Pinetime::Controllers::AlarmController& alarmController, Pinetime::Controllers::BrightnessController& brightnessController, Pinetime::Controllers::TouchHandler& touchHandler, @@ -94,6 +95,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, settingsController {settingsController}, motorController {motorController}, motionController {motionController}, + stopWatchController {stopWatchController}, alarmController {alarmController}, brightnessController {brightnessController}, touchHandler {touchHandler}, @@ -109,6 +111,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, settingsController, motorController, motionController, + stopWatchController, alarmController, brightnessController, nullptr, diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 2f276eaf9e..33dec1de88 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -13,6 +13,7 @@ #include "components/settings/Settings.h" #include "displayapp/screens/Screen.h" #include "components/timer/Timer.h" +#include "components/stopwatch/StopWatchController.h" #include "components/alarm/AlarmController.h" #include "touchhandler/TouchHandler.h" @@ -63,6 +64,7 @@ namespace Pinetime { Controllers::Settings& settingsController, Pinetime::Controllers::MotorController& motorController, Pinetime::Controllers::MotionController& motionController, + Pinetime::Controllers::StopWatchController& stopWatchController, Pinetime::Controllers::AlarmController& alarmController, Pinetime::Controllers::BrightnessController& brightnessController, Pinetime::Controllers::TouchHandler& touchHandler, @@ -93,6 +95,7 @@ namespace Pinetime { Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::MotorController& motorController; Pinetime::Controllers::MotionController& motionController; + Pinetime::Controllers::StopWatchController& stopWatchController; Pinetime::Controllers::AlarmController& alarmController; Pinetime::Controllers::BrightnessController& brightnessController; Pinetime::Controllers::TouchHandler& touchHandler; diff --git a/src/displayapp/DisplayAppRecovery.cpp b/src/displayapp/DisplayAppRecovery.cpp index bcb8db0e9d..cb358d601e 100644 --- a/src/displayapp/DisplayAppRecovery.cpp +++ b/src/displayapp/DisplayAppRecovery.cpp @@ -21,6 +21,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Controllers::Settings& /*settingsController*/, Pinetime::Controllers::MotorController& /*motorController*/, Pinetime::Controllers::MotionController& /*motionController*/, + Pinetime::Controllers::StopWatchController& /*stopWatchController*/, Pinetime::Controllers::AlarmController& /*alarmController*/, Pinetime::Controllers::BrightnessController& /*brightnessController*/, Pinetime::Controllers::TouchHandler& /*touchHandler*/, diff --git a/src/displayapp/DisplayAppRecovery.h b/src/displayapp/DisplayAppRecovery.h index 162ff2575e..ed387a8b08 100644 --- a/src/displayapp/DisplayAppRecovery.h +++ b/src/displayapp/DisplayAppRecovery.h @@ -31,6 +31,7 @@ namespace Pinetime { class MotionController; class TouchHandler; class MotorController; + class StopWatchController; class AlarmController; class BrightnessController; class FS; @@ -57,6 +58,7 @@ namespace Pinetime { Controllers::Settings& settingsController, Pinetime::Controllers::MotorController& motorController, Pinetime::Controllers::MotionController& motionController, + Pinetime::Controllers::StopWatchController& stopWatchController, Pinetime::Controllers::AlarmController& alarmController, Pinetime::Controllers::BrightnessController& brightnessController, Pinetime::Controllers::TouchHandler& touchHandler, diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index ff852beb69..026260cbfa 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -4,49 +4,51 @@ #include "displayapp/InfiniTimeTheme.h" using namespace Pinetime::Applications::Screens; +using namespace Pinetime::Controllers; namespace { - TimeSeparated_t convertTicksToTimeSegments(const TickType_t timeElapsed) { - // Centiseconds - const int timeElapsedCentis = timeElapsed * 100 / configTICK_RATE_HZ; - - const int hundredths = (timeElapsedCentis % 100); - const int secs = (timeElapsedCentis / 100) % 60; - const int mins = ((timeElapsedCentis / 100) / 60) % 60; - const int hours = ((timeElapsedCentis / 100) / 60) / 60; - return TimeSeparated_t {hours, mins, secs, hundredths}; + TimeSeparated ConvertTicksToTimeSegments(const TickType_t timeElapsed) { + const uint32_t timeElapsedSecs = timeElapsed / configTICK_RATE_HZ; + const uint16_t timeElapsedFraction = timeElapsed % configTICK_RATE_HZ; + + const uint8_t hundredths = timeElapsedFraction * 100 / configTICK_RATE_HZ; + const uint8_t secs = (timeElapsedSecs) % 60; + const uint8_t mins = (timeElapsedSecs / 60) % 60; + const uint16_t hours = (timeElapsedSecs / 60) / 60; + return TimeSeparated {hours, mins, secs, hundredths, timeElapsedSecs}; } - void play_pause_event_handler(lv_obj_t* obj, lv_event_t event) { + void PlayPauseEventHandler(lv_obj_t* obj, lv_event_t event) { auto* stopWatch = static_cast(obj->user_data); if (event == LV_EVENT_CLICKED) { - stopWatch->playPauseBtnEventHandler(); + stopWatch->PlayPauseBtnEventHandler(); } } - void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) { + void StopLapEventHandler(lv_obj_t* obj, lv_event_t event) { auto* stopWatch = static_cast(obj->user_data); if (event == LV_EVENT_CLICKED) { - stopWatch->stopLapBtnEventHandler(); + stopWatch->StopLapBtnEventHandler(); } } constexpr TickType_t blinkInterval = pdMS_TO_TICKS(1000); } -StopWatch::StopWatch(System::SystemTask& systemTask) : wakeLock(systemTask) { +StopWatch::StopWatch(System::SystemTask& systemTask, StopWatchController& stopWatchController) + : wakeLock(systemTask), stopWatchController {stopWatchController} { static constexpr uint8_t btnWidth = 115; static constexpr uint8_t btnHeight = 80; btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); btnPlayPause->user_data = this; - lv_obj_set_event_cb(btnPlayPause, play_pause_event_handler); + lv_obj_set_event_cb(btnPlayPause, PlayPauseEventHandler); lv_obj_set_size(btnPlayPause, btnWidth, btnHeight); lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0); txtPlayPause = lv_label_create(btnPlayPause, nullptr); btnStopLap = lv_btn_create(lv_scr_act(), nullptr); btnStopLap->user_data = this; - lv_obj_set_event_cb(btnStopLap, stop_lap_event_handler); + lv_obj_set_event_cb(btnStopLap, StopLapEventHandler); lv_obj_set_size(btnStopLap, btnWidth, btnHeight); lv_obj_align(btnStopLap, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 0, 0); txtStopLap = lv_label_create(btnStopLap, nullptr); @@ -55,26 +57,48 @@ StopWatch::StopWatch(System::SystemTask& systemTask) : wakeLock(systemTask) { lapText = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lapText, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray); - lv_label_set_text_static(lapText, "\n"); + lv_label_set_text_static(lapText, ""); lv_label_set_long_mode(lapText, LV_LABEL_LONG_BREAK); lv_label_set_align(lapText, LV_LABEL_ALIGN_CENTER); lv_obj_set_width(lapText, LV_HOR_RES_MAX); - lv_obj_align(lapText, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, -btnHeight); - - msecTime = lv_label_create(lv_scr_act(), nullptr); - lv_label_set_text_static(msecTime, "00"); - lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DISABLED, Colors::lightGray); - lv_obj_align(msecTime, lapText, LV_ALIGN_OUT_TOP_MID, 0, 0); + lv_obj_align(lapText, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, -btnHeight - 2); time = lv_label_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DISABLED, Colors::lightGray); lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); lv_label_set_text_static(time, "00:00"); - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DISABLED, Colors::lightGray); - lv_obj_align(time, msecTime, LV_ALIGN_OUT_TOP_MID, 0, 0); + lv_label_set_long_mode(time, LV_LABEL_LONG_CROP); + lv_label_set_align(time, LV_LABEL_ALIGN_CENTER); + lv_obj_set_width(time, LV_HOR_RES_MAX); + lv_obj_align(time, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 0); - SetInterfaceStopped(); + msecTime = lv_label_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DISABLED, Colors::lightGray); + lv_label_set_text_static(msecTime, "00"); + lv_obj_align(msecTime, time, LV_ALIGN_OUT_BOTTOM_MID, 0, -2); taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); + + // Figure out what the current state of the stopwatch is and select the correct display + if (stopWatchController.IsCleared()) { + DisplayCleared(); + } else { + if (stopWatchController.GetMaxLapNumber() > 0) { + RenderLaps(); + } + RenderTime(); + + if (stopWatchController.IsRunning()) { + lv_obj_set_state(btnStopLap, LV_STATE_DISABLED); + lv_obj_set_state(txtStopLap, LV_STATE_DISABLED); + DisplayStarted(); + wakeLock.Lock(); + } else if (stopWatchController.IsPaused()) { + lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); + lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); + DisplayPaused(); + } + } } StopWatch::~StopWatch() { @@ -82,14 +106,14 @@ StopWatch::~StopWatch() { lv_obj_clean(lv_scr_act()); } -void StopWatch::SetInterfacePaused() { +void StopWatch::DisplayPaused() { lv_obj_set_style_local_bg_color(btnStopLap, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::blue); lv_label_set_text_static(txtPlayPause, Symbols::play); lv_label_set_text_static(txtStopLap, Symbols::stop); } -void StopWatch::SetInterfaceRunning() { +void StopWatch::DisplayStarted() { lv_obj_set_state(time, LV_STATE_DEFAULT); lv_obj_set_state(msecTime, LV_STATE_DEFAULT); lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt); @@ -102,7 +126,7 @@ void StopWatch::SetInterfaceRunning() { lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); } -void StopWatch::SetInterfaceStopped() { +void StopWatch::DisplayCleared() { lv_obj_set_state(time, LV_STATE_DISABLED); lv_obj_set_state(msecTime, LV_STATE_DISABLED); lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::blue); @@ -110,11 +134,7 @@ void StopWatch::SetInterfaceStopped() { lv_label_set_text_static(time, "00:00"); lv_label_set_text_static(msecTime, "00"); - if (isHoursLabelUpdated) { - lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); - lv_obj_realign(time); - isHoursLabelUpdated = false; - } + SetHoursVisible(false); lv_label_set_text_static(lapText, ""); lv_label_set_text_static(txtPlayPause, Symbols::play); @@ -123,97 +143,123 @@ void StopWatch::SetInterfaceStopped() { lv_obj_set_state(txtStopLap, LV_STATE_DISABLED); } -void StopWatch::Reset() { - SetInterfaceStopped(); - currentState = States::Init; - oldTimeElapsed = 0; - lapsDone = 0; -} - -void StopWatch::Start() { - SetInterfaceRunning(); - startTime = xTaskGetTickCount(); - currentState = States::Running; - wakeLock.Lock(); +void StopWatch::RenderTime() { + TimeSeparated elapsedTime = ConvertTicksToTimeSegments(stopWatchController.GetElapsedTime()); + renderedSeconds = elapsedTime.epochSecs; + if (renderedSeconds.IsUpdated()) { + SetHoursVisible(elapsedTime.hours != 0); + if (!hoursVisible) { + lv_label_set_text_fmt(time, "%02d:%02d", elapsedTime.mins, elapsedTime.secs); + } else { + lv_label_set_text_fmt(time, "%02d:%02d:%02d", elapsedTime.hours, elapsedTime.mins, elapsedTime.secs); + } + } + lv_label_set_text_fmt(msecTime, "%02d", elapsedTime.hundredths); } -void StopWatch::Pause() { - SetInterfacePaused(); - startTime = 0; - // Store the current time elapsed in cache - oldTimeElapsed = laps[lapsDone]; - blinkTime = xTaskGetTickCount() + blinkInterval; - currentState = States::Halted; - wakeLock.Release(); +void StopWatch::RenderPause() { + const TickType_t currentTime = xTaskGetTickCount(); + if (currentTime > blinkTime) { + blinkTime = currentTime + blinkInterval; + if (lv_obj_get_state(time, LV_LABEL_PART_MAIN) == LV_STATE_DEFAULT) { + lv_obj_set_state(time, LV_STATE_DISABLED); + lv_obj_set_state(msecTime, LV_STATE_DISABLED); + } else { + lv_obj_set_state(time, LV_STATE_DEFAULT); + lv_obj_set_state(msecTime, LV_STATE_DEFAULT); + } + } } -void StopWatch::Refresh() { - if (currentState == States::Running) { - laps[lapsDone] = oldTimeElapsed + xTaskGetTickCount() - startTime; +void StopWatch::RenderLaps() { + lv_label_set_text(lapText, ""); + for (int i = displayedLaps - 1; i >= 0; i--) { + std::optional lap = stopWatchController.GetLapFromHistory(i); - TimeSeparated_t currentTimeSeparated = convertTicksToTimeSegments(laps[lapsDone]); - if (currentTimeSeparated.hours == 0) { - lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); - } else { - lv_label_set_text_fmt(time, "%02d:%02d:%02d", currentTimeSeparated.hours, currentTimeSeparated.mins, currentTimeSeparated.secs); - if (!isHoursLabelUpdated) { - lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); - lv_obj_realign(time); - isHoursLabelUpdated = true; - } - } - lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); - } else if (currentState == States::Halted) { - const TickType_t currentTime = xTaskGetTickCount(); - if (currentTime > blinkTime) { - blinkTime = currentTime + blinkInterval; - if (lv_obj_get_state(time, LV_LABEL_PART_MAIN) == LV_STATE_DEFAULT) { - lv_obj_set_state(time, LV_STATE_DISABLED); - lv_obj_set_state(msecTime, LV_STATE_DISABLED); + if (lap) { + TimeSeparated laptime = ConvertTicksToTimeSegments(lap->timeSinceStart); + char buffer[19]; + if (laptime.hours > 0) { + snprintf(buffer, sizeof(buffer), "\n#%-3d %3d:%02d:%02d.%02d", + lap->number, + laptime.hours, + laptime.mins, + laptime.secs, + laptime.hundredths); + } else if (laptime.mins > 0) { + snprintf(buffer, sizeof(buffer), "\n#%-3d %2d:%02d.%02d", + lap->number, + laptime.mins, + laptime.secs, + laptime.hundredths); } else { - lv_obj_set_state(time, LV_STATE_DEFAULT); - lv_obj_set_state(msecTime, LV_STATE_DEFAULT); + snprintf(buffer, sizeof(buffer), "\n#%-3d %2d.%02d", + lap->number, + laptime.secs, + laptime.hundredths); } + lv_label_ins_text(lapText, LV_LABEL_POS_LAST, buffer); } } + lv_obj_realign(lapText); } -void StopWatch::playPauseBtnEventHandler() { - if (currentState == States::Init || currentState == States::Halted) { - Start(); - } else if (currentState == States::Running) { - Pause(); +void StopWatch::SetHoursVisible(bool visible) { + if (hoursVisible != visible) { + lv_font_t* font = visible ? &jetbrains_mono_42 : &jetbrains_mono_76; + lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font); + lv_obj_set_height(time, font->line_height); + lv_obj_align(time, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, visible ? 5 : 0); + lv_obj_align(msecTime, time, LV_ALIGN_OUT_BOTTOM_MID, 0, visible ? 5 : -2); + displayedLaps = visible ? 4 : 3; + hoursVisible = visible; + if (stopWatchController.GetLapFromHistory(0)) { + RenderLaps(); + } } } -void StopWatch::stopLapBtnEventHandler() { - // If running, then this button is used to save laps - if (currentState == States::Running) { - lv_label_set_text(lapText, ""); - lapsDone = std::min(lapsDone + 1, maxLapCount); - for (int i = lapsDone - displayedLaps; i < lapsDone; i++) { - if (i < 0) { - lv_label_ins_text(lapText, LV_LABEL_POS_LAST, "\n"); - continue; - } - TimeSeparated_t times = convertTicksToTimeSegments(laps[i]); - char buffer[17]; - if (times.hours == 0) { - snprintf(buffer, sizeof(buffer), "#%2d %2d:%02d.%02d\n", i + 1, times.mins, times.secs, times.hundredths); - } else { - snprintf(buffer, sizeof(buffer), "#%2d %2d:%02d:%02d.%02d\n", i + 1, times.hours, times.mins, times.secs, times.hundredths); - } - lv_label_ins_text(lapText, LV_LABEL_POS_LAST, buffer); - } - } else if (currentState == States::Halted) { - Reset(); +void StopWatch::Refresh() { + if (stopWatchController.IsRunning()) { + RenderTime(); + } else if (stopWatchController.IsPaused()) { + RenderPause(); + } +} + +void StopWatch::PlayPauseBtnEventHandler() { + if (stopWatchController.IsCleared() || stopWatchController.IsPaused()) { + stopWatchController.Start(); + DisplayStarted(); + wakeLock.Lock(); + } else if (stopWatchController.IsRunning()) { + OnPause(); + } +} + +void StopWatch::StopLapBtnEventHandler() { + if (stopWatchController.IsRunning()) { + stopWatchController.AddLapToHistory(); + RenderLaps(); + } else if (stopWatchController.IsPaused()) { + stopWatchController.Clear(); + DisplayCleared(); + wakeLock.Release(); } } bool StopWatch::OnButtonPushed() { - if (currentState == States::Running) { - Pause(); + if (stopWatchController.IsRunning()) { + OnPause(); return true; } return false; } + +void StopWatch::OnPause() { + stopWatchController.Pause(); + blinkTime = xTaskGetTickCount() + blinkInterval; + RenderTime(); // make sure displayed time is not stale + DisplayPaused(); + wakeLock.Release(); +} diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index 55a178dcbe..2d8d67aec1 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -3,72 +3,66 @@ #include "displayapp/screens/Screen.h" #include -#include -#include "portmacro_cmsis.h" - +#include "components/stopwatch/StopWatchController.h" #include "systemtask/SystemTask.h" #include "systemtask/WakeLock.h" -#include "displayapp/apps/Apps.h" -#include "displayapp/Controllers.h" #include "Symbols.h" +#include "utility/DirtyValue.h" -namespace Pinetime { - namespace Applications { - namespace Screens { - - enum class States { Init, Running, Halted }; +namespace Pinetime::Applications { + namespace Screens { - struct TimeSeparated_t { - int hours; - int mins; - int secs; - int hundredths; - }; + struct TimeSeparated { + uint16_t hours; + uint8_t mins; + uint8_t secs; + uint8_t hundredths; + uint32_t epochSecs; + }; - class StopWatch : public Screen { - public: - explicit StopWatch(System::SystemTask& systemTask); - ~StopWatch() override; - void Refresh() override; + class StopWatch : public Screen { + public: + explicit StopWatch(System::SystemTask& systemTask, Controllers::StopWatchController& stopWatchController); + ~StopWatch() override; + void Refresh() override; - void playPauseBtnEventHandler(); - void stopLapBtnEventHandler(); - bool OnButtonPushed() override; + void PlayPauseBtnEventHandler(); + void StopLapBtnEventHandler(); + bool OnButtonPushed() override; - private: - void SetInterfacePaused(); - void SetInterfaceRunning(); - void SetInterfaceStopped(); + private: + void OnPause(); - void Reset(); - void Start(); - void Pause(); + void DisplayPaused(); + void DisplayStarted(); + void DisplayCleared(); - Pinetime::System::WakeLock wakeLock; - States currentState = States::Init; - TickType_t startTime; - TickType_t oldTimeElapsed = 0; - TickType_t blinkTime = 0; - static constexpr int maxLapCount = 20; - TickType_t laps[maxLapCount + 1]; - static constexpr int displayedLaps = 2; - int lapsDone = 0; - lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; - lv_obj_t* lapText; - bool isHoursLabelUpdated = false; + void RenderTime(); + void RenderPause(); + void RenderLaps(); - lv_task_t* taskRefresh; - }; - } + void SetHoursVisible(bool visible); - template <> - struct AppTraits { - static constexpr Apps app = Apps::StopWatch; - static constexpr const char* icon = Screens::Symbols::stopWatch; + Pinetime::System::WakeLock wakeLock; + Controllers::StopWatchController& stopWatchController; + TickType_t blinkTime = 0; + uint8_t displayedLaps = 3; + lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; + lv_obj_t* lapText; + Utility::DirtyValue renderedSeconds; + bool hoursVisible = false; - static Screens::Screen* Create(AppControllers& controllers) { - return new Screens::StopWatch(*controllers.systemTask); - }; + lv_task_t* taskRefresh; }; } + + template <> + struct AppTraits { + static constexpr Apps app = Apps::StopWatch; + static constexpr const char* icon = Screens::Symbols::stopWatch; + + static Screens::Screen* Create(AppControllers& controllers) { + return new Screens::StopWatch(*controllers.systemTask, controllers.stopWatchController); + }; + }; } diff --git a/src/main.cpp b/src/main.cpp index 24f13caddd..c573b482f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,7 @@ #include "components/motor/MotorController.h" #include "components/datetime/DateTimeController.h" #include "components/heartrate/HeartRateController.h" +#include "components/stopwatch/StopWatchController.h" #include "components/fs/FS.h" #include "drivers/Spi.h" #include "drivers/SpiMaster.h" @@ -104,6 +105,7 @@ Pinetime::Controllers::DateTime dateTimeController {settingsController}; Pinetime::Drivers::Watchdog watchdog; Pinetime::Controllers::NotificationManager notificationManager; Pinetime::Controllers::MotionController motionController; +Pinetime::Controllers::StopWatchController stopWatchController; Pinetime::Controllers::AlarmController alarmController {dateTimeController, fs}; Pinetime::Controllers::TouchHandler touchHandler; Pinetime::Controllers::ButtonHandler buttonHandler; @@ -120,6 +122,7 @@ Pinetime::Applications::DisplayApp displayApp(lcd, settingsController, motorController, motionController, + stopWatchController, alarmController, brightnessController, touchHandler, @@ -133,6 +136,7 @@ Pinetime::System::SystemTask systemTask(spi, batteryController, bleController, dateTimeController, + stopWatchController, alarmController, watchdog, notificationManager, diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index eb013d6d1a..976fcda25f 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -39,6 +39,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, Controllers::Battery& batteryController, Controllers::Ble& bleController, Controllers::DateTime& dateTimeController, + Controllers::StopWatchController& stopWatchController, Controllers::AlarmController& alarmController, Drivers::Watchdog& watchdog, Pinetime::Controllers::NotificationManager& notificationManager, @@ -59,6 +60,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, batteryController {batteryController}, bleController {bleController}, dateTimeController {dateTimeController}, + stopWatchController {stopWatchController}, alarmController {alarmController}, watchdog {watchdog}, notificationManager {notificationManager}, diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 0060e36096..2f925aed31 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -15,6 +15,7 @@ #include "systemtask/SystemMonitor.h" #include "components/ble/NimbleController.h" #include "components/ble/NotificationManager.h" +#include "components/stopwatch/StopWatchController.h" #include "components/alarm/AlarmController.h" #include "components/fs/FS.h" #include "touchhandler/TouchHandler.h" @@ -60,6 +61,7 @@ namespace Pinetime { Controllers::Battery& batteryController, Controllers::Ble& bleController, Controllers::DateTime& dateTimeController, + Controllers::StopWatchController& stopWatchController, Controllers::AlarmController& alarmController, Drivers::Watchdog& watchdog, Pinetime::Controllers::NotificationManager& notificationManager, @@ -100,6 +102,7 @@ namespace Pinetime { Pinetime::Controllers::Ble& bleController; Pinetime::Controllers::DateTime& dateTimeController; + Pinetime::Controllers::StopWatchController& stopWatchController; Pinetime::Controllers::AlarmController& alarmController; QueueHandle_t systemTasksMsgQueue; Pinetime::Drivers::Watchdog& watchdog;