-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
58 lines (48 loc) · 1.02 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
#include <poll.h>
#include <fcntl.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <string>
using namespace std;
int main() {
int x = open("/sys/class/gpio/gpio21/value", O_RDONLY);
if (x == -1) {
cerr << "no good" << endl;
exit(1);
}
int y = open("/sys/class/gpio/gpio20/value", O_RDONLY);
if (y == -1) {
cerr << "no good" << endl;
exit(1);
}
struct pollfd pfds[2];
pfds[0].fd = x;
pfds[0].events = POLLPRI | POLLERR;
pfds[1].fd = y;
pfds[1].events = POLLPRI | POLLERR;
char val;
read(x, &val, 1);
read(y, &val, 1);
string bits_rec;
while(1) {
int ret = poll(pfds, 2, 100);
if (ret == -1) {
cerr << "no good" << endl;
exit(1);
} else if (ret == 0) {
if (bits_rec.length() != 0) {
cout << bits_rec << endl;
bits_rec = "";
}
}
for (int i = 0; i < 2; i++) {
if (pfds[i].revents & POLLPRI) {
lseek(pfds[i].fd, 0, SEEK_SET);
read(pfds[i].fd, &val, 1);
bits_rec += (i == 0) ? "0" : "1";
//cout << "falling: " << i << endl;
}
}
}
}