-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFtrx.cpp
More file actions
executable file
·290 lines (222 loc) · 6.95 KB
/
RFtrx.cpp
File metadata and controls
executable file
·290 lines (222 loc) · 6.95 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
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
/*
* RFtrx library v1.0 for Arduino
*
* This library receives and sends RF,IR signals. No protocol decoding is done here.
*
* Copyright 2015 by Arco van Geest <arco@appeltaart.mine.nu>
*
* The purpose of this library is to send and recieve data in manchester or spacelength to support all sort of domotic signals.
*
* 20150107 Initial version
* 20151007 join functions with channel selector
*
* License: GPLv3. See license.txt
*/
#include "RFtrx.h"
unsigned int RFtrx::rxtail[ RADIOCOUNT ];
unsigned int RFtrx::rxenable[ RADIOCOUNT ];
unsigned int RFtrx::rxhead[ RADIOCOUNT ];
unsigned long RFtrx::lastTime[ RADIOCOUNT ];
//unsigned long RFtrx::minStartLength=2500;
//unsigned long RFtrx::maxStartLength=20000;
//unsigned long RFtrx::minPeriodLength=200;
//unsigned long RFtrx::maxPeriodLength=4000;
unsigned long RFtrx::minStartLength=1000;
unsigned long RFtrx::maxStartLength=20000;
unsigned long RFtrx::minPeriodLength=100;
unsigned long RFtrx::maxPeriodLength=3600;
bool RFtrx::activedata[ RADIOCOUNT ];
bool RFtrx::glitch[ RADIOCOUNT ];
unsigned long volatile RFtrx::rxbuffer[ RADIOCOUNT ][ RX_SIZE +4 ];
//constructor
RFtrx::RFtrx() {
// Serial.println("RFtrx constructor");
//long currentTime=micros();
//lastTime=micros();
//rxtail=0;
//rxhead=0;
//minStartLength=3700;
//maxStartLength=20000;
//setMinPeriodLength(150);
//setMaxPeriodLength(2000);
for ( int i=0; i<RADIOCOUNT ; i++) {
RFtrx::activedata[ i ]=0;
RFtrx::glitch[ i ]=0;
RFtrx::rxtail[ i ]=0;
RFtrx::rxenable[ i ]=0;
RFtrx::rxhead[ i ]=0;
RFtrx::lastTime[ i ]=0;
}
}
void RFtrx::initInterrupt(int vector,int channel=0) {
if ( channel == 0) attachInterrupt(vector, receiveInterruptChannel0, CHANGE);
if ( channel == 1) attachInterrupt(vector, receiveInterruptChannel1, CHANGE);
if ( channel == 2) attachInterrupt(vector, receiveInterruptChannel2, CHANGE);
// Serial.println("RFtrx init");
// attachInterrupt(0, receiveInterrupt, CHANGE);
}
void RFtrx::enableReceive(int channel=0) {
rxenable[ channel] =1;
//Serial.print("+");
}
void RFtrx::disableReceive(int channel=0) {
//Serial.print("-");
rxenable[ channel] =0;
}
bool RFtrx::dataready(int channel=0) {
return (( rxhead[channel] == rxtail[channel] ) ?false:true );
}
int RFtrx::dataCount(int channel=0) {
if ( rxhead[ channel] == rxtail[ channel]) return 0;
if ( rxhead[ channel] > rxtail[ channel] ) {
return rxhead[ channel]-rxtail[ channel];
} else {
return RX_SIZE+rxhead[ channel]-rxtail[ channel];
}
}
void RFtrx::setMinStartLength( unsigned long length ) {minStartLength = length;}
void RFtrx::setMaxStartLength( unsigned long length ) {maxStartLength = length;}
void RFtrx::setMinPeriodLength( unsigned long length ) {minPeriodLength = length;}
void RFtrx::setMaxPeriodLength( unsigned long length ) {maxPeriodLength = length;}
long RFtrx::getNext(int channel=0) {
long r=-1;
if ( rxhead[ channel] == rxtail[ channel] ) {
Serial.println(F("ERROR NO DATA READY"));
} else {
r=rxbuffer[ channel ][ rxtail[ channel] ];
rxtail[ channel]++;
if ( rxtail[ channel] >= RX_SIZE ) rxtail[ channel]=0;
}
return r;
}
void RFtrx::receiveInterruptChannel0() {
if ( rxenable[0] == 1 ) RFtrx::receiveInterrupt(0);
}
void RFtrx::receiveInterruptChannel1() {
if ( rxenable[1] == 1 ) RFtrx::receiveInterrupt(1);
}
void RFtrx::receiveInterruptChannel2() {
if ( rxenable[2] == 1 ) RFtrx::receiveInterrupt(2);
}
// receive a frame flank for channel n
void RFtrx::receiveInterrupt(int channel=0) {
long currentTime=micros();
// clock wrap would not happen much
if ( currentTime < RFtrx::lastTime[ channel] ) {
// clock wrap
RFtrx::lastTime[ channel]=currentTime;
RFtrx::activedata[ channel]=0;
return;
}
// the duration of the pulse or space
word duration=currentTime - RFtrx::lastTime[ channel];
//if the pulse is too short it's probably a glitch
if (duration < RFtrx::minPeriodLength) {
// no spikes
glitch[ channel]=1;
return;
}
// glitch stops at first normal length
if ( glitch[ channel] == 1 ) {
//end of spike.
glitch[ channel]=0;
return;
}
// if frame is active but pulse is longer then the max period it's a stop
if ((activedata[ channel] == 1 )&& ( duration >RFtrx::maxPeriodLength)) {
RFtrx::activedata[ channel]=0;
// do we need to know it was a large pulse?
RFtrx::rxbuffer[ channel][RFtrx::rxhead[ channel]]= 0;
RFtrx::rxhead[ channel]++;
if ( RFtrx::rxhead[ channel] >= RX_SIZE ) RFtrx::rxhead[ channel]=0;
if ( RFtrx::rxhead[ channel] == RFtrx::rxtail[ channel] ) {
//overflow
}
return;
}
// longer then max start length means no frame at all
if (duration >RFtrx::maxStartLength) {
RFtrx::activedata[ channel]=0;
return;
}
// start of frame
if ( (duration > RFtrx::minStartLength) && (duration < RFtrx::maxStartLength) && RFtrx::activedata[ channel] == 0 ){
RFtrx::activedata[ channel]=1;
RFtrx::rxbuffer[ channel][RFtrx::rxhead[ channel]]= 0;
RFtrx::rxhead[ channel]++;
if ( RFtrx::rxhead[ channel] >= RX_SIZE ) RFtrx::rxhead[ channel]=0;
if ( RFtrx::rxhead[ channel] == RFtrx::rxtail[ channel] ) {
//overflow
}
}
//if (duration > RFtrx::maxStartLength) {
// return;
//}
RFtrx::rxbuffer[ channel][RFtrx::rxhead[ channel]]= duration;
RFtrx::rxhead[ channel]++;
if ( RFtrx::rxhead[ channel] >= RX_SIZE ) RFtrx::rxhead[ channel]=0;
if ( RFtrx::rxhead[ channel] == RFtrx::rxtail[ channel] ) {
//overflow
}
//Serial.println(duration);
//digitalWrite(13,digitalRead(2));
RFtrx::lastTime[ channel]=currentTime;
}
void RFtrx::receiveInterruptA() {
long currentTime=micros();
if ( currentTime < RFtrx::lastTime[0] ) {
// clock wrap
RFtrx::lastTime[0]=currentTime;
RFtrx::activedata[0]=0;
return;
}
word duration=currentTime - RFtrx::lastTime[0];
if (duration < RFtrx::minPeriodLength) {
// no spikes
glitch[0]=1;
return;
}
if ( glitch[0] == 1 ) {
//end of spike.
glitch[0]=0;
return;
}
if ((activedata[0] == 1 )&& ( duration >RFtrx::maxPeriodLength)) {
RFtrx::activedata[0]=0;
RFtrx::rxbuffer[0][RFtrx::rxhead[0]]= 0;
RFtrx::rxhead[0]++;
if ( RFtrx::rxhead[0] >= RX_SIZE ) RFtrx::rxhead[0]=0;
if ( RFtrx::rxhead[0] == RFtrx::rxtail[0] ) {
//overflow
}
// digitalWrite(13,0);
return;
}
if (duration >RFtrx::maxStartLength) {
RFtrx::activedata[0]=0;
// digitalWrite(13,0);
return;
}
if ( (duration > RFtrx::minStartLength) && (duration < RFtrx::maxStartLength) && RFtrx::activedata[0] == 0 ){
RFtrx::activedata[0]=1;
RFtrx::rxbuffer[0][RFtrx::rxhead[0]]= 0;
RFtrx::rxhead[0]++;
if ( RFtrx::rxhead[0] >= RX_SIZE ) RFtrx::rxhead[0]=0;
if ( RFtrx::rxhead[0] == RFtrx::rxtail[0] ) {
//overflow
}
}
if (duration > RFtrx::maxStartLength) {
// digitalWrite(13,0);
return;
}
RFtrx::rxbuffer[0][RFtrx::rxhead[0]]= duration;
RFtrx::rxhead[0]++;
if ( RFtrx::rxhead[0] >= RX_SIZE ) RFtrx::rxhead[0]=0;
if ( RFtrx::rxhead[0] == RFtrx::rxtail[0] ) {
//overflow
}
//Serial.println(duration);
//digitalWrite(13,digitalRead(2));
RFtrx::lastTime[0]=currentTime;
}