-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRFremote.cpp
More file actions
303 lines (262 loc) · 6.58 KB
/
RFremote.cpp
File metadata and controls
303 lines (262 loc) · 6.58 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
291
292
293
294
295
296
297
298
299
300
301
302
/*
* RFremote
* Version 0.1 July, 2013
* Copyright 2013 Renato Aloi
* For details, see http://www.seriallink.com.br
*
* Interrupt code based on EMTECOrec by Renato Aloi
* Also influenced by IRremote by Ken Shirriff @ http://arcfn.com
*
* Code based on PPA TOK 433Mhz Code Learning remote control
*/
#include "Arduino.h"
#include "RFremote.h"
volatile RFparams rfparams;
//SignalPatternParams _signalparams;
void intSinal();
// Method: Constructor
// Desc: Put FACTORY DEFAULT initial values to variables and arrays
RFrecv::RFrecv()
{
init();
// Default config for PPA TOK 433Mhz remote
_signalparams.spaceMin = 10000;
_signalparams.spaceMax = 15000;
_signalparams.dotMin = 450;
_signalparams.dotMax = 550;
_signalparams.traceMin = 950;
_signalparams.traceMax = 1050;
_signalparams.skipFirst = 0;
_signalparams.skipLast = 0;
}
// Method: Constructor
// Desc: Put CUSTOM initial values to variables and arrays
RFrecv::RFrecv(SignalPatternParams *signalparams)
{
init();
// custom config
_signalparams.spaceMin = signalparams->spaceMin;
_signalparams.spaceMax = signalparams->spaceMax;
_signalparams.dotMin = signalparams->dotMin;
_signalparams.dotMax = signalparams->dotMax;
_signalparams.traceMin = signalparams->traceMin;
_signalparams.traceMax = signalparams->traceMax;
_signalparams.skipFirst = signalparams->skipFirst;
_signalparams.skipLast = signalparams->skipLast;
}
void RFrecv::init()
{
rfparams.lock = 0;
rfparams.eof = 0;
rfparams.diff = 0;
rfparams.tempo = micros();
rfparams.state = 0;
rfparams.idx = 0;
foundIdx[0] = 0;
foundIdx[1] = 0;
resetBuffer();
resetCmd();
}
// Method: begin
// Desc: Configures pins and interrupts
void RFrecv::begin()
{
// LED debug pin config
pinMode(LED_DEBUG, OUTPUT);
digitalWrite(LED_DEBUG, LOW);
// RF pin config
pinMode(PORTA_RF, INPUT);
digitalWrite(PORTA_RF, HIGH);
// Interrupt to trigger when RF pin state change
attachInterrupt(INT_RF, intSinal, CHANGE);
}
// Method: available
// Desc: Check for data arrival, for pattern match, for data timmings
// and fill cmd array if result is true
unsigned char RFrecv::available()
{
unsigned char ret = 0;
// Check if there is a lock from interrupt
// Check also if not end of buffer
if (rfparams.lock && !rfparams.eof)
{
// Putting state and time into buffer
buffState[rfparams.idx] = rfparams.state;
buffDiff[rfparams.idx] = rfparams.diff;
// release buffer lock
rfparams.lock = 0;
// increment idx
rfparams.idx++;
if (rfparams.idx > BUFF_SIZE - 1)
{
// if end of buffer, set flag eof
// and put zero to idx
rfparams.eof = 1;
rfparams.idx = 0;
}
}
else if (rfparams.eof)
{
// if end of buffer, starts to check
// signal pattern and data
if (gotPattern())
{
// if signal pattern is correct
// check data and fill cmd[] with command data
if (gotData())
{
// if got here, command available!
ret = 1;
if (DEBUG)
{
rfparams.lock = 1;
unsigned long t = millis() + 200;
digitalWrite(LED_DEBUG, HIGH);
while(t>millis());
t = millis() + 200;
digitalWrite(LED_DEBUG, LOW);
while(t>millis());
t = millis() + 200;
digitalWrite(LED_DEBUG, HIGH);
while(t>millis());
t = millis() + 200;
digitalWrite(LED_DEBUG, LOW);
while(t>millis());
t = millis() + 200;
rfparams.lock = 0;
}
}
}
// check if there is errors
if (!ret)
{
// reset command array in case of error
resetCmd();
}
// release eof flag
rfparams.eof = 0;
// clear the buffer to start over again
resetBuffer();
}
return ret;
}
// Method: gotPattern
// Desc: Check if pattern recog is achieved
unsigned char RFrecv::gotPattern()
{
unsigned char foundItTwice = 0;
unsigned char foundCount = 0;
// loop through buffer timmings to find
// two (2) delays between 10000us to 15000us (its configurable)
for (int i = 0; i < BUFF_SIZE; i++)
{
// check if pattern is found
if (buffDiff[i] > _signalparams.spaceMin && buffDiff[i] < _signalparams.spaceMax)
{
foundIdx[foundCount] = i;
/*
if (foundCount == 0)
foundIdx[foundCount] = i + _signalparams.skipFirst;
else
foundIdx[foundCount] = i - _signalparams.skipLast;
*/
foundCount++;
}
// if found two items that correspond to pattern
// return true
if (foundCount > 1)
{
foundItTwice = 1;
break;
}
}
return foundItTwice;
}
// Method: gotData
// Desc: Check if data has correct timmings and fill cmd array
unsigned char RFrecv::gotData()
{
unsigned char isError = 0;
unsigned int cmdIdx = 0;
// Check if gotPattern found the two pattern's needed here
if (foundIdx[0] && foundIdx[1])
{
// loop through found indexes
for (int i = foundIdx[0] + 1 + _signalparams.skipFirst; i < foundIdx[1] - _signalparams.skipLast; i++)
{
// check if its a 500us timming
// so that case fill 1 byte into cmd array
if (buffDiff[i] > _signalparams.dotMin && buffDiff[i] < _signalparams.dotMax)
{
cmd[cmdIdx] = (buffState[i] ? '1' : '0');
}
// check if its a 1000us timming
// so that case fill 2 byte into cmd array
else if (buffDiff[i] > _signalparams.traceMin && buffDiff[i] < _signalparams.traceMax)
{
cmd[cmdIdx] = (buffState[i] ? '1' : '0');
cmdIdx++;
cmd[cmdIdx] = (buffState[i] ? '1' : '0');
}
else
{
// if timmings not 500us or 1000us,
// so, it is an invalid signal, discard
isError = 1;
break;
}
// increment cmd array index
// and check for buffer overflow
cmdIdx++;
if (cmdIdx > CMD_SIZE - 1)
{
isError = 1;
break;
}
}
}
else
{
// if foundIdx's values are zero (0)
// means not valid pattern, abort buffer
isError = 1;
}
return !isError;
}
// Method: resetBuffer
// Desc: Initiate buffer's arrays
void RFrecv::resetBuffer()
{
for (int i = 0; i < BUFF_SIZE; i++)
{
buffState[i] = 0;
buffDiff[i] = 0;
}
}
// Method: resetCmd
// Desc: Initiate cmd's arrays
void RFrecv::resetCmd()
{
for (int i = 0; i < CMD_SIZE; i++)
{
cmd[i] = '\0';
}
}
// Method: intSinal
// Desc: Interrupt function triggered by attachInterrupt()
void intSinal()
{
// Run only if buffer lock is lose
// and is not buffer's end
if (!rfparams.lock && !rfparams.eof)
{
// Lock immediately!
rfparams.lock = 1;
// Read pin's state and calculate time interval
rfparams.state = digitalRead(PORTA_RF);
rfparams.diff = micros() - rfparams.tempo;
rfparams.tempo = micros();
// Debug led, it blinks trhough state changes
if (DEBUG_INT) digitalWrite(LED_DEBUG, rfparams.state);
}
}