-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcadenceio.cpp
More file actions
142 lines (110 loc) · 3.06 KB
/
cadenceio.cpp
File metadata and controls
142 lines (110 loc) · 3.06 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "cadenceio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <QDebug>
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
#define POLL_TIMEOUT (10 * 1000) /* 10 seconds */
CadenceIO::CadenceIO(QObject *parent) : QThread(parent)
{
exportGpio(5);
m_timer.setInterval(5000);
m_timer.start();
m_timer.moveToThread(this);
connect(&m_timer, &QTimer::timeout, this, [&]() {
emit cadence(0);
});
}
void CadenceIO::exportGpio(int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/direction");
return;
}
write(fd, "in", 3);
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-edge");
return;
}
write(fd, "falling", 8);
return;
}
int CadenceIO::gpioOpen(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
perror("gpio/fd_open");
return -1;
}
return fd;
}
void CadenceIO::run()
{
struct pollfd fdset[1];
int nfds = 1;
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpio = 5;
int len;
timeout = POLL_TIMEOUT;
gpio_fd = gpioOpen(gpio);
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = gpio_fd;
fdset[0].events = POLLPRI | POLLERR;
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return;
}
if (rc == 0) {
//printf(".");
emit cadence(0.f);
}
if (fdset[0].revents & POLLPRI) {
lseek(fdset[0].fd, 0, SEEK_SET);
len = read(fdset[0].fd, buf, MAX_BUF);
//printf("\npoll() GPIO %d interrupt occurred\n", gpio);
// No one chan havea revulation that is below 250 ms
if (m_int.isValid() && m_int.elapsed() > 250) {
int elapsed = m_int.elapsed();
float c = (1000.f / (float)elapsed) * 60.f;
// Make sure we don't but out to much power
// For some reasone the cadence can be a bit to high
if (c > 175.f) {
c = 175.f;
}
m_timer.start();
emit cadence(c);
}
m_int.restart();
}
if (fdset[0].revents & POLLIN) {
(void)read(fdset[0].fd, buf, 1);
printf("\npoll() stdin read 0x%2.2X\n", buf[0]);
}
fflush(stdout);
}
}