forked from pimatic/RFControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFControl.cpp
More file actions
237 lines (217 loc) · 5.9 KB
/
RFControl.cpp
File metadata and controls
237 lines (217 loc) · 5.9 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
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
#include "RFControl.h"
#define MAX_RECORDINGS 255
#define STATUS_WAITING 0
#define STATUS_RECORDING 1
#define STATUS_VERIFY 2
#define STATUS_DATA_READY 3
#define MIN_FOOTER_LENGTH 4000
#define MIN_PULSE_LENGTH 100
unsigned int footer_length;
unsigned int timings[MAX_RECORDINGS];
unsigned char state;
int recording_pos;
int recording_size;
int verify_pos;
int interruptPin = -1;
void handleInterrupt();
void RFControl::startReceiving(int _interruptPin)
{
footer_length = 0;
state = STATUS_WAITING;
recording_pos = 0;
recording_size = 0;
verify_pos = 0;
if(interruptPin != -1) {
detachInterrupt(interruptPin);
}
interruptPin = _interruptPin;
attachInterrupt(interruptPin, handleInterrupt, CHANGE);
}
void RFControl::stopReceiving()
{
if(interruptPin != -1) {
detachInterrupt(interruptPin);
}
interruptPin = -1;
}
bool RFControl::hasData()
{
return state == STATUS_DATA_READY;
}
void RFControl::getRaw(unsigned int **buffer, unsigned int* timings_size)
{
*buffer = timings;
*timings_size = recording_size;
}
void RFControl::continueReceiving()
{
state = STATUS_WAITING;
}
bool probablyFooter(unsigned int duration) {
return duration >= MIN_FOOTER_LENGTH;
}
bool matchesFooter(unsigned int duration)
{
unsigned int footer_delta = footer_length/4;
return (footer_length - footer_delta < duration && duration < footer_length + footer_delta);
}
void startRecording(unsigned int duration)
{
#ifdef RF_CONTROL_SIMULATE_ARDUINO
printf(" => start recoding");
#endif
footer_length = duration;
recording_pos = 0;
state = STATUS_RECORDING;
}
void startVerify()
{
#ifdef RF_CONTROL_SIMULATE_ARDUINO
printf(" => start verify (recording_size=%i)", recording_pos);
#endif
state = STATUS_VERIFY;
recording_size = recording_pos;
verify_pos = 0;
}
void handleInterrupt()
{
static unsigned long lastTime;
long currentTime = micros();
unsigned int duration = currentTime - lastTime;
lastTime = currentTime;
#ifdef RF_CONTROL_SIMULATE_ARDUINO
printf("%s: recording=%i verify=%i duration=%i", sate2string[state], recording_pos, verify_pos, duration);
#endif
switch(state) {
case STATUS_WAITING:
if(probablyFooter(duration))
{
startRecording(duration);
}
break;
case STATUS_RECORDING:
{
if(matchesFooter(duration))
{
timings[recording_pos] = duration;
recording_pos++;
//If we have at least recorded 32 values:
if(recording_pos >= 32) {
startVerify();
} else {
// so seems it was not a footer, just a pulse of the regular recording,
startRecording(duration);
}
} else {
if(duration > MIN_PULSE_LENGTH)
{
if(duration > footer_length) {
startRecording(duration);
} else if(recording_pos < MAX_RECORDINGS-1) {
timings[recording_pos] = duration;
recording_pos++;
} else {
state = STATUS_WAITING;
}
} else {
state = STATUS_WAITING;
}
}
}
break;
case STATUS_VERIFY:
{
unsigned int refVal = timings[verify_pos];
unsigned int delta = refVal/4 + refVal/8;
if(refVal - delta < duration && duration < refVal + delta)
{
verify_pos++;
if(verify_pos == recording_size) {
state = STATUS_DATA_READY;
} else {
// keep recording parallel for the case verification fails.
if(recording_pos < MAX_RECORDINGS-1) {
timings[recording_pos] = duration;
recording_pos++;
}
}
} else {
if(probablyFooter(duration)) {
// verification failed but it could be a footer, try to verify again with the
// parallel recorded results
if(recording_pos < MAX_RECORDINGS-1) {
timings[recording_pos] = duration;
recording_pos++;
startVerify();
} else {
state = STATUS_WAITING;
}
} else {
state = STATUS_WAITING;
}
}
}
break;
}
#ifdef RF_CONTROL_SIMULATE_ARDUINO
printf("\n");
#endif
}
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;
}
void RFControl::sendByTimings(int transmitterPin, unsigned int *timings, unsigned int timings_size, unsigned int repeats) {
pinMode(transmitterPin, OUTPUT);
for(unsigned int i = 0; i < repeats; i++) {
digitalWrite(transmitterPin, LOW);
int state = LOW;
for(unsigned int j = 0; j < timings_size; j++) {
state = !state;
digitalWrite(transmitterPin, state);
delayMicroseconds(timings[j]);
}
}
digitalWrite(transmitterPin, LOW);
}