From 3a5103537c8ef663ee036b2c28b50bb471b2a4f7 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sat, 23 Oct 2021 21:18:45 -0600 Subject: [PATCH 01/11] Add time of day to stopwatch app --- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/StopWatch.cpp | 12 +++++++++++- src/displayapp/screens/StopWatch.h | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 38ce930ae0..57c189bc2e 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -424,7 +424,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown); break; case Apps::StopWatch: - currentScreen = std::make_unique(this, *systemTask); + currentScreen = std::make_unique(this, *systemTask, dateTimeController); break; case Apps::Twos: currentScreen = std::make_unique(this); diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index a260d29310..adc04d0bca 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -45,9 +45,10 @@ static void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) { stopWatch->stopLapBtnEventHandler(event); } -StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask) +StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController) : Screen(app), systemTask {systemTask}, + dateTimeController {dateTimeController}, currentState {States::Init}, startTime {}, oldTimeElapsed {}, @@ -55,6 +56,7 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask) lapBuffer {}, lapNr {} { + // Running time 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_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); @@ -101,6 +103,13 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask) lv_obj_align(lapTwoText, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 50, 55); lv_label_set_text(lapTwoText, ""); + // Date time + dateTime = lv_label_create(lv_scr_act(), nullptr); + lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); + lv_label_set_align(dateTime, LV_LABEL_ALIGN_CENTER); + lv_obj_align(dateTime, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0); + lv_obj_set_style_local_text_color(dateTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE); + taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); } @@ -152,6 +161,7 @@ void StopWatch::pause() { } void StopWatch::Refresh() { + lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); if (currentState == States::Running) { timeElapsed = calculateDelta(startTime, xTaskGetTickCount()); currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed)); diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index 0720a5868d..07529e569e 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -62,7 +62,9 @@ namespace Pinetime::Applications::Screens { class StopWatch : public Screen { public: - StopWatch(DisplayApp* app, System::SystemTask& systemTask); + StopWatch(DisplayApp* app, + System::SystemTask& systemTask, + Controllers::DateTime& dateTimeController); ~StopWatch() override; void Refresh() override; @@ -76,6 +78,8 @@ namespace Pinetime::Applications::Screens { private: Pinetime::System::SystemTask& systemTask; + Controllers::DateTime& dateTimeController; + TickType_t timeElapsed; States currentState; TickType_t startTime; @@ -85,6 +89,7 @@ namespace Pinetime::Applications::Screens { int lapNr = 0; lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; lv_obj_t *lapOneText, *lapTwoText; + lv_obj_t *dateTime; lv_task_t* taskRefresh; }; From c75102fb65570f62598b302f50be36e8958edb6f Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sat, 23 Oct 2021 23:28:07 -0600 Subject: [PATCH 02/11] This may have been a bad idea --- .../stopwatch/StopWatchController.cpp | 46 ++++++++++++++++++ .../stopwatch/StopWatchController.h | 39 +++++++++++++++ src/displayapp/DisplayApp.cpp | 5 +- src/displayapp/DisplayApp.h | 4 ++ src/displayapp/screens/StopWatch.cpp | 47 +++++++++---------- src/displayapp/screens/StopWatch.h | 10 ++-- src/systemtask/SystemTask.cpp | 2 + src/systemtask/SystemTask.h | 3 ++ 8 files changed, 124 insertions(+), 32 deletions(-) create mode 100644 src/components/stopwatch/StopWatchController.cpp create mode 100644 src/components/stopwatch/StopWatchController.h diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp new file mode 100644 index 0000000000..a43a3bdea6 --- /dev/null +++ b/src/components/stopwatch/StopWatchController.cpp @@ -0,0 +1,46 @@ +#include "StopWatchController.h" +#include +#include + +using namespace Pinetime::Controllers; + +StopWatch::StopWatch() {} + +// StopWatch::init() {} + +void StopWatch::start(uint32_t start) { + currentState = StopWatchStates::Running; + startTime = start; +} + +// void StopWatch::lap(uint32_t lapEnd); + +void StopWatch::pause(uint32_t end) { + currentState = StopWatchStates::Paused; + timeElapsedPreviously += end - startTime; +} + +void StopWatch::clear() { + currentState = StopWatchStates::Cleared; + timeElapsedPreviously = 0; +} + +uint32_t StopWatch::getStart() { + return startTime; +} + +uint32_t StopWatch::getElapsedPreviously() { + return timeElapsedPreviously; +} + +bool StopWatch::isRunning() { + return currentState == StopWatchStates::Running; +} + +bool StopWatch::isClear() { + return currentState == StopWatchStates::Cleared; +} + +bool StopWatch::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..bcda7e15fe --- /dev/null +++ b/src/components/stopwatch/StopWatchController.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include "portmacro_cmsis.h" + +namespace Pinetime { + namespace System { + class SystemTask; + } + namespace Controllers { + + enum class StopWatchStates { Cleared, Running, Paused }; + + class StopWatch { + public: + StopWatch() = default; + + // void init(); + void start(uint32_t start); + // void lap(uint32_t lapEnd); + void pause(uint32_t end); + void clear(); + + uint32_t getStart(); + uint32_t getElapsedPreviously(); + + bool isRunning(); + bool isCleared(); + bool isPaused(); + + private: + StopWatchStates currentState = StopWatchStates::Cleared; + // Start time of current duration + TickType_t startTime; + // How much time was elapsed before current duration + TickType_t timeElapsedPreviously = 0; + }; + } +} diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 57c189bc2e..c4a2921edf 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -10,6 +10,7 @@ #include "components/ble/NotificationManager.h" #include "components/motion/MotionController.h" #include "components/motor/MotorController.h" +#include "components/stopwatch/StopWatchController.h" #include "displayapp/screens/ApplicationList.h" #include "displayapp/screens/Brightness.h" #include "displayapp/screens/Clock.h" @@ -95,6 +96,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Pinetime::Controllers::MotionController& motionController, Pinetime::Controllers::TimerController& timerController, Pinetime::Controllers::AlarmController& alarmController, + Pinetime::Controllers::StopWatchController& stopWatchController, Pinetime::Controllers::TouchHandler& touchHandler) : lcd {lcd}, lvgl {lvgl}, @@ -110,6 +112,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, motionController {motionController}, timerController {timerController}, alarmController {alarmController}, + stopWatchController {stopWatchController}, touchHandler {touchHandler} { } @@ -424,7 +427,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown); break; case Apps::StopWatch: - currentScreen = std::make_unique(this, *systemTask, dateTimeController); + currentScreen = std::make_unique(this, *systemTask, dateTimeController, stopWatchController); break; case Apps::Twos: currentScreen = std::make_unique(this); diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 39fe631455..109249e677 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -16,6 +16,7 @@ #include "components/timer/TimerController.h" #include "components/alarm/AlarmController.h" #include "touchhandler/TouchHandler.h" +#include "components/stopwatch/StopWatchController.h" #include "displayapp/Messages.h" #include "BootErrors.h" @@ -36,6 +37,7 @@ namespace Pinetime { class HeartRateController; class MotionController; class TouchHandler; + class StopWatchController; } namespace System { @@ -61,6 +63,7 @@ namespace Pinetime { Pinetime::Controllers::MotionController& motionController, Pinetime::Controllers::TimerController& timerController, Pinetime::Controllers::AlarmController& alarmController, + Pinetime::Controllers::StopWatch& stopWatchController, Pinetime::Controllers::TouchHandler& touchHandler); void Start(System::BootErrors error); void PushMessage(Display::Messages msg); @@ -88,6 +91,7 @@ namespace Pinetime { Pinetime::Controllers::TimerController& timerController; Pinetime::Controllers::AlarmController& alarmController; Pinetime::Controllers::TouchHandler& touchHandler; + Pinetime::Controllers::StopWatchController& stopWatchController; Pinetime::Controllers::FirmwareValidator validator; Controllers::BrightnessController brightnessController; diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index adc04d0bca..501a796708 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -45,13 +45,12 @@ static void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) { stopWatch->stopLapBtnEventHandler(event); } -StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController) +StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController, Controllers::StopWatch& stopWatchController) : Screen(app), systemTask {systemTask}, dateTimeController {dateTimeController}, - currentState {States::Init}, - startTime {}, - oldTimeElapsed {}, + stopWatchController {stopWatchController}, + timeElapsed {}, currentTimeSeparated {}, lapBuffer {}, lapNr {} { @@ -120,8 +119,8 @@ StopWatch::~StopWatch() { } void StopWatch::reset() { - currentState = States::Init; - oldTimeElapsed = 0; + stopWatchController.clear(); + 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); @@ -137,34 +136,34 @@ void StopWatch::reset() { } void StopWatch::start() { + stopWatchController.start(xTaskGetTickCount()); + 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; + stopWatchController.pause(xTaskGetTickCount()); + 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); } void StopWatch::Refresh() { lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); - if (currentState == States::Running) { - timeElapsed = calculateDelta(startTime, xTaskGetTickCount()); - currentTimeSeparated = convertTicksToTimeSegments((oldTimeElapsed + timeElapsed)); + if (stopWatchController.isRunning()) { + timeElapsed = calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); + currentTimeSeparated = convertTicksToTimeSegments((stopWatchController.getElapsedPreviously() + timeElapsed)); lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); @@ -175,12 +174,10 @@ void StopWatch::playPauseBtnEventHandler(lv_event_t event) { if (event != LV_EVENT_CLICKED) { return; } - if (currentState == States::Init) { - start(); - } else if (currentState == States::Running) { - pause(); - } else if (currentState == States::Halted) { - start(); + if (stopWatchController.isCleared() || stopWatchController.isPaused()) { + stopWatchController.start(xTaskGetTickCount()); + } else if (stopWatchController.isRunning()) { + stopWatchController.pause(xTaskGetTickCount()); } } @@ -189,7 +186,7 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { return; } // If running, then this button is used to save laps - if (currentState == States::Running) { + if (stopWatchController.isRunning()) { lapBuffer.addLaps(currentTimeSeparated); lapNr++; if (lapBuffer[1]) { @@ -199,14 +196,14 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { 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) { + } else if (stopWatchController.isPaused()) { reset(); } } bool StopWatch::OnButtonPushed() { - if (currentState == States::Running) { - pause(); + if (stopWatchController.isRunning()) { + stopWatchController.pause(xTaskGetTickCount()); return true; } return false; diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index 07529e569e..8827a25130 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -64,7 +64,8 @@ namespace Pinetime::Applications::Screens { public: StopWatch(DisplayApp* app, System::SystemTask& systemTask, - Controllers::DateTime& dateTimeController); + Controllers::DateTime& dateTimeController, + Controllers::StopWatch& stopWatchController); ~StopWatch() override; void Refresh() override; @@ -79,17 +80,14 @@ namespace Pinetime::Applications::Screens { private: Pinetime::System::SystemTask& systemTask; Controllers::DateTime& dateTimeController; + Controllers::StopWatch& stopWatchController; TickType_t timeElapsed; - States currentState; - TickType_t startTime; - TickType_t oldTimeElapsed; TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs LapTextBuffer_t<2> lapBuffer; int lapNr = 0; - lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; + lv_obj_t *dateTime, *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; lv_obj_t *lapOneText, *lapTwoText; - lv_obj_t *dateTime; lv_task_t* taskRefresh; }; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 1120b80d05..062c68b939 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -65,6 +65,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, Controllers::DateTime& dateTimeController, Controllers::TimerController& timerController, Controllers::AlarmController& alarmController, + Controllers::StopWatch& stopWatchController, Drivers::Watchdog& watchdog, Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::MotorController& motorController, @@ -89,6 +90,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, dateTimeController {dateTimeController}, timerController {timerController}, alarmController {alarmController}, + stopWatchController {stopWatchController}, watchdog {watchdog}, notificationManager {notificationManager}, motorController {motorController}, diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index e2e6de7f66..753fb3c615 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -18,6 +18,7 @@ #include "components/motor/MotorController.h" #include "components/timer/TimerController.h" #include "components/alarm/AlarmController.h" +#include "components/stopwatch/StopWatchController.h" #include "components/fs/FS.h" #include "touchhandler/TouchHandler.h" #include "buttonhandler/ButtonHandler.h" @@ -63,6 +64,7 @@ namespace Pinetime { Controllers::DateTime& dateTimeController, Controllers::TimerController& timerController, Controllers::AlarmController& alarmController, + Controllers::StopWatch& stopWatchController, Drivers::Watchdog& watchdog, Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::MotorController& motorController, @@ -121,6 +123,7 @@ namespace Pinetime { Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::HeartRateController& heartRateController; Pinetime::Controllers::MotionController& motionController; + Pinetime::Controllers::StopWatch& stopWatchController; Pinetime::Applications::DisplayApp& displayApp; Pinetime::Applications::HeartRateTask& heartRateApp; From ec2b6738cd86229c4e7781946b0c97d82c310833 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sun, 24 Oct 2021 08:46:38 -0600 Subject: [PATCH 03/11] Implement stop watch controller --- src/CMakeLists.txt | 1 + src/components/stopwatch/StopWatchController.cpp | 4 ++-- src/components/stopwatch/StopWatchController.h | 3 ++- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/DisplayApp.h | 4 ++-- src/main.cpp | 4 ++++ 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e727b2b0ce..5f9dbabfa6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -482,6 +482,7 @@ list(APPEND SOURCE_FILES components/motor/MotorController.cpp components/settings/Settings.cpp components/timer/TimerController.cpp + components/stopwatch/StopWatchController.cpp components/alarm/AlarmController.cpp components/fs/FS.cpp drivers/Cst816s.cpp diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp index a43a3bdea6..f833ffccd8 100644 --- a/src/components/stopwatch/StopWatchController.cpp +++ b/src/components/stopwatch/StopWatchController.cpp @@ -4,7 +4,7 @@ using namespace Pinetime::Controllers; -StopWatch::StopWatch() {} +// StopWatch::StopWatch() {} // StopWatch::init() {} @@ -37,7 +37,7 @@ bool StopWatch::isRunning() { return currentState == StopWatchStates::Running; } -bool StopWatch::isClear() { +bool StopWatch::isCleared() { return currentState == StopWatchStates::Cleared; } diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h index bcda7e15fe..3098a7a919 100644 --- a/src/components/stopwatch/StopWatchController.h +++ b/src/components/stopwatch/StopWatchController.h @@ -1,7 +1,8 @@ #pragma once #include -#include "portmacro_cmsis.h" +// #include "portmacro_cmsis.h" +#include "FreeRTOS.h" namespace Pinetime { namespace System { diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index c4a2921edf..6b05374c5b 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -96,7 +96,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Pinetime::Controllers::MotionController& motionController, Pinetime::Controllers::TimerController& timerController, Pinetime::Controllers::AlarmController& alarmController, - Pinetime::Controllers::StopWatchController& stopWatchController, + Pinetime::Controllers::StopWatch& stopWatchController, Pinetime::Controllers::TouchHandler& touchHandler) : lcd {lcd}, lvgl {lvgl}, diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 109249e677..069b5794cb 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -37,7 +37,7 @@ namespace Pinetime { class HeartRateController; class MotionController; class TouchHandler; - class StopWatchController; + class StopWatch; } namespace System { @@ -91,7 +91,7 @@ namespace Pinetime { Pinetime::Controllers::TimerController& timerController; Pinetime::Controllers::AlarmController& alarmController; Pinetime::Controllers::TouchHandler& touchHandler; - Pinetime::Controllers::StopWatchController& stopWatchController; + Pinetime::Controllers::StopWatch& stopWatchController; Pinetime::Controllers::FirmwareValidator validator; Controllers::BrightnessController brightnessController; diff --git a/src/main.cpp b/src/main.cpp index 53f78ce8a1..0dfdd24d09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,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" @@ -107,6 +108,7 @@ Pinetime::Drivers::WatchdogView watchdogView(watchdog); Pinetime::Controllers::NotificationManager notificationManager; Pinetime::Controllers::MotionController motionController; Pinetime::Controllers::TimerController timerController; +Pinetime::Controllers::StopWatch stopWatchController; Pinetime::Controllers::AlarmController alarmController {dateTimeController}; Pinetime::Controllers::TouchHandler touchHandler(touchPanel, lvgl); Pinetime::Controllers::ButtonHandler buttonHandler; @@ -129,6 +131,7 @@ Pinetime::Applications::DisplayApp displayApp(lcd, motionController, timerController, alarmController, + stopWatchController, touchHandler); Pinetime::System::SystemTask systemTask(spi, @@ -142,6 +145,7 @@ Pinetime::System::SystemTask systemTask(spi, dateTimeController, timerController, alarmController, + stopWatchController, watchdog, notificationManager, motorController, From 04a6b7013dbcb8688a4587f9cb2a8d74ac9a3cd3 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sun, 24 Oct 2021 10:29:34 -0600 Subject: [PATCH 04/11] Fix stopwatch display issues --- src/displayapp/screens/StopWatch.cpp | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index 501a796708..e731e6acd5 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -110,6 +110,18 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controller lv_obj_set_style_local_text_color(dateTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE); taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); + + if (stopWatchController.isRunning()) { + start(); + } else if (stopWatchController.isPaused()) { + pause(); + currentTimeSeparated = convertTicksToTimeSegments(stopWatchController.getElapsedPreviously()); + + lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); + lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); + lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); + lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); + } } StopWatch::~StopWatch() { @@ -119,8 +131,6 @@ StopWatch::~StopWatch() { } void StopWatch::reset() { - stopWatchController.clear(); - 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); @@ -136,8 +146,6 @@ void StopWatch::reset() { } void StopWatch::start() { - stopWatchController.start(xTaskGetTickCount()); - 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); @@ -149,13 +157,14 @@ void StopWatch::start() { } void StopWatch::pause() { - stopWatchController.pause(xTaskGetTickCount()); + lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); - 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); + lv_label_set_text(txtPlayPause, Symbols::play); + lv_label_set_text(txtStopLap, Symbols::stop); + systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); } @@ -176,8 +185,10 @@ void StopWatch::playPauseBtnEventHandler(lv_event_t event) { } if (stopWatchController.isCleared() || stopWatchController.isPaused()) { stopWatchController.start(xTaskGetTickCount()); + start(); } else if (stopWatchController.isRunning()) { stopWatchController.pause(xTaskGetTickCount()); + pause(); } } @@ -197,6 +208,7 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { lv_label_set_text_fmt(lapTwoText, "#%2d %2d:%02d.%02d", lapNr, lapBuffer[0]->mins, lapBuffer[0]->secs, lapBuffer[0]->hundredths); } } else if (stopWatchController.isPaused()) { + stopWatchController.clear(); reset(); } } @@ -204,6 +216,7 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { bool StopWatch::OnButtonPushed() { if (stopWatchController.isRunning()) { stopWatchController.pause(xTaskGetTickCount()); + pause(); return true; } return false; From 2fd2cdb6dacf993595150bf9cb23b073c20223ee Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sun, 24 Oct 2021 20:41:02 -0600 Subject: [PATCH 05/11] Implement lap tracking --- .../stopwatch/StopWatchController.cpp | 67 +++++++++++++++++-- .../stopwatch/StopWatchController.h | 49 +++++++++++--- src/displayapp/screens/StopWatch.cpp | 64 ++++++++++++++---- src/displayapp/screens/StopWatch.h | 47 +------------ 4 files changed, 151 insertions(+), 76 deletions(-) diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp index f833ffccd8..01a305ecba 100644 --- a/src/components/stopwatch/StopWatchController.cpp +++ b/src/components/stopwatch/StopWatchController.cpp @@ -4,27 +4,80 @@ using namespace Pinetime::Controllers; -// StopWatch::StopWatch() {} +namespace { + TickType_t calculateDelta(const TickType_t startTime, const TickType_t currentTime) { + TickType_t delta = 0; + // Take care of overflow + if (startTime > currentTime) { + delta = 0xffffffff - startTime; + delta += (currentTime + 1); + } else { + delta = currentTime - startTime; + } + return delta; + } +} + +StopWatch::StopWatch() { + clear(); +} -// StopWatch::init() {} +// State Change -void StopWatch::start(uint32_t start) { +void StopWatch::start(TickType_t start) { currentState = StopWatchStates::Running; startTime = start; } -// void StopWatch::lap(uint32_t lapEnd); - -void StopWatch::pause(uint32_t end) { +void StopWatch::pause(TickType_t end) { currentState = StopWatchStates::Paused; - timeElapsedPreviously += end - startTime; + timeElapsedPreviously += calculateDelta(startTime, end); } void StopWatch::clear() { currentState = StopWatchStates::Cleared; timeElapsedPreviously = 0; + + for (int i = 0; i < LAP_CAPACITY; i++) { + laps[i].count = 0; + laps[i].time = 0; + } + lapCount = 0; + lapHead = 0; +} + +// Lap + +void StopWatch::pushLap(TickType_t lapEnd) { + laps[lapHead].time = lapEnd; + laps[lapHead].count = lapCount + 1; + lapCount += 1; + lapHead = lapCount % LAP_CAPACITY; } +uint32_t StopWatch::getLapNum() { + if (lapCount < LAP_CAPACITY) + return lapCount; + else + return LAP_CAPACITY; +} + +uint32_t StopWatch::getLapCount() { + return lapCount; +} + +LapInfo_t *StopWatch::lastLap(uint32_t lap) { + if (lap >= LAP_CAPACITY || lap >= lapCount || lapCount == 0) { + // Return "empty" LapInfo_t + return &emptyLapInfo; + } + // Index backwards + uint32_t index = ((lapHead + LAP_CAPACITY) - lap) % LAP_CAPACITY; + return &laps[index]; +} + +// Data acess + uint32_t StopWatch::getStart() { return startTime; } diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h index 3098a7a919..99af3a6150 100644 --- a/src/components/stopwatch/StopWatchController.h +++ b/src/components/stopwatch/StopWatchController.h @@ -1,9 +1,10 @@ #pragma once #include -// #include "portmacro_cmsis.h" #include "FreeRTOS.h" +#define LAP_CAPACITY 2 + namespace Pinetime { namespace System { class SystemTask; @@ -12,29 +13,55 @@ namespace Pinetime { enum class StopWatchStates { Cleared, Running, Paused }; + struct LapInfo_t { + uint32_t count = 0; // Used to label the lap + TickType_t time = 0; // delta time from beginning of stopwatch + }; + class StopWatch { public: - StopWatch() = default; + StopWatch(); - // void init(); - void start(uint32_t start); - // void lap(uint32_t lapEnd); - void pause(uint32_t end); + // StopWatch functionality and data + void start(TickType_t start); + void pause(TickType_t end); void clear(); - uint32_t getStart(); - uint32_t getElapsedPreviously(); + TickType_t getStart(); + TickType_t getElapsedPreviously(); + + // Lap functionality + + /// Only the latest laps are stored, the lap count is saved until reset + void pushLap(TickType_t lapEnd); + + /// Returns actual count of stored laps + uint32_t getLapNum(); + + /// Returns lapCount + uint32_t getLapCount(); + + /// Indexes into lap history, with 0 being the latest lap. + /// If the lap is unavailable, count and time will be 0. If there is a + /// real value, count should be above 0 + LapInfo_t *lastLap(uint32_t lap=0); bool isRunning(); bool isCleared(); bool isPaused(); private: - StopWatchStates currentState = StopWatchStates::Cleared; + // Current state of stopwatch + StopWatchStates currentState = StopWatchStates::Cleared; // Start time of current duration - TickType_t startTime; + TickType_t startTime = 0; // How much time was elapsed before current duration - TickType_t timeElapsedPreviously = 0; + TickType_t timeElapsedPreviously = 0; + // Stores lap times + LapInfo_t laps[LAP_CAPACITY]; + LapInfo_t emptyLapInfo = { .count = 0, .time = 0 }; + uint32_t lapCount = 0; + uint32_t lapHead = 0; }; } } diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index e731e6acd5..e789c35c1b 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -51,9 +51,7 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controller dateTimeController {dateTimeController}, stopWatchController {stopWatchController}, timeElapsed {}, - currentTimeSeparated {}, - lapBuffer {}, - lapNr {} { + currentTimeSeparated {} { // Running time time = lv_label_create(lv_scr_act(), nullptr); @@ -121,6 +119,12 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controller lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); + } else { + reset(); + } + + if (stopWatchController.getLapCount() > 0) { + refreshLaps(); } } @@ -139,8 +143,7 @@ void StopWatch::reset() { 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); } @@ -179,6 +182,35 @@ void StopWatch::Refresh() { } } +void StopWatch::refreshLaps() { + Pinetime::Controllers::LapInfo_t + // Latest lap + *lap1 = stopWatchController.lastLap(), + // Second latest lap + *lap2 = stopWatchController.lastLap(1); + + if (lap1->count != 0) { + TimeSeparated_t laptime = convertTicksToTimeSegments(lap1->time); + lv_label_set_text_fmt( + lapOneText, + "#%2d %2d:%02d.%02d", + lap1->count, + laptime.mins, + laptime.secs, + laptime.hundredths); + } + if (lap2->count != 0) { + TimeSeparated_t laptime = convertTicksToTimeSegments(lap2->time); + lv_label_set_text_fmt( + lapTwoText, + "#%2d %2d:%02d.%02d", + lap2->count, + laptime.mins, + laptime.secs, + laptime.hundredths); + } +} + void StopWatch::playPauseBtnEventHandler(lv_event_t event) { if (event != LV_EVENT_CLICKED) { return; @@ -198,15 +230,19 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { } // If running, then this button is used to save laps if (stopWatchController.isRunning()) { - lapBuffer.addLaps(currentTimeSeparated); - lapNr++; - 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); - } + // lapBuffer.addLaps(currentTimeSeparated); + // lapNr++; + // 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); + // } + TickType_t currentTime = stopWatchController.getElapsedPreviously() + calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); + stopWatchController.pushLap(currentTime); + + refreshLaps(); } else if (stopWatchController.isPaused()) { stopWatchController.clear(); reset(); diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index 8827a25130..38ebdf15b0 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -3,6 +3,7 @@ #include "displayapp/screens/Screen.h" #include "components/datetime/DateTimeController.h" #include "displayapp/LittleVgl.h" +#include "components/stopwatch/StopWatchController.h" #include "FreeRTOS.h" #include "portmacro_cmsis.h" @@ -12,54 +13,12 @@ namespace Pinetime::Applications::Screens { - enum class States { Init, Running, Halted }; - struct TimeSeparated_t { int mins; int secs; int hundredths; }; - // A simple buffer to hold the latest two laps - template struct LapTextBuffer_t { - LapTextBuffer_t() : buffer {}, currentSize {}, capacity {N}, head {-1} { - } - - void addLaps(const TimeSeparated_t& timeVal) { - head++; - head %= capacity; - buffer[head] = timeVal; - - if (currentSize < capacity) { - currentSize++; - } - } - - void clearBuffer() { - buffer = {}; - currentSize = 0; - head = -1; - } - - TimeSeparated_t* operator[](std::size_t idx) { - // Sanity check for out-of-bounds - if (idx >= 0 && idx < capacity) { - if (idx < currentSize) { - // This transformation is to ensure that head is always pointing to index 0. - const auto transformed_idx = (head - idx) % capacity; - return (&buffer[transformed_idx]); - } - } - return nullptr; - } - - private: - std::array buffer; - uint8_t currentSize; - uint8_t capacity; - int8_t head; - }; - class StopWatch : public Screen { public: StopWatch(DisplayApp* app, @@ -76,6 +35,7 @@ namespace Pinetime::Applications::Screens { void reset(); void start(); void pause(); + void refreshLaps() ; private: Pinetime::System::SystemTask& systemTask; @@ -84,8 +44,7 @@ namespace Pinetime::Applications::Screens { TickType_t timeElapsed; TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs - LapTextBuffer_t<2> lapBuffer; - int lapNr = 0; + lv_obj_t *dateTime, *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; lv_obj_t *lapOneText, *lapTwoText; From 9ea6bc38d4ff5ffc3bea6f886580b6fcc6b2e1b2 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 25 Oct 2021 06:11:27 -0600 Subject: [PATCH 06/11] Removed unsigned ints These appear to have been the cause of my issues. Not sure why, I'd have to know the nrf52832 better to diagnose it. --- .../stopwatch/StopWatchController.cpp | 18 +++++++++++------- src/components/stopwatch/StopWatchController.h | 12 ++++++------ src/displayapp/screens/StopWatch.cpp | 7 ++----- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp index 01a305ecba..2ec3f94194 100644 --- a/src/components/stopwatch/StopWatchController.cpp +++ b/src/components/stopwatch/StopWatchController.cpp @@ -55,34 +55,38 @@ void StopWatch::pushLap(TickType_t lapEnd) { lapHead = lapCount % LAP_CAPACITY; } -uint32_t StopWatch::getLapNum() { +int StopWatch::getLapNum() { if (lapCount < LAP_CAPACITY) return lapCount; else return LAP_CAPACITY; } -uint32_t StopWatch::getLapCount() { +int StopWatch::getLapCount() { return lapCount; } -LapInfo_t *StopWatch::lastLap(uint32_t lap) { - if (lap >= LAP_CAPACITY || lap >= lapCount || lapCount == 0) { +int wrap(int index) { + return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY; +} + +LapInfo_t *StopWatch::lastLap(int lap) { + if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) { // Return "empty" LapInfo_t return &emptyLapInfo; } // Index backwards - uint32_t index = ((lapHead + LAP_CAPACITY) - lap) % LAP_CAPACITY; + int index = wrap(lapHead - lap); return &laps[index]; } // Data acess -uint32_t StopWatch::getStart() { +TickType_t StopWatch::getStart() { return startTime; } -uint32_t StopWatch::getElapsedPreviously() { +TickType_t StopWatch::getElapsedPreviously() { return timeElapsedPreviously; } diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h index 99af3a6150..67470d1198 100644 --- a/src/components/stopwatch/StopWatchController.h +++ b/src/components/stopwatch/StopWatchController.h @@ -14,7 +14,7 @@ namespace Pinetime { enum class StopWatchStates { Cleared, Running, Paused }; struct LapInfo_t { - uint32_t count = 0; // Used to label the lap + int count = 0; // Used to label the lap TickType_t time = 0; // delta time from beginning of stopwatch }; @@ -36,15 +36,15 @@ namespace Pinetime { void pushLap(TickType_t lapEnd); /// Returns actual count of stored laps - uint32_t getLapNum(); + int getLapNum(); /// Returns lapCount - uint32_t getLapCount(); + int getLapCount(); /// Indexes into lap history, with 0 being the latest lap. /// If the lap is unavailable, count and time will be 0. If there is a /// real value, count should be above 0 - LapInfo_t *lastLap(uint32_t lap=0); + LapInfo_t *lastLap(int lap=0); bool isRunning(); bool isCleared(); @@ -60,8 +60,8 @@ namespace Pinetime { // Stores lap times LapInfo_t laps[LAP_CAPACITY]; LapInfo_t emptyLapInfo = { .count = 0, .time = 0 }; - uint32_t lapCount = 0; - uint32_t lapHead = 0; + int lapCount = 0; + int lapHead = 0; }; } } diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index e789c35c1b..8ddda3dcdf 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -183,11 +183,8 @@ void StopWatch::Refresh() { } void StopWatch::refreshLaps() { - Pinetime::Controllers::LapInfo_t - // Latest lap - *lap1 = stopWatchController.lastLap(), - // Second latest lap - *lap2 = stopWatchController.lastLap(1); + Pinetime::Controllers::LapInfo_t *lap1 = stopWatchController.lastLap(); + Pinetime::Controllers::LapInfo_t *lap2 = stopWatchController.lastLap(1); if (lap1->count != 0) { TimeSeparated_t laptime = convertTicksToTimeSegments(lap1->time); From 858edfc95f53c1d99698f1a4fbf8ae027eda3a18 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 25 Oct 2021 06:43:16 -0600 Subject: [PATCH 07/11] Make function names more descriptive Also removes OnButtonPressed. I prefer the button's function to stay the same when possible, but I'll change it if needed for the PR. --- src/displayapp/screens/StopWatch.cpp | 64 ++++++++++------------------ src/displayapp/screens/StopWatch.h | 9 ++-- 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index 8ddda3dcdf..0cb5d8f273 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -110,9 +110,9 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controller taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); if (stopWatchController.isRunning()) { - start(); + displayRunning(); } else if (stopWatchController.isPaused()) { - pause(); + displayPaused(); currentTimeSeparated = convertTicksToTimeSegments(stopWatchController.getElapsedPreviously()); lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); @@ -120,11 +120,11 @@ StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controller lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); } else { - reset(); + displayCleared(); } if (stopWatchController.getLapCount() > 0) { - refreshLaps(); + updateLaps(); } } @@ -134,7 +134,7 @@ StopWatch::~StopWatch() { lv_obj_clean(lv_scr_act()); } -void StopWatch::reset() { +void StopWatch::displayCleared() { 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); @@ -148,7 +148,7 @@ void StopWatch::reset() { lv_obj_set_state(txtStopLap, LV_STATE_DISABLED); } -void StopWatch::start() { +void StopWatch::displayRunning() { 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); @@ -159,7 +159,7 @@ void StopWatch::start() { systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping); } -void StopWatch::pause() { +void StopWatch::displayPaused() { lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW); @@ -171,18 +171,7 @@ void StopWatch::pause() { systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping); } -void StopWatch::Refresh() { - lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); - if (stopWatchController.isRunning()) { - timeElapsed = calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); - currentTimeSeparated = convertTicksToTimeSegments((stopWatchController.getElapsedPreviously() + timeElapsed)); - - lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); - lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); - } -} - -void StopWatch::refreshLaps() { +void StopWatch::updateLaps() { Pinetime::Controllers::LapInfo_t *lap1 = stopWatchController.lastLap(); Pinetime::Controllers::LapInfo_t *lap2 = stopWatchController.lastLap(1); @@ -208,16 +197,27 @@ void StopWatch::refreshLaps() { } } +void StopWatch::Refresh() { + lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); + if (stopWatchController.isRunning()) { + timeElapsed = calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); + currentTimeSeparated = convertTicksToTimeSegments((stopWatchController.getElapsedPreviously() + timeElapsed)); + + lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); + lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); + } +} + void StopWatch::playPauseBtnEventHandler(lv_event_t event) { if (event != LV_EVENT_CLICKED) { return; } if (stopWatchController.isCleared() || stopWatchController.isPaused()) { stopWatchController.start(xTaskGetTickCount()); - start(); + displayRunning(); } else if (stopWatchController.isRunning()) { stopWatchController.pause(xTaskGetTickCount()); - pause(); + displayPaused(); } } @@ -227,30 +227,12 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { } // If running, then this button is used to save laps if (stopWatchController.isRunning()) { - // lapBuffer.addLaps(currentTimeSeparated); - // lapNr++; - // 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); - // } TickType_t currentTime = stopWatchController.getElapsedPreviously() + calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); stopWatchController.pushLap(currentTime); - refreshLaps(); + updateLaps(); } else if (stopWatchController.isPaused()) { stopWatchController.clear(); - reset(); - } -} - -bool StopWatch::OnButtonPushed() { - if (stopWatchController.isRunning()) { - stopWatchController.pause(xTaskGetTickCount()); - pause(); - return true; + displayCleared(); } - return false; } diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index 38ebdf15b0..e65bcb87e8 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -30,12 +30,11 @@ namespace Pinetime::Applications::Screens { void playPauseBtnEventHandler(lv_event_t event); void stopLapBtnEventHandler(lv_event_t event); - bool OnButtonPushed() override; - void reset(); - void start(); - void pause(); - void refreshLaps() ; + void displayCleared(); + void displayRunning(); + void displayPaused(); + void updateLaps(); private: Pinetime::System::SystemTask& systemTask; From c7c63095299b626032136ab77284859bcb325d58 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 25 Oct 2021 06:53:05 -0600 Subject: [PATCH 08/11] Run clang-format on files changed in branch --- .../stopwatch/StopWatchController.cpp | 2 +- .../stopwatch/StopWatchController.h | 6 ++-- src/displayapp/screens/StopWatch.cpp | 28 +++++++------------ src/main.cpp | 8 ++---- src/systemtask/SystemTask.cpp | 3 +- 5 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/components/stopwatch/StopWatchController.cpp b/src/components/stopwatch/StopWatchController.cpp index 2ec3f94194..adbc091524 100644 --- a/src/components/stopwatch/StopWatchController.cpp +++ b/src/components/stopwatch/StopWatchController.cpp @@ -70,7 +70,7 @@ int wrap(int index) { return ((index % LAP_CAPACITY) + LAP_CAPACITY) % LAP_CAPACITY; } -LapInfo_t *StopWatch::lastLap(int lap) { +LapInfo_t* StopWatch::lastLap(int lap) { if (lap >= LAP_CAPACITY || lap > lapCount || lapCount == 0) { // Return "empty" LapInfo_t return &emptyLapInfo; diff --git a/src/components/stopwatch/StopWatchController.h b/src/components/stopwatch/StopWatchController.h index 67470d1198..d1198f1de7 100644 --- a/src/components/stopwatch/StopWatchController.h +++ b/src/components/stopwatch/StopWatchController.h @@ -14,7 +14,7 @@ namespace Pinetime { enum class StopWatchStates { Cleared, Running, Paused }; struct LapInfo_t { - int count = 0; // Used to label the lap + int count = 0; // Used to label the lap TickType_t time = 0; // delta time from beginning of stopwatch }; @@ -44,7 +44,7 @@ namespace Pinetime { /// Indexes into lap history, with 0 being the latest lap. /// If the lap is unavailable, count and time will be 0. If there is a /// real value, count should be above 0 - LapInfo_t *lastLap(int lap=0); + LapInfo_t* lastLap(int lap = 0); bool isRunning(); bool isCleared(); @@ -59,7 +59,7 @@ namespace Pinetime { TickType_t timeElapsedPreviously = 0; // Stores lap times LapInfo_t laps[LAP_CAPACITY]; - LapInfo_t emptyLapInfo = { .count = 0, .time = 0 }; + LapInfo_t emptyLapInfo = {.count = 0, .time = 0}; int lapCount = 0; int lapHead = 0; }; diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index 0cb5d8f273..c7028474a1 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -45,7 +45,10 @@ static void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) { stopWatch->stopLapBtnEventHandler(event); } -StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController, Controllers::StopWatch& stopWatchController) +StopWatch::StopWatch(DisplayApp* app, + System::SystemTask& systemTask, + Controllers::DateTime& dateTimeController, + Controllers::StopWatch& stopWatchController) : Screen(app), systemTask {systemTask}, dateTimeController {dateTimeController}, @@ -172,28 +175,16 @@ void StopWatch::displayPaused() { } void StopWatch::updateLaps() { - Pinetime::Controllers::LapInfo_t *lap1 = stopWatchController.lastLap(); - Pinetime::Controllers::LapInfo_t *lap2 = stopWatchController.lastLap(1); + Pinetime::Controllers::LapInfo_t* lap1 = stopWatchController.lastLap(); + Pinetime::Controllers::LapInfo_t* lap2 = stopWatchController.lastLap(1); if (lap1->count != 0) { TimeSeparated_t laptime = convertTicksToTimeSegments(lap1->time); - lv_label_set_text_fmt( - lapOneText, - "#%2d %2d:%02d.%02d", - lap1->count, - laptime.mins, - laptime.secs, - laptime.hundredths); + lv_label_set_text_fmt(lapOneText, "#%2d %2d:%02d.%02d", lap1->count, laptime.mins, laptime.secs, laptime.hundredths); } if (lap2->count != 0) { TimeSeparated_t laptime = convertTicksToTimeSegments(lap2->time); - lv_label_set_text_fmt( - lapTwoText, - "#%2d %2d:%02d.%02d", - lap2->count, - laptime.mins, - laptime.secs, - laptime.hundredths); + lv_label_set_text_fmt(lapTwoText, "#%2d %2d:%02d.%02d", lap2->count, laptime.mins, laptime.secs, laptime.hundredths); } } @@ -227,7 +218,8 @@ void StopWatch::stopLapBtnEventHandler(lv_event_t event) { } // If running, then this button is used to save laps if (stopWatchController.isRunning()) { - TickType_t currentTime = stopWatchController.getElapsedPreviously() + calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); + TickType_t currentTime = + stopWatchController.getElapsedPreviously() + calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); stopWatchController.pushLap(currentTime); updateLaps(); diff --git a/src/main.cpp b/src/main.cpp index 0dfdd24d09..3353e53b76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -160,7 +160,7 @@ Pinetime::System::SystemTask systemTask(spi, touchHandler, buttonHandler); -/* Variable Declarations for variables in noinit SRAM +/* Variable Declarations for variables in noinit SRAM Increment NoInit_MagicValue upon adding variables to this area */ extern uint32_t __start_noinit_data; @@ -169,7 +169,6 @@ static constexpr uint32_t NoInit_MagicValue = 0xDEAD0000; uint32_t NoInit_MagicWord __attribute__((section(".noinit"))); std::chrono::time_point NoInit_BackUpTime __attribute__((section(".noinit"))); - void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { if (pin == Pinetime::PinMap::Cst816sIrq) { systemTask.OnTouchEvent(); @@ -328,12 +327,11 @@ int main(void) { // retrieve version stored by bootloader Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); - if (NoInit_MagicWord == NoInit_MagicValue) { dateTimeController.SetCurrentTime(NoInit_BackUpTime); } else { - //Clear Memory to known state - memset(&__start_noinit_data,0,(uintptr_t)&__stop_noinit_data-(uintptr_t)&__start_noinit_data); + // Clear Memory to known state + memset(&__start_noinit_data, 0, (uintptr_t) &__stop_noinit_data - (uintptr_t) &__start_noinit_data); NoInit_MagicWord = NoInit_MagicValue; } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 062c68b939..2e29e259e5 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -195,7 +195,8 @@ void SystemTask::Work() { nrfx_gpiote_in_event_enable(PinMap::Button, true); // Touchscreen - nrf_gpio_cfg_sense_input(PinMap::Cst816sIrq, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_Low); + nrf_gpio_cfg_sense_input( + PinMap::Cst816sIrq, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_Low); pinConfig.skip_gpio_setup = true; pinConfig.hi_accuracy = false; From a4a0a4c874c9c53ce848e79466d1c2f3a2938e1f Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 25 Oct 2021 07:13:40 -0600 Subject: [PATCH 09/11] Prevent lap count check when watch is stopped --- src/displayapp/screens/StopWatch.cpp | 42 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index c7028474a1..55ef2f26ee 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -63,12 +63,13 @@ StopWatch::StopWatch(DisplayApp* app, lv_label_set_text(time, "00:00"); lv_obj_align(time, lv_scr_act(), LV_ALIGN_CENTER, 0, -45); + // Create millisecond label msecTime = lv_label_create(lv_scr_act(), nullptr); - // lv_obj_set_style_local_text_font(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20); lv_obj_set_style_local_text_color(msecTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); lv_label_set_text(msecTime, "00"); lv_obj_align(msecTime, lv_scr_act(), LV_ALIGN_CENTER, 0, 3); + // Create play/pause button btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); btnPlayPause->user_data = this; lv_obj_set_event_cb(btnPlayPause, play_pause_event_handler); @@ -78,6 +79,7 @@ StopWatch::StopWatch(DisplayApp* app, txtPlayPause = lv_label_create(btnPlayPause, nullptr); lv_label_set_text(txtPlayPause, Symbols::play); + // Create stop/lap button btnStopLap = lv_btn_create(lv_scr_act(), nullptr); btnStopLap->user_data = this; lv_obj_set_event_cb(btnStopLap, stop_lap_event_handler); @@ -91,19 +93,19 @@ StopWatch::StopWatch(DisplayApp* app, lv_obj_set_state(btnStopLap, LV_STATE_DISABLED); lv_obj_set_state(txtStopLap, LV_STATE_DISABLED); + // Create first lap text label lapOneText = lv_label_create(lv_scr_act(), nullptr); - // lv_obj_set_style_local_text_font(lapOneText, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20); lv_obj_set_style_local_text_color(lapOneText, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW); lv_obj_align(lapOneText, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 50, 30); lv_label_set_text(lapOneText, ""); + // Create second lap text label lapTwoText = lv_label_create(lv_scr_act(), nullptr); - // lv_obj_set_style_local_text_font(lapTwoText, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20); lv_obj_set_style_local_text_color(lapTwoText, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW); lv_obj_align(lapTwoText, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 50, 55); lv_label_set_text(lapTwoText, ""); - // Date time + // Create Date time label dateTime = lv_label_create(lv_scr_act(), nullptr); lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); lv_label_set_align(dateTime, LV_LABEL_ALIGN_CENTER); @@ -112,22 +114,26 @@ StopWatch::StopWatch(DisplayApp* app, taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); - if (stopWatchController.isRunning()) { - displayRunning(); - } else if (stopWatchController.isPaused()) { - displayPaused(); - currentTimeSeparated = convertTicksToTimeSegments(stopWatchController.getElapsedPreviously()); - - lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); - lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); - lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); - lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); - } else { + // Figure out what the current state of the stopwatch is and select the correct display + if (stopWatchController.isCleared()) { displayCleared(); - } + } else { + // Add laps if there are any + if (stopWatchController.getLapCount() > 0) { + updateLaps(); + } - if (stopWatchController.getLapCount() > 0) { - updateLaps(); + if (stopWatchController.isRunning()) { + displayRunning(); + } else if (stopWatchController.isPaused()) { + displayPaused(); + currentTimeSeparated = convertTicksToTimeSegments(stopWatchController.getElapsedPreviously()); + + lv_label_set_text_fmt(time, "%02d:%02d", currentTimeSeparated.mins, currentTimeSeparated.secs); + lv_label_set_text_fmt(msecTime, "%02d", currentTimeSeparated.hundredths); + lv_obj_set_state(btnStopLap, LV_STATE_DEFAULT); + lv_obj_set_state(txtStopLap, LV_STATE_DEFAULT); + } } } From 8fbc164844a1bc03105fc10fa74750bcf353e2d5 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Tue, 26 Oct 2021 18:23:05 -0600 Subject: [PATCH 10/11] Make `make all` complete successfully - Added StopWatchController to DisplayAppRecovery - Added StopWatchController to everywhere needed in CMakeLists - Fixed the reorder warning --- src/CMakeLists.txt | 2 ++ src/displayapp/DisplayApp.h | 2 +- src/displayapp/DisplayAppRecovery.cpp | 1 + src/displayapp/DisplayAppRecovery.h | 2 ++ src/systemtask/SystemTask.h | 2 +- 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f9dbabfa6..89a85f6890 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -553,6 +553,7 @@ list(APPEND RECOVERY_SOURCE_FILES components/settings/Settings.cpp components/timer/TimerController.cpp components/alarm/AlarmController.cpp + components/stopwatch/StopWatchController.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c @@ -661,6 +662,7 @@ set(INCLUDE_FILES components/settings/Settings.h components/timer/TimerController.h components/alarm/AlarmController.h + components/stopwatch/StopWatchController.h drivers/Cst816s.h FreeRTOS/portmacro.h FreeRTOS/portmacro_cmsis.h diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 069b5794cb..1839f29193 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -90,8 +90,8 @@ namespace Pinetime { Pinetime::Controllers::MotionController& motionController; Pinetime::Controllers::TimerController& timerController; Pinetime::Controllers::AlarmController& alarmController; - Pinetime::Controllers::TouchHandler& touchHandler; Pinetime::Controllers::StopWatch& stopWatchController; + Pinetime::Controllers::TouchHandler& touchHandler; Pinetime::Controllers::FirmwareValidator validator; Controllers::BrightnessController brightnessController; diff --git a/src/displayapp/DisplayAppRecovery.cpp b/src/displayapp/DisplayAppRecovery.cpp index fd7017a451..e293b561f3 100644 --- a/src/displayapp/DisplayAppRecovery.cpp +++ b/src/displayapp/DisplayAppRecovery.cpp @@ -23,6 +23,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd, Pinetime::Controllers::MotionController& motionController, Pinetime::Controllers::TimerController& timerController, Pinetime::Controllers::AlarmController& alarmController, + Pinetime::Controllers::StopWatch& stopWatchController, Pinetime::Controllers::TouchHandler& touchHandler) : lcd {lcd}, bleController {bleController} { diff --git a/src/displayapp/DisplayAppRecovery.h b/src/displayapp/DisplayAppRecovery.h index 86e956d133..47d8cd2d79 100644 --- a/src/displayapp/DisplayAppRecovery.h +++ b/src/displayapp/DisplayAppRecovery.h @@ -34,6 +34,7 @@ namespace Pinetime { class MotorController; class TimerController; class AlarmController; + class StopWatch; } namespace System { @@ -57,6 +58,7 @@ namespace Pinetime { Pinetime::Controllers::MotionController& motionController, Pinetime::Controllers::TimerController& timerController, Pinetime::Controllers::AlarmController& alarmController, + Pinetime::Controllers::StopWatch& stopWatchController, Pinetime::Controllers::TouchHandler& touchHandler); void Start(); void Start(Pinetime::System::BootErrors){ Start(); }; diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 753fb3c615..9c1e75f39c 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -110,6 +110,7 @@ namespace Pinetime { Pinetime::Controllers::DateTime& dateTimeController; Pinetime::Controllers::TimerController& timerController; Pinetime::Controllers::AlarmController& alarmController; + Pinetime::Controllers::StopWatch& stopWatchController; QueueHandle_t systemTasksMsgQueue; std::atomic isSleeping {false}; std::atomic isGoingToSleep {false}; @@ -123,7 +124,6 @@ namespace Pinetime { Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::HeartRateController& heartRateController; Pinetime::Controllers::MotionController& motionController; - Pinetime::Controllers::StopWatch& stopWatchController; Pinetime::Applications::DisplayApp& displayApp; Pinetime::Applications::HeartRateTask& heartRateApp; From ec72f56d2bbb831d34d29d8660cf1c90215271dd Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Tue, 26 Oct 2021 19:03:46 -0600 Subject: [PATCH 11/11] Add battery icon to stopwatch screen --- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/StopWatch.cpp | 11 ++++++++++- src/displayapp/screens/StopWatch.h | 8 ++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 6b05374c5b..aebbda032a 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -427,7 +427,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) ReturnApp(Apps::QuickSettings, FullRefreshDirections::Down, TouchEvents::SwipeDown); break; case Apps::StopWatch: - currentScreen = std::make_unique(this, *systemTask, dateTimeController, stopWatchController); + currentScreen = std::make_unique(this, *systemTask, dateTimeController, stopWatchController, batteryController); break; case Apps::Twos: currentScreen = std::make_unique(this); diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index 55ef2f26ee..fb88aff723 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -6,6 +6,7 @@ #include "projdefs.h" #include "FreeRTOSConfig.h" #include "task.h" +#include "BatteryIcon.h" #include @@ -48,11 +49,13 @@ static void stop_lap_event_handler(lv_obj_t* obj, lv_event_t event) { StopWatch::StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController, - Controllers::StopWatch& stopWatchController) + Controllers::StopWatch& stopWatchController, + Controllers::Battery& batteryController) : Screen(app), systemTask {systemTask}, dateTimeController {dateTimeController}, stopWatchController {stopWatchController}, + batteryController {batteryController}, timeElapsed {}, currentTimeSeparated {} { @@ -112,6 +115,11 @@ StopWatch::StopWatch(DisplayApp* app, lv_obj_align(dateTime, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0); lv_obj_set_style_local_text_color(dateTime, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE); + // Battery + batteryIcon = lv_label_create(lv_scr_act(), nullptr); + lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryController.PercentRemaining())); + lv_obj_align(batteryIcon, nullptr, LV_ALIGN_IN_TOP_RIGHT, -8, 0); + 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 @@ -196,6 +204,7 @@ void StopWatch::updateLaps() { void StopWatch::Refresh() { lv_label_set_text_fmt(dateTime, "%02i:%02i", dateTimeController.Hours(), dateTimeController.Minutes()); + lv_label_set_text(batteryIcon, BatteryIcon::GetBatteryIcon(batteryController.PercentRemaining())); if (stopWatchController.isRunning()) { timeElapsed = calculateDelta(stopWatchController.getStart(), xTaskGetTickCount()); currentTimeSeparated = convertTicksToTimeSegments((stopWatchController.getElapsedPreviously() + timeElapsed)); diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index e65bcb87e8..67f985066a 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -4,6 +4,8 @@ #include "components/datetime/DateTimeController.h" #include "displayapp/LittleVgl.h" #include "components/stopwatch/StopWatchController.h" +#include "components/battery/BatteryController.h" +#include "displayapp/LittleVgl.h" #include "FreeRTOS.h" #include "portmacro_cmsis.h" @@ -24,7 +26,8 @@ namespace Pinetime::Applications::Screens { StopWatch(DisplayApp* app, System::SystemTask& systemTask, Controllers::DateTime& dateTimeController, - Controllers::StopWatch& stopWatchController); + Controllers::StopWatch& stopWatchController, + Controllers::Battery& batteryController); ~StopWatch() override; void Refresh() override; @@ -40,11 +43,12 @@ namespace Pinetime::Applications::Screens { Pinetime::System::SystemTask& systemTask; Controllers::DateTime& dateTimeController; Controllers::StopWatch& stopWatchController; + Controllers::Battery& batteryController; TickType_t timeElapsed; TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs - lv_obj_t *dateTime, *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap; + lv_obj_t *dateTime, *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap, *batteryIcon; lv_obj_t *lapOneText, *lapTwoText; lv_task_t* taskRefresh;