-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmonitor.cpp
60 lines (53 loc) · 1014 Bytes
/
monitor.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
#include "header.h"
#ifndef gettime
#define gettime
double getTime()
{
double ret;
struct timeval tv;
gettimeofday(&tv, NULL);
ret = (tv.tv_sec) + tv.tv_usec*1e-6;
return ret;
}
#endif
void setLEDInput (uint8_t id)
{
int fd;
char buf[128];
ssize_t ret;
sprintf(buf, "/sys/class/gpio/gpio%d/direction", id);
fd = open(buf, O_WRONLY);
ret = write(fd, "out", 3);
close(fd);
}
void setLED (uint8_t id, bool value)
{
int fd;
char buf[128];
ssize_t ret;
sprintf(buf, "/sys/class/gpio/gpio%d/value", id);
fd = open(buf, O_WRONLY);
if (value) ret = write(fd, "1", 1);
else ret = write(fd, "0", 1);
close(fd);
}
bool getButtonPress (uint8_t id, bool blocking)
{
int fd;
char buf[128], val;
ssize_t ret;
sprintf(buf, "/sys/class/gpio/gpio%d/value", id);
fd = open(buf, O_RDONLY);
ret = read(fd, &val, 1);
close(fd);
usleep(50000);
while (val!='0' && blocking)
{
usleep(100000);
fd = open(buf, O_RDONLY);
ret = read(fd, &val, 1);
close(fd);
}
close(fd);
return (val=='0');
}