-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio_button_manager.cpp
More file actions
196 lines (172 loc) · 5.37 KB
/
gpio_button_manager.cpp
File metadata and controls
196 lines (172 loc) · 5.37 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "gpio_button_manager.hpp"
#include <poll.h>
#include <errno.h>
#include <signal.h>
#include <cstring>
#include "gpio_button.hpp"
#include "log.hpp"
#include "config.h"
const char GpioButtonManager::EXIT;
const char GpioButtonManager::BUTTON_CHANGED;
const char GpioButtonManager::BUTTON_LIST_CHANGED;
std::mutex GpioButtonManager::_mut;
GpioButtonManager* GpioButtonManager::_instance = NULL;
std::map<int, GpioButton*> GpioButtonManager::_btns;
bool GpioButtonManager::add(GpioButton *btn) {
if(btn == NULL) {
return false;
}
{ //mutex scope
const std::lock_guard<std::mutex> lock(_mut);
int pin = btn->_pin;
if(_btns.count(pin) != 0) {
return false;
}
if(_instance == NULL) {
_instance = new GpioButtonManager();
}
_btns[pin] = btn;
}
_instance->_pipe.send(GpioButtonManager::BUTTON_LIST_CHANGED);
return true;
}
void GpioButtonManager::remove(GpioButton *btn) {
if((btn == NULL) || (_instance == NULL)) {
return;
}
GpioButtonManager *toDelete = NULL;
{ //mutex scope
const std::lock_guard<std::mutex> lock(_mut);
std::map<int, GpioButton*>::iterator i = _btns.find(btn->_pin);
if(i == _btns.end()) {
return;
}
_btns.erase(i);
if(_btns.empty()) {
toDelete = _instance;
_instance = NULL;
}
}
if(toDelete != NULL) {
delete toDelete;
}
else {
_instance->_pipe.send(GpioButtonManager::BUTTON_LIST_CHANGED);
}
}
void GpioButtonManager::onButtonTimerChanged() {
if(_instance == NULL) {
return;
}
_instance->_pipe.send(GpioButtonManager::BUTTON_CHANGED);
}
GpioButtonManager::GpioButtonManager() {
_timerFd = timerfd_create(CLOCK_MONOTONIC, 0);
if(_timerFd == -1) {
log(LOG_ERR, "unable to create timer for debouncing: %s", strerror(errno));
return;
}
_initTimerFd();
pthread_create(&_thread, NULL, GpioButtonManager::_startRun, (void*)this);
}
GpioButtonManager::~GpioButtonManager() {
_pipe.send(GpioButtonManager::EXIT);
//wait the thread for 3sec
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 3;
int joined = pthread_timedjoin_np(_thread, NULL, &ts);
if(joined != 0) {
log(LOG_ERR, "unable to join the button manager thread");
}
if(_timerFd != -1){
close(_timerFd);
}
}
void GpioButtonManager::_initTimerFd() {
_interval.it_value.tv_sec = DEBOUNCE_READ_DELAY / 1000000;
_interval.it_value.tv_nsec = (DEBOUNCE_READ_DELAY % 1000000)* 1000;
_interval.it_interval.tv_sec = DEBOUNCE_READ_DELAY / 1000000;
_interval.it_interval.tv_nsec = (DEBOUNCE_READ_DELAY % 1000000)* 1000;
timerfd_settime(_timerFd, 0, &_interval, NULL);
}
void* GpioButtonManager::_startRun(void *manager) {
signal(SIGCHLD,SIG_DFL); // A child process dies
signal(SIGTSTP,SIG_IGN); // Various TTY signals
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP, SIG_IGN); // Ignore hangup signal
signal(SIGINT,SIG_IGN); // ignore SIGTERM
signal(SIGQUIT,SIG_IGN); // ignore SIGTERM
signal(SIGTERM,SIG_IGN); // ignore SIGTERM
int oldstate;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
((GpioButtonManager*)manager)->_run();
return NULL;
}
void GpioButtonManager::_run(){
pollfd fdList[66]; //max 64 pins + event + timerfd
_resetLocalList();
int fdCount = _initFdList(fdList);
while(1) {
if(-1 == poll(fdList, fdCount, -1)) {
log(LOG_ERR, "unable to listen buttons fd: %s", strerror(errno));
return;
}
if(fdList[0].revents == POLLIN) {
char msg = _pipe.read();
if(msg == GpioButtonManager::EXIT){
return;
}
else if(msg == GpioButtonManager::BUTTON_LIST_CHANGED) {
_resetLocalList();
}
fdCount = _initFdList(fdList);
}
if(fdList[1].revents == POLLIN) { // tick
_clearTimer(_timerFd);
//all debouncing buttons have to read their value
for(GpioButton* btn : _localBtns) {
btn->_update();
}
}
for(int i = 2; i < fdCount; i++) {
if(fdList[i].revents == POLLIN) {
_clearTimer(fdList[i].fd);
for(GpioButton* btn : _localBtns) {
if(btn->_timerFd == fdList[i].fd) {
btn->_onDelay();
}
}
}
}
}
}
int GpioButtonManager::_initFdList(pollfd *fdList) {
memset(fdList, 0, sizeof(pollfd)*66);
int fdCount = 2;
fdList[0].fd = _pipe.getReadFd();
fdList[0].events = POLLIN;
fdList[1].fd = _timerFd;
fdList[1].events = POLLIN;
for(GpioButton* btn : _localBtns) {
if(btn->_timerFd != -1) {
fdList[fdCount].fd = btn->_timerFd;
fdList[fdCount].events = POLLIN;
++fdCount;
}
}
return fdCount;
}
void GpioButtonManager::_resetLocalList() {
std::lock_guard<std::mutex> lock(_mut);
_localBtns.resize(_btns.size());
int i = 0;
for(std::pair<int, GpioButton*> pair : _btns) {
_localBtns[i++] = pair.second;
}
}
void GpioButtonManager::_clearTimer(int timerFd) {
uint64_t data;
read(timerFd, &data, sizeof(uint64_t));
}