ref: 0ce98c7ac7ba66acaf504be9bb042796e12f2733
src/displayapp/screens/StopWatch.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 hundredths; }; // 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; 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; }; } |