summaryrefslogtreecommitdiff
path: root/src/displayapp/screens/StopWatch.h
diff options
context:
space:
mode:
authorJF002 <JF002@users.noreply.github.com>2021-03-21 16:33:27 +0100
committerGitHub <noreply@github.com>2021-03-21 16:33:27 +0100
commit7b39130f88525be1419a483b7313b73feaec2bb7 (patch)
tree8a744bce87e0779a52342c64456c11f6b86488c4 /src/displayapp/screens/StopWatch.h
parenta3ff2e46ca9e663af864a2bd04b9afa3efbddfb8 (diff)
parent49f30189ef9243a9ed863d62c6673e381139bd79 (diff)
Merge pull request #231 from Panky-codes/feature/add-stop-watch
Added stopwatch
Diffstat (limited to 'src/displayapp/screens/StopWatch.h')
-rw-r--r--src/displayapp/screens/StopWatch.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/displayapp/screens/StopWatch.h b/src/displayapp/screens/StopWatch.h
new file mode 100644
index 00000000..f9dd5c76
--- /dev/null
+++ b/src/displayapp/screens/StopWatch.h
@@ -0,0 +1,86 @@
+#pragma once
+
+#include "Screen.h"
+#include "components/datetime/DateTimeController.h"
+#include "../LittleVgl.h"
+
+#include "FreeRTOS.h"
+#include "portmacro_cmsis.h"
+
+#include <array>
+
+namespace Pinetime::Applications::Screens {
+
+ enum class States { Init, Running, Halted };
+
+ enum class Events { Play, Pause, Stop };
+
+ struct TimeSeparated_t {
+ int mins;
+ int secs;
+ int msecs;
+ };
+
+ // A simple buffer to hold the latest two laps
+ template <int N> 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<TimeSeparated_t, N> buffer;
+ uint8_t currentSize;
+ uint8_t capacity;
+ int8_t head;
+ };
+
+ class StopWatch : public Screen {
+ public:
+ StopWatch(DisplayApp* app);
+ ~StopWatch() override;
+ bool Refresh() override;
+ bool OnButtonPushed() override;
+ void playPauseBtnEventHandler(lv_event_t event);
+ void stopLapBtnEventHandler(lv_event_t event);
+
+ private:
+ bool running;
+ States currentState;
+ Events currentEvent;
+ TickType_t startTime;
+ TickType_t oldTimeElapsed;
+ TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs
+ LapTextBuffer_t<2> lapBuffer;
+ int lapNr;
+ bool lapPressed;
+ lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
+ lv_obj_t *lapOneText, *lapTwoText;
+ };
+}