-
Notifications
You must be signed in to change notification settings - Fork 3
/
KTimer.cxx
71 lines (67 loc) · 1.18 KB
/
KTimer.cxx
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
/*
* A Kittchen Timer
*
*/
#ifndef KTIMER_H
#define KTIMER_H
#include <chrono>
class KTimer
{
public:
KTimer(){};
KTimer(int t){
SetDuration(t);
f_start = std::chrono::system_clock::now();
};
virtual ~KTimer(){};
void SetDuration(int t){
f_duration = std::chrono::milliseconds(t);
f_start = std::chrono::system_clock::now();
};
void Reset(){
f_start = std::chrono::system_clock::now();
};
bool Check(){
auto now = std::chrono::system_clock::now();
auto elapse = (now - f_start);
if (elapse > f_duration) {
Reset();
return true;
} else {return false;}
};
#if 0
bool TryCheck(){
auto now = std::chrono::system_clock::now();
auto elapse = (now - f_start);
if (elapse > f_duration) {
return true;
} else {
return false;
}
};
#endif
protected:
private:
std::chrono::system_clock::time_point f_start;
std::chrono::system_clock::duration f_duration;
};
#endif
#ifdef TEST_MAIN_KTIMER
#include <iostream>
#include <unistd.h>
int main(int argc, char* argv[])
{
KTimer kt;
kt.SetDuration(100);
while (true) {
if (kt.Check()) {
std::cout << "x" << std::flush;
//break;
} else {
std::cout << ".";
}
usleep(1000);
}
return 0;
}
#endif