-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.cpp
210 lines (187 loc) · 4.86 KB
/
led.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "led.hpp"
#include <signal.h>
#include <errno.h>
#include <cstring>
#include "log.hpp"
const char Led::ON;
const char Led::OFF;
const char Led::BLINK_SLOWLY;
const char Led::BLINK_QUICKLY;
const char Led::BLINK_NUMBER;
const char Led::QUIT;
const long Led::SLOW_TIME;
const long Led::QUICK_TIME;
const long Led::NUMBER_TIME;
Led::Led(int ledPin): _pin(ledPin) {
_isOn = false;
_status = Led::OFF;
_blinkCount = 0;
}
Led::~Led() {
_stopBlinking();
_light(false);
_pin.write(Gpio::low);
}
void Led::on() {
const std::lock_guard<std::mutex> lock(_mut);
if(_status == Led::ON){
return;
}
_stopBlinking();
_status = Led::ON;
_light(true);
}
void Led::off() {
const std::lock_guard<std::mutex> lock(_mut);
if(_status == Led::OFF){
return;
}
_stopBlinking();
_status = Led::OFF;
_light(false);
}
void Led::blinkSlowly() {
const std::lock_guard<std::mutex> lock(_mut);
if(_status == Led::BLINK_SLOWLY){
return;
}
if(_blinking()){
_blinkCount = 0;
_status = Led::BLINK_SLOWLY;
_pipe.send(Led::BLINK_SLOWLY);
}
else{
_status = Led::BLINK_SLOWLY;
pthread_create(&_thread, NULL, Led::_startBlinking, (void*)this);
}
}
void Led::blinkQuickly() {
const std::lock_guard<std::mutex> lock(_mut);
if(_status == Led::BLINK_QUICKLY){
return;
}
if(_blinking()){
_blinkCount = 0;
_status = Led::BLINK_QUICKLY;
_pipe.send(Led::BLINK_QUICKLY);
}
else{
_status = Led::BLINK_QUICKLY;
pthread_create(&_thread, NULL, Led::_startBlinking, (void*)this);
}
}
void Led::blinkNumber(unsigned int number) {
const std::lock_guard<std::mutex> lock(_mut);
_blinkNumber = number;
_blinkCount = _isOn ? 0 : 1;
if(_status == Led::BLINK_NUMBER){
return;
}
if(_blinking()){
_status = Led::BLINK_NUMBER;
_pipe.send(Led::BLINK_NUMBER);
}
else{
_status = Led::BLINK_NUMBER;
pthread_create(&_thread, NULL, Led::_startBlinking, (void*)this);
}
}
void Led::_light(bool value){
if(_isOn == value){
return;
}
_pin.write(value ? Gpio::high : Gpio::low);
_isOn = value;
}
bool Led::_blinking(){
return (_status == Led::BLINK_SLOWLY || _status == Led::BLINK_QUICKLY || _status == Led::BLINK_NUMBER);
}
void Led::_stopBlinking() {
if(!_blinking()){
return;
}
_pipe.send(Led::QUIT);
//wait thread for 3sec
timespec ts;
if(clock_gettime(CLOCK_REALTIME, &ts) == -1) {
log(LOG_ERR, "clock gettime failed");
}
else{ ts.tv_sec += 3;
int joined = pthread_timedjoin_np(_thread, NULL, &ts);
if(joined != 0) {
log(LOG_ERR, "unable to join the thread");
}
}
}
void* Led::_startBlinking(void*led){
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);
((Led*)led)->_blink();
return NULL;
}
long Led::_getBlinkDelay() {
switch(_status) {
case Led::BLINK_SLOWLY: return Led::SLOW_TIME;
case Led::BLINK_QUICKLY: return Led::QUICK_TIME;
case Led::BLINK_NUMBER:
return Led::NUMBER_TIME * ((0 == _blinkCount) ? 2 : 1);
default:
return 1000;
}
}
void Led::_blink(){
_blinkCount = 0;
long time = _getBlinkDelay();
_light(!_isOn);
bool exit = false;
while(!exit) {
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
FD_SET(_pipe.getReadFd(), &rfds);
tv.tv_sec = 0;
tv.tv_usec = time;
retval = select(_pipe.getReadFd()+1, &rfds, NULL, NULL, &tv);
if(retval == -1) {
log(LOG_ERR, "select() failed");
exit = true;
}
else if (!retval) { // timeout
time = _getBlinkDelay();
_light(!_isOn);
if(_status == Led::BLINK_NUMBER) {
++_blinkCount;
if(_blinkNumber*2 <= _blinkCount) {
_blinkCount = 0;
}
}
}
else {
char value = _pipe.read();
if(value == 0) {
exit = true;
}
else if(value == Led::QUIT){
exit = true;
}
else{
time = _getBlinkDelay();
if(tv.tv_usec > time){
time = tv.tv_usec - time;
}
else{
_light(!_isOn);
}
}
}
}
}