-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.hpp
65 lines (48 loc) · 1.23 KB
/
timer.hpp
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
#pragma once
#include <limits>
#include <stdexcept>
#include "timer_api.hpp"
class Timer
{
private:
static inline bool timer_availabilities[2] = {true, true};
int id;
unsigned last_loaded = 0;
void init() { timer_init(id); }
void restore() { timer_restore(id); }
void load(unsigned value) { timer_load(id, value); }
unsigned read() { return timer_read(id); }
explicit Timer(int id) : id{id} { init(); }
public:
static constexpr unsigned int ticks_per_ms = 32;
static Timer make_timer()
{
for (int i = 0; i < 2; i++)
{
if (timer_availabilities[i])
{
timer_availabilities[i] = false;
return Timer{i};
}
}
throw std::runtime_error("No more timers available");
}
~Timer()
{
restore();
timer_availabilities[id] = true;
}
void set_raw(unsigned value) { load(value); }
void set_ms(unsigned ms) { load(ms * ticks_per_ms); }
unsigned get_raw() { return read(); }
unsigned get_ms() { return read() / ticks_per_ms; }
};
class Stopwatch
{
private:
Timer timer;
public:
Stopwatch() : timer{Timer::make_timer()} {};
void start() { timer.set_raw(std::numeric_limits<uint32_t>::max()); }
double get_ms() { return (double)(std::numeric_limits<uint32_t>::max() - timer.get_raw()) / Timer::ticks_per_ms; }
};