-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
89 lines (77 loc) · 2.02 KB
/
main.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef _MAIN_H_INCLUDED_
#define _MAIN_H_INCLUDED_
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <iostream>
#include <time.h>
#include <wiringPi.h>
#include <cmath>
#include <thread>
// declare global variables :DDD
bool stop_program = false;
void stopProgram ()
{
stop_program = true;
}
void sig_handler(int signo)
{
if (signo == SIGINT || signo == SIGUSR1) {
stopProgram();
}
}
void registerSignals()
{
if (signal(SIGINT, sig_handler) == SIG_ERR) {
std::cout << "can't catch SIGINT" << std::endl;
stopProgram();
}
if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
std::cout << "can't catch SIGUSER1" << std::endl;
stopProgram();
}
}
class Timer
{
public:
long original_duration = 49220000;
long duration_mod = 0;
struct timespec loop_start, loop_end;
double loop_actual = 0.0;
void onLoopStart()
{
clock_gettime(CLOCK_REALTIME, &loop_start);
}
void onLoopEnd()
{
struct timespec loop_delay = {0, original_duration + duration_mod};
nanosleep(&loop_delay, NULL);
clock_gettime(CLOCK_REALTIME, &loop_end);
loop_actual = (double)(loop_end.tv_sec - loop_start.tv_sec) + (double)loop_end.tv_nsec / 1000000000.0 - (double)loop_start.tv_nsec / 1000000000.0;
}
std::string toDurationString(double seconds, bool includeSeconds)
{
std::string sec = (includeSeconds || seconds < 1) ? std::to_string(std::fmod(round(seconds*100)/100, 60)).substr(0,4) + "s" : "";
std::string duration;
if(seconds < 60) {
duration = sec;
} else if(seconds >= 60 && seconds < 3600) {
duration = std::to_string((int)seconds / 60) + "m " + sec;
} else if(seconds >= 3600) {
duration = std::to_string((int)seconds / 3600) + "h " + std::to_string((int)seconds % 3600 / 60) + "m " + sec;
}
return duration;
}
double roundToMinute(double seconds)
{
int sec = (int)round(seconds);
int mod = sec % 60;
if(mod > 30) {
return sec + 60 - mod;
} else {
return sec - mod;
}
}
};
#endif