2021-03-11 04:54:14 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Screen.h"
|
|
|
|
#include "components/datetime/DateTimeController.h"
|
|
|
|
#include "../LittleVgl.h"
|
|
|
|
|
2021-03-11 05:56:58 -05:00
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "portmacro_cmsis.h"
|
2021-03-11 04:54:14 -05:00
|
|
|
|
2021-03-12 14:24:53 -05:00
|
|
|
#include <array>
|
|
|
|
|
2021-03-11 04:54:14 -05:00
|
|
|
namespace Pinetime::Applications::Screens {
|
|
|
|
|
|
|
|
enum class States { INIT, RUNNING, HALTED };
|
|
|
|
|
|
|
|
enum class Events { PLAY, PAUSE, STOP };
|
|
|
|
|
2021-03-12 03:43:13 -05:00
|
|
|
struct TimeSeparated_t {
|
|
|
|
int mins;
|
|
|
|
int secs;
|
|
|
|
int msecs;
|
|
|
|
};
|
|
|
|
|
2021-03-13 08:53:37 -05:00
|
|
|
// A simple buffer to hold the latest two laps
|
2021-03-12 14:24:53 -05:00
|
|
|
template <int N> struct LapTextBuffer_t {
|
|
|
|
LapTextBuffer_t() : _arr {}, currentSz {}, capacity {N}, head {-1} {
|
|
|
|
}
|
|
|
|
|
|
|
|
void addLaps(const TimeSeparated_t& timeVal) {
|
2021-03-13 07:59:54 -05:00
|
|
|
head++;
|
2021-03-12 14:24:53 -05:00
|
|
|
head %= capacity;
|
2021-03-13 07:59:54 -05:00
|
|
|
_arr[head] = timeVal;
|
2021-03-12 14:24:53 -05:00
|
|
|
|
|
|
|
if (currentSz < capacity) {
|
|
|
|
currentSz++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 07:59:54 -05:00
|
|
|
void clearBuffer() {
|
|
|
|
_arr = {};
|
|
|
|
currentSz = 0;
|
|
|
|
head = -1;
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:24:53 -05:00
|
|
|
TimeSeparated_t* operator[](std::size_t idx) {
|
|
|
|
// Sanity check for out-of-bounds
|
|
|
|
if (idx >= 0 && idx < capacity) {
|
|
|
|
if (idx < currentSz) {
|
2021-03-13 08:53:37 -05:00
|
|
|
// This transformation is to ensure that head is always pointing to index 0.
|
2021-03-13 07:59:54 -05:00
|
|
|
const auto transformed_idx = (head - idx) % capacity;
|
2021-03-12 14:24:53 -05:00
|
|
|
return (&_arr[transformed_idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::array<TimeSeparated_t, N> _arr;
|
|
|
|
uint8_t currentSz;
|
|
|
|
uint8_t capacity;
|
|
|
|
int8_t head;
|
|
|
|
};
|
|
|
|
|
2021-03-11 04:54:14 -05:00
|
|
|
class StopWatch : public Screen {
|
|
|
|
public:
|
2021-03-15 16:35:36 -04:00
|
|
|
StopWatch(DisplayApp* app);
|
2021-03-11 04:54:14 -05:00
|
|
|
~StopWatch() override;
|
|
|
|
bool Refresh() override;
|
|
|
|
bool OnButtonPushed() override;
|
2021-03-11 17:41:24 -05:00
|
|
|
void playPauseBtnEventHandler(lv_event_t event);
|
2021-03-12 03:43:13 -05:00
|
|
|
void stopLapBtnEventHandler(lv_event_t event);
|
2021-03-11 04:54:14 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool running;
|
|
|
|
States currentState;
|
|
|
|
Events currentEvent;
|
2021-03-11 05:56:58 -05:00
|
|
|
TickType_t startTime;
|
2021-03-11 17:41:24 -05:00
|
|
|
TickType_t oldTimeElapsed;
|
2021-03-12 14:24:53 -05:00
|
|
|
TimeSeparated_t currentTimeSeparated; // Holds Mins, Secs, millisecs
|
|
|
|
LapTextBuffer_t<2> lapBuffer;
|
2021-03-12 03:43:13 -05:00
|
|
|
int lapNr;
|
2021-03-12 14:24:53 -05:00
|
|
|
bool lapPressed;
|
2021-03-12 03:43:13 -05:00
|
|
|
lv_obj_t *time, *msecTime, *btnPlayPause, *btnStopLap, *txtPlayPause, *txtStopLap;
|
|
|
|
lv_obj_t *lapOneText, *lapTwoText;
|
2021-03-11 04:54:14 -05:00
|
|
|
};
|
2021-03-15 16:35:36 -04:00
|
|
|
}
|