-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
106 lines (86 loc) · 1.78 KB
/
main.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//#define DEBUG
#include <cstdio>
#include <iostream>
#include <wiringPi.h>
#include <unistd.h>
#include <chrono>
#include <ctime>
#define PIN 8
double timeoutTime = 60.0;
auto start = std::chrono::system_clock::now();
bool changing = false;
bool countTime = false;
void ScreenTurnOff()
{
system("vcgencmd display_power 0 >> /dev/null");
}
void ScreenTurnOn()
{
system("vcgencmd display_power 1 >> /dev/null");
}
void PinValueChanged()
{
if (!digitalRead(PIN))
{
#ifdef DEBUG
std::cout << "PIN value: " << digitalRead(PIN) << std::endl;
std::cout << "Detected" << std::endl;
#endif // DEBUG
ScreenTurnOn();
#ifdef DEBUG
std::cout << "Screen turned on" << std::endl;
#endif // DEBUG
countTime = false;
}
else
{
#ifdef DEBUG
std::cout << "PIN value: " << digitalRead(PIN) << std::endl;
std::cout << "Undetected" << std::endl;
#endif // DEBUG
while (changing) {};
changing = true;
start = std::chrono::system_clock::now();
changing = false;
countTime = true;
}
}
int main(int argc, char* argv[])
{
wiringPiSetup();
pinMode(PIN, INPUT);
ScreenTurnOff();
#ifdef DEBUG
std::cout << "Screen turned off" << std::endl;
#endif // DEBUG
if (argc > 1)
{
double setTimeTimeoutTime = (double) atoi(argv[1]);
if (setTimeTimeoutTime != 0.0)
{
timeoutTime = setTimeTimeoutTime;
}
}
// Bind to interrupt
wiringPiISR(PIN, INT_EDGE_BOTH, &PinValueChanged);
while(1)
{
if (countTime && !changing)
{
changing = true;
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsedSeconds = end - start;
changing = false;
if (elapsedSeconds.count() > timeoutTime)
{
ScreenTurnOff();
#ifdef DEBUG
std::cout << "Screen turned off" << std::endl;
#endif // DEBUG
countTime = false;
}
}
sleep(1);
}
return 0;
}