Files
Flipdot-display/src/esp32/logic/stopwatch.h
T

37 lines
753 B
C++
Raw Normal View History

#pragma once
#include <stdint.h>
// The stopwatch of the dynamic pages, started and stopped in the web UI and Home Assistant.
// Times are millis(). Pure logic, tested in test/test_esp32.
class Stopwatch
{
public:
void start(uint32_t now)
{
if (_running)
return;
_running = true;
_started = now;
}
void stop(uint32_t now)
{
if (!_running)
return;
_before += now - _started;
_running = false;
}
// Back to 0, stopped
void reset()
{
_running = false;
_before = 0;
}
bool running() const { return _running; }
uint32_t elapsed(uint32_t now) const { return _before + (_running ? now - _started : 0); }
private:
bool _running = false;
uint32_t _started = 0;
uint32_t _before = 0; // ms counted before the last start
};