-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonpoll.cpp
92 lines (76 loc) · 1.79 KB
/
buttonpoll.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
#include "buttonpoll.h"
#include <iostream>
#include <chrono>
#include <thread>
#include <unistd.h> // lseek()
#include <poll.h> // poll()
ButtonPoll::~ButtonPoll()
{
for(auto &button : _buttons)
close(button._fd);
}
void ButtonPoll::addButton(int pin, TriggerEdge edge)
{
_buttons.push_back(Button(pin, static_cast<int>(edge)));
}
bool ButtonPoll::isButtonPressed()
{
return !_pressed_queue.empty();
}
int ButtonPoll::getNextPressedPin()
{
if(_pressed_queue.empty())
return -1;
int pin = _buttons.at(_pressed_queue.front())._gpio_pin;
_pressed_queue.pop();
return pin;
}
void ButtonPoll::start()
{
const int BUTTON_COUNT = _buttons.size();
std::vector<bool> first_interrupt(BUTTON_COUNT, true);
std::chrono::milliseconds timeout{700};
while(true)
{
struct pollfd *fdset = new pollfd[BUTTON_COUNT];
for(int i=0; i<BUTTON_COUNT; i++)
{
fdset[i].fd = _buttons[i]._fd;
fdset[i].events = POLLPRI;
}
// blocking until at least one fd ready, or timeout
int ready_count = poll(fdset, BUTTON_COUNT, timeout.count());
if(ready_count < 0)
{
std::cout << std::endl << "poll() failed!" << std::endl;
goto NEXT_POLL;
}
if(ready_count == 0)
{
std::cout << ".";
goto NEXT_POLL;
}
for(int i=0; i<BUTTON_COUNT; i++)
{
if(fdset[i].revents & POLLPRI)
{
lseek(fdset[i].fd, 0, SEEK_SET);
const int BUFFER_SIZE{1};
int buf[BUFFER_SIZE];
read(fdset[i].fd, &buf, BUFFER_SIZE);
// do nothing for 1st interrupt
if(first_interrupt.at(i))
{
first_interrupt[i] = false;
continue;
}
std::cout << std::endl << "Button " << i << "pressed" << std::endl;
_pressed_queue.push(i);
}
}
NEXT_POLL:
std::cout.flush(); // print all cout buffer
delete[] fdset;
std::this_thread::sleep_for(std::chrono::seconds(1) - timeout);
}
}