InfiniTime/src/displayapp/screens/StopWatch.h

92 lines
2.2 KiB
C
Raw Normal View History

#pragma once
#include "Screen.h"
#include "components/datetime/DateTimeController.h"
#include "../LittleVgl.h"
#include "FreeRTOS.h"
#include "portmacro_cmsis.h"
#include <array>
#include "systemtask/SystemTask.h"
namespace Pinetime::Applications::Screens {
2021-03-20 21:42:13 +00:00
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 <int N> struct LapTextBuffer_t {
2021-03-20 21:42:13 +00:00
LapTextBuffer_t() : buffer {}, currentSize {}, capacity {N}, head {-1} {
}
void addLaps(const TimeSeparated_t& timeVal) {
2021-03-13 12:59:54 +00:00
head++;
head %= capacity;
2021-03-20 21:42:13 +00:00
buffer[head] = timeVal;
2021-03-20 21:42:13 +00:00
if (currentSize < capacity) {
currentSize++;
}
}
2021-03-13 12:59:54 +00:00
void clearBuffer() {
2021-03-20 21:42:13 +00:00
buffer = {};
currentSize = 0;
2021-03-13 12:59:54 +00:00
head = -1;
}
TimeSeparated_t* operator[](std::size_t idx) {
// Sanity check for out-of-bounds
if (idx >= 0 && idx < capacity) {
2021-03-20 21:42:13 +00:00
if (idx < currentSize) {
// This transformation is to ensure that head is always pointing to index 0.
2021-03-13 12:59:54 +00:00
const auto transformed_idx = (head - idx) % capacity;
2021-03-20 21:42:13 +00:00
return (&buffer[transformed_idx]);
}
}
return nullptr;
}
private:
2021-03-20 21:42:13 +00:00
std::array<TimeSeparated_t, N> buffer;
uint8_t currentSize;
uint8_t capacity;
int8_t head;
};
class StopWatch : public Screen {
public:
StopWatch(DisplayApp* app, System::SystemTask& systemTask);
~StopWatch() override;
2021-07-19 13:26:12 +00:00
void Refresh() override;
2021-03-11 22:41:24 +00:00
void playPauseBtnEventHandler(lv_event_t event);
void stopLapBtnEventHandler(lv_event_t event);
bool OnButtonPushed() override;
void reset();
void start();
void pause();
private:
Pinetime::System::SystemTask& systemTask;
TickType_t timeElapsed;
States currentState;
TickType_t startTime;
2021-03-11 22:41:24 +00:00
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 *lapOneText, *lapTwoText;
2021-07-19 13:26:12 +00:00
lv_task_t* taskRefresh;
};
2021-03-15 20:35:36 +00:00
}