-
Notifications
You must be signed in to change notification settings - Fork 105
/
ReceiverIR.cpp
330 lines (315 loc) · 9.66 KB
/
ReceiverIR.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* IR receiver (Version 0.0.4)
*
* Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
* http://shinta.main.jp/
*/
#include "ReceiverIR.h"
#define LOCK()
#define UNLOCK()
#define InRange(x,y) ((((y) * 0.7) < (x)) && ((x) < ((y) * 1.3)))
/**
* Constructor.
*
* @param rxpin Pin for receive IR signal.
*/
ReceiverIR::ReceiverIR(PinName rxpin) : evt(rxpin) {
init_state();
evt.fall(this, &ReceiverIR::isr_fall);
evt.rise(this, &ReceiverIR::isr_rise);
evt.mode(PullUp);
ticker.attach_us(this, &ReceiverIR::isr_wdt, 10 * 1000);
}
/**
* Destructor.
*/
ReceiverIR::~ReceiverIR() {
}
/**
* Get state.
*
* @return Current state.
*/
ReceiverIR::State ReceiverIR::getState() {
LOCK();
State s = work.state;
UNLOCK();
return s;
}
/**
* Get data.
*
* @param format Pointer to format.
* @param buf Buffer of a data.
* @param bitlength Bit length of the buffer.
*
* @return Data bit length.
*/
int ReceiverIR::getData(RemoteIR::Format *format, uint8_t *buf, int bitlength) {
LOCK();
if (bitlength < data.bitcount) {
UNLOCK();
return -1;
}
const int nbits = data.bitcount;
const int nbytes = data.bitcount / 8 + (((data.bitcount % 8) != 0) ? 1 : 0);
*format = data.format;
for (int i = 0; i < nbytes; i++) {
buf[i] = data.buffer[i];
}
init_state();
UNLOCK();
return nbits;
}
void ReceiverIR::init_state(void) {
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
work.state = Idle;
data.format = RemoteIR::UNKNOWN;
data.bitcount = 0;
timer.stop();
timer.reset();
for (int i = 0; i < sizeof(data.buffer); i++) {
data.buffer[i] = 0;
}
}
void ReceiverIR::isr_wdt(void) {
LOCK();
static int cnt = 0;
if ((Idle != work.state) || ((0 <= work.c1) || (0 <= work.c2) || (0 <= work.c3) || (0 <= work.d1) || (0 <= work.d2))) {
cnt++;
if (cnt > 50) {
#if 0
printf("# WDT [c1=%d, c2=%d, c3=%d, d1=%d, d2=%d, state=%d, format=%d, bitcount=%d]\n",
work.c1,
work.c2,
work.c3,
work.d1,
work.d2,
work.state,
data.format,
data.bitcount);
#endif
init_state();
cnt = 0;
}
} else {
cnt = 0;
}
UNLOCK();
}
void ReceiverIR::isr_fall(void) {
LOCK();
switch (work.state) {
case Idle:
if (work.c1 < 0) {
timer.start();
work.c1 = timer.read_us();
} else {
work.c3 = timer.read_us();
int a = work.c2 - work.c1;
int b = work.c3 - work.c2;
if (InRange(a, RemoteIR::TUS_NEC * 16) && InRange(b, RemoteIR::TUS_NEC * 8)) {
/*
* NEC.
*/
data.format = RemoteIR::NEC;
work.state = Receiving;
data.bitcount = 0;
} else if (InRange(a, RemoteIR::TUS_NEC * 16) && InRange(b, RemoteIR::TUS_NEC * 4)) {
/*
* NEC Repeat.
*/
data.format = RemoteIR::NEC_REPEAT;
work.state = Received;
data.bitcount = 0;
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
} else if (InRange(a, RemoteIR::TUS_AEHA * 8) && InRange(b, RemoteIR::TUS_AEHA * 4)) {
/*
* AEHA.
*/
data.format = RemoteIR::AEHA;
work.state = Receiving;
data.bitcount = 0;
} else if (InRange(a, RemoteIR::TUS_AEHA * 8) && InRange(b, RemoteIR::TUS_AEHA * 8)) {
/*
* AEHA Repeat.
*/
data.format = RemoteIR::AEHA_REPEAT;
work.state = Received;
data.bitcount = 0;
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
} else {
init_state();
}
}
break;
case Receiving:
if (RemoteIR::NEC == data.format) {
work.d2 = timer.read_us();
int a = work.d2 - work.d1;
if (InRange(a, RemoteIR::TUS_NEC * 3)) {
data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8));
} else if (InRange(a, RemoteIR::TUS_NEC * 1)) {
data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8));
}
data.bitcount++;
#if 1
/*
* Length of NEC is always 32 bits.
*/
if (32 <= data.bitcount) {
work.state = Received;
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
}
#else
/*
* Set timeout for tail detection automatically.
*/
timeout.detach();
timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_NEC * 5);
#endif
} else if (RemoteIR::AEHA == data.format) {
work.d2 = timer.read_us();
int a = work.d2 - work.d1;
if (InRange(a, RemoteIR::TUS_AEHA * 3)) {
data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8));
} else if (InRange(a, RemoteIR::TUS_AEHA * 1)) {
data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8));
}
data.bitcount++;
#if 0
/*
* Typical length of AEHA is 48 bits.
* Please check a specification of your remote controller if you find a problem.
*/
if (48 <= data.bitcount) {
data.state = Received;
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
}
#else
/*
* Set timeout for tail detection automatically.
*/
timeout.detach();
timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_AEHA * 5);
#endif
} else if (RemoteIR::SONY == data.format) {
work.d1 = timer.read_us();
}
break;
case Received:
break;
default:
break;
}
UNLOCK();
}
void ReceiverIR::isr_rise(void) {
LOCK();
switch (work.state) {
case Idle:
if (0 <= work.c1) {
work.c2 = timer.read_us();
int a = work.c2 - work.c1;
if (InRange(a, RemoteIR::TUS_SONY * 4)) {
data.format = RemoteIR::SONY;
work.state = Receiving;
data.bitcount = 0;
} else {
static const int MINIMUM_LEADER_WIDTH = 150;
if (a < MINIMUM_LEADER_WIDTH) {
init_state();
}
}
} else {
init_state();
}
break;
case Receiving:
if (RemoteIR::NEC == data.format) {
work.d1 = timer.read_us();
} else if (RemoteIR::AEHA == data.format) {
work.d1 = timer.read_us();
} else if (RemoteIR::SONY == data.format) {
work.d2 = timer.read_us();
int a = work.d2 - work.d1;
if (InRange(a, RemoteIR::TUS_SONY * 2)) {
data.buffer[data.bitcount / 8] |= (1 << (data.bitcount % 8));
} else if (InRange(a, RemoteIR::TUS_SONY * 1)) {
data.buffer[data.bitcount / 8] &= ~(1 << (data.bitcount % 8));
}
data.bitcount++;
#if 1
/*
* How do I know the correct length? (6bits, 12bits, 15bits, 20bits...)
* By a model only?
* Please check a specification of your remote controller if you find a problem.
*/
if (32 <= data.bitcount) {
work.state = Received;
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
}
#else
/*
* Set timeout for tail detection automatically.
*/
timeout.detach();
timeout.attach_us(this, &ReceiverIR::isr_timeout, RemoteIR::TUS_SONY * 4);
#endif
}
break;
case Received:
break;
default:
break;
}
UNLOCK();
}
void ReceiverIR::isr_timeout(void) {
LOCK();
#if 0
printf("# TIMEOUT [c1=%d, c2=%d, c3=%d, d1=%d, d2=%d, state=%d, format=%d, bitcount=%d]\n",
work.c1,
work.c2,
work.c3,
work.d1,
work.d2,
work.state,
data.format,
data.bitcount);
#endif
if (work.state == Receiving) {
printf("done\r\n");
work.state = Received;
work.c1 = -1;
work.c2 = -1;
work.c3 = -1;
work.d1 = -1;
work.d2 = -1;
}
UNLOCK();
}