forked from pimatic/RFControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFControl.cpp
415 lines (374 loc) · 10.8 KB
/
RFControl.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "RFControl.h"
#ifndef RF_CONTROL_VARDUINO
#include "arduino_functions.h"
#else
#define byte uint8_t
#endif
// Scale down time by 4 to fit in 16 bit unsigned int
#define PULSE_LENGTH_DIVIDER 4
// Noise filter
#define MIN_MSG_LEN 16
// Max length of data pulse. Longer pulses are treated as sync.
#define MAX_PULSE_PERIODS 20
// Minimum signal period time for a proper message
#define MIN_PERIOD_TIME (120 / PULSE_LENGTH_DIVIDER)
// Remembers the time of the last interrupt
volatile unsigned int lastTime;
// Pulse period time of message being received
volatile unsigned int periodTime;
// Pulse counter for message being received
volatile byte streak;
// Buffer pointer where the next message will be stored
volatile byte writer;
// Buffer pointer for the message reader
volatile byte reader;
// Circular message buffer (256) and extended raw buffer
volatile unsigned int msgbuf[MAX_RECORDINGS];
// Interrupt Service Routine
void isr();
// RF GPIO IN
int interruptPin = -1;
// Latest pulse time
volatile unsigned int duration;
// Indicates the availability of a pulse time to read in raw mode
volatile bool new_duration = false;
unsigned int RFControl::getPulseLengthDivider() {
return PULSE_LENGTH_DIVIDER;
}
void RFControl::startReceiving(int _interruptPin) {
lastTime = hw_micros() / PULSE_LENGTH_DIVIDER;
periodTime = 0;
writer = 0;
reader = 0;
streak = 0;
if(interruptPin != -1) {
hw_detachInterrupt(interruptPin);
}
interruptPin = _interruptPin;
hw_attachInterrupt(interruptPin, isr);
}
void RFControl::stopReceiving() {
if(interruptPin != -1) {
hw_detachInterrupt(interruptPin);
}
interruptPin = -1;
}
bool RFControl::hasData() {
return writer != reader;
}
/* Message capture writes to a circular buffer, but the RFControl API
is not compatible with such a construction. getRaw() unfolds the
message into a linear sequence in memory. The circular buffer uses
256 words, but msgbuf is typically larger than that. If the message
wraps around, the words beyond index 255 is used for the unfolded
message.
A captured message starts with a one word header containing the
average period time. After that follows the number of periods for
each pulse in the message. getRaw() removes the header and
multiplies the pulse periods with the period time to covert to the
correct RFControl API message format.
*/
void RFControl::getRaw(unsigned int **buffer, unsigned int* timings_size) {
static unsigned int size;
unsigned int start = reader;
unsigned int max_size = MAX_RECORDINGS - reader;
byte pos = 0;
size = 0;
if (reader != writer) {
unsigned int pt = msgbuf[(byte)(reader + pos++)];
while ((byte)(reader+pos) != writer && msgbuf[(byte)(reader+pos)] <= MAX_PULSE_PERIODS && size < max_size) {
msgbuf[start + size++] = pt * msgbuf[(byte)(reader + pos++)];
}
if (size >= max_size) {
// Unable to fit message from circular buffer in flat buffer. Drop message
size = 0;
}
else if ((reader+pos) != writer) {
// Include sync at end
msgbuf[start + size++] = pt * msgbuf[(byte)(reader + pos++)];
}
}
*timings_size = size;
*buffer = (unsigned int*)&msgbuf[start];
}
void RFControl::continueReceiving() {
if (reader != writer) {
// Go to next message
reader++;
while (reader != writer && msgbuf[reader] <= MAX_PULSE_PERIODS) {
reader++;
}
if (reader != writer) {
// Include sync at end
reader++;
}
}
}
unsigned int RFControl::getLastDuration(){
new_duration = false;
return duration;
}
bool RFControl::existNewDuration(){
return new_duration;
}
void isr()
{
unsigned int now = hw_micros() / PULSE_LENGTH_DIVIDER;
unsigned int pulseTime = now - lastTime;
unsigned int periods = (pulseTime + periodTime/2) / periodTime;
byte lowPulse = digitalRead(interruptPin + 2);
lastTime = now;
duration = pulseTime;
new_duration = true;
if (periods == 0) {
// Noise, ignore message
streak = 0;
}
if (streak > 0) {
// Receive message
byte index = (writer + streak++);
if (index == reader) {
// Reception buffer is full, drop message
streak = 0;
}
else {
msgbuf[index] = periods;
}
}
if (lowPulse) {
if (periodTime > MIN_PERIOD_TIME && periods > MAX_PULSE_PERIODS) {
// Sync detected
if (streak > MIN_MSG_LEN) {
// Message complete
msgbuf[writer] = periodTime;
writer = (writer + streak);
}
// Start new message
streak = 1;
}
}
else {
// high pulse
if (periods > MAX_PULSE_PERIODS) {
// Noise, ignore message
streak = 0;
}
if (streak > 0) {
if (periods == 1) {
// Approximate average of single period high pulses in message
periodTime = (periodTime*streak + 2*pulseTime) / (streak + 2);
}
}
else {
// Initiate search for new period time and sync
periodTime = pulseTime;
}
}
}
bool RFControl::compressTimings(unsigned int buckets[8], unsigned int *timings, unsigned int timings_size) {
for(int j = 0; j < 8; j++ ) {
buckets[j] = 0;
}
unsigned long sums[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned int counts[8] = {0, 0, 0, 0, 0, 0, 0, 0};
//sort timings into buckets, handle max 8 different pulse length
for(unsigned int i = 0; i < timings_size; i++)
{
int j = 0;
for(; j < 8; j++) {
unsigned int refVal = buckets[j];
unsigned int val = timings[i];
//if bucket is empty
if(refVal == 0) {
//sort into bucket
buckets[j] = val;
timings[i] = j;
sums[j] += val;
counts[j]++;
break;
} else {
//check if bucket fits:
unsigned int delta = refVal/4 + refVal/8;
if(refVal - delta < val && val < refVal + delta) {
timings[i] = j;
sums[j] += val;
counts[j]++;
break;
}
}
//try next..
}
if(j == 8) {
//we have not found a bucket for this timing, exit...
return false;
}
}
for(int j = 0; j < 8; j++) {
if(counts[j] != 0) {
buckets[j] = sums[j] / counts[j];
}
}
return true;
}
bool RFControl::compressTimingsAndSortBuckets(unsigned int buckets[8], unsigned int *timings, unsigned int timings_size) {
//clear buckets
for(int j = 0; j < 8; j++ ) {
buckets[j] = 0;
}
//define arrays too calc the average value from the buckets
unsigned long sums[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned int counts[8] = {0, 0, 0, 0, 0, 0, 0, 0};
//sort timings into buckets, handle max 8 different pulse length
for(unsigned int i = 0; i < timings_size; i++)
{
int j = 0;
//timings need only there to load
unsigned int val = timings[i];
for(; j < 8; j++) {
unsigned int refVal = buckets[j];
//if bucket is empty
if(refVal == 0) {
//sort into bucket
buckets[j] = val;
sums[j] += val;
counts[j]++;
break;
} else {
//check if bucket fits:
//its allowed round about 37,5% diff
unsigned int delta = refVal/4 + refVal/8;
if(refVal - delta < val && val < refVal + delta) {
sums[j] += val;
counts[j]++;
break;
}
}
//try next..
}
if(j == 8) {
//we have not found a bucket for this timing, exit...
return false;
}
}
//calc the average value from the buckets
for(int j = 0; j < 8; j++) {
if(counts[j] != 0) {
buckets[j] = sums[j] / counts[j];
}
}
//buckets are defined
//lets scramble a little bit
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 7; j++) {
if(buckets[j] > buckets[j+1]){
unsigned int temp = buckets[j];
buckets[j] = buckets[j+1];
buckets[j+1] = temp;
}
}
}
// now the buckets are ordered by size from low to high.
// but the zero ist first. lets move this back
// find first value
int first = 0;
for(int i = 0; i < 8; i++){
if(buckets[i] != 0){
first = i;
break;
}
}
//copy buckets to the start of the array
int end = 8 - first;
for(int i = 0; i < end; i++){
buckets[i] = buckets[first];
buckets[first] = 0;
first++;
}
//and now we can assign the timings with the position_value from the buckets
//pre calc ref values. save time
unsigned int ref_Val_h[8];
unsigned int ref_Val_l[8];
for(int i = 0; i<8;i++) {
unsigned int refVal = buckets[i];
//check if bucket fits:
unsigned int delta = refVal/4 + refVal/8;
ref_Val_h[i] = refVal + delta;
ref_Val_l[i] = refVal - delta;
}
for(unsigned int i = 0; i < timings_size; i++)
{
unsigned int val = timings[i];
for(int j = 0; j < 8; j++) {
if(ref_Val_l[j] < val && val < ref_Val_h[j]) {
timings[i] = j;
break;
}
}
}
return true;
}
void listenBeforeTalk()
{
// listen before talk
unsigned long waited = 0;
if(interruptPin != -1) {
waited += 500;
hw_delayMicroseconds(500);
while(streak > 0) {
//wait till no rf message is in the air
waited += 5;
hw_delayMicroseconds(3); // 5 - some micros for other stuff
// don't wait longer than 5sec
if(waited > 5000000) {
break;
}
// some delay between the message in air and the new message send
// there could be additional repeats following so wait some more time
if(streak == 0) {
waited += periodTime * MAX_PULSE_PERIODS;
hw_delayMicroseconds(periodTime * MAX_PULSE_PERIODS);
}
}
// stop receiving while sending, this method preserves the recording state
hw_detachInterrupt(interruptPin);
}
}
void afterTalk()
{
// enable reciving again
if(interruptPin != -1) {
hw_attachInterrupt(interruptPin, isr);
}
}
void RFControl::sendByCompressedTimings(int transmitterPin,unsigned long* buckets, char* compressTimings, unsigned int repeats) {
listenBeforeTalk();
unsigned int timings_size = strlen(compressTimings);
hw_pinMode(transmitterPin, OUTPUT);
for(unsigned int i = 0; i < repeats; i++) {
hw_digitalWrite(transmitterPin, LOW);
int state = LOW;
for(unsigned int j = 0; j < timings_size; j++) {
state = !state;
hw_digitalWrite(transmitterPin, state);
unsigned int index = compressTimings[j] - '0';
hw_delayMicroseconds(buckets[index]);
}
}
hw_digitalWrite(transmitterPin, LOW);
afterTalk();
}
void RFControl::sendByTimings(int transmitterPin, unsigned int *timings, unsigned int timings_size, unsigned int repeats) {
listenBeforeTalk();
hw_pinMode(transmitterPin, OUTPUT);
for(unsigned int i = 0; i < repeats; i++) {
hw_digitalWrite(transmitterPin, LOW);
int state = LOW;
for(unsigned int j = 0; j < timings_size; j++) {
state = !state;
hw_digitalWrite(transmitterPin, state);
hw_delayMicroseconds(timings[j]);
}
}
hw_digitalWrite(transmitterPin, LOW);
afterTalk();
}