From ce91e1a7a6e7ccf674aabfb20770d13cb4eb3190 Mon Sep 17 00:00:00 2001 From: panky-codes Date: Thu, 11 Mar 2021 11:56:58 +0100 Subject: Changed the clock source. Stopwatch works. Need to add butttons. --- src/displayapp/screens/StopWatch.cpp | 33 +++++++++++++++++++++++++-------- src/displayapp/screens/StopWatch.h | 8 ++++---- 2 files changed, 29 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/displayapp/screens/StopWatch.cpp b/src/displayapp/screens/StopWatch.cpp index 834471e0..2ccafb16 100644 --- a/src/displayapp/screens/StopWatch.cpp +++ b/src/displayapp/screens/StopWatch.cpp @@ -2,17 +2,34 @@ #include "Screen.h" #include "lvgl/lvgl.h" +#include "projdefs.h" +#include "FreeRTOSConfig.h" +#include "task.h" #include // Anonymous namespace for local functions namespace { - std::tuple convertMilliSecsToSegments(const int64_t& currentTime) { - const int milliSecs = (currentTime % 1000); // Get only the first two digits and ignore the last - const int secs = (currentTime / 1000) % 60; - const int mins = (currentTime / 1000) / 60; + std::tuple convertTicksToTimeSegments(const TickType_t timeElapsed) { + const int timeElapsedMillis = (static_cast(timeElapsed) / static_cast(configTICK_RATE_HZ)) * 1000; + + const int milliSecs = (timeElapsedMillis % 1000) / 10; // Get only the first two digits and ignore the last + const int secs = (timeElapsedMillis / 1000) % 60; + const int mins = (timeElapsedMillis / 1000) / 60; return std::make_tuple(mins, secs, milliSecs); } + + 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; + } } using namespace Pinetime::Applications::Screens; @@ -43,16 +60,16 @@ bool StopWatch::Refresh() { lv_label_set_text(time, "00:00"); lv_label_set_text(msecTime, "00"); if (currentEvent == Events::PLAY) { - startTime = dateTime.CurrentDateTime(); + startTime = xTaskGetTickCount(); currentState = States::RUNNING; } break; } case States::RUNNING: { - auto delta = std::chrono::duration_cast(dateTime.CurrentDateTime() - startTime); - timeElapsed = delta.count(); - auto timeSeparated = convertMilliSecsToSegments(timeElapsed); + auto timeElapsed = calculateDelta(startTime, xTaskGetTickCount()); + auto timeSeparated = convertTicksToTimeSegments(timeElapsed); lv_label_set_text_fmt(time, "%02d:%02d", std::get<0>(timeSeparated), std::get<1>(timeSeparated)); + lv_label_set_text_fmt(msecTime, "%02d", std::get<2>(timeSeparated)); break; } case States::HALTED: { diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h index ecc1f276..4763fd1e 100644 --- a/src/displayapp/screens/StopWatch.h +++ b/src/displayapp/screens/StopWatch.h @@ -4,7 +4,8 @@ #include "components/datetime/DateTimeController.h" #include "../LittleVgl.h" -#include +#include "FreeRTOS.h" +#include "portmacro_cmsis.h" namespace Pinetime::Applications::Screens { @@ -21,13 +22,12 @@ namespace Pinetime::Applications::Screens { bool OnTouchEvent(uint16_t x, uint16_t y) override; private: - using timeUnit = std::chrono::time_point; const Pinetime::Controllers::DateTime& dateTime; bool running; States currentState; Events currentEvent; - timeUnit startTime; - int64_t timeElapsed; + TickType_t startTime; + TickType_t timeElapsed; lv_obj_t *time, *msecTime; }; } \ No newline at end of file -- cgit v1.2.3