-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRF433Detector.cpp
More file actions
170 lines (138 loc) · 4.13 KB
/
RF433Detector.cpp
File metadata and controls
170 lines (138 loc) · 4.13 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
/*
Rf433 - Arduino libary to transmit arbitrary RF433 signals
Copyright (c) 2018 François Terrier. All right reserved.
Contributors:
- François Terrier / fterrier(at)gmail(dot)com
Project home: https://github.com/fterrier/dwarf433
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "RF433Detector.h"
#include "List.h"
#include <Arduino.h>
Detector::Detector(int bs, int p) {
stream = new List(bs);
iterator = stream->iterator();
pin = p;
dtSize = 0;
from = 0;
to = 0;
detectorTimings = new DetectorTiming[0];
currentIndexes = new unsigned int[0];
}
Detector::~Detector() {
delete stream;
delete [] detectorTimings;
delete [] currentIndexes;
}
void Detector::reset() {
from = 0;
to = 0;
dtSize = 0;
delete [] detectorTimings;
delete [] currentIndexes;
detectorTimings = new DetectorTiming[0];
currentIndexes = new unsigned int[0];
}
void Detector::start(void (*callback)(void)) {
attachInterrupt(digitalPinToInterrupt(pin), callback, CHANGE);
}
static unsigned int minimum(unsigned int *timings, unsigned int size) {
unsigned int min = 0-1;
for (int i=0;i<size;i++) {
if (timings[i] < min) {
min = timings[i];
}
}
return min;
}
static unsigned int maximum(unsigned int *timings, unsigned int size) {
unsigned int max = 0;
for (int i=0;i<size;i++) {
if (timings[i] > max) {
max = timings[i];
}
}
return max;
}
void Detector::addDetection(unsigned int *timings, unsigned int size, float tolerance) {
DetectorTiming* dt = new DetectorTiming[dtSize+1];
unsigned int* c = new unsigned int[dtSize+1];
memcpy(dt, detectorTimings, dtSize*sizeof(DetectorTiming));
memcpy(c, currentIndexes, dtSize*sizeof(unsigned int));
delete [] detectorTimings;
delete [] currentIndexes;
detectorTimings = dt;
currentIndexes = c;
unsigned int* t = new unsigned int[size];
memcpy(t, timings, size*sizeof(unsigned int));
unsigned int mn = minimum(timings, size);
unsigned int mx = maximum(timings, size);
if (from == 0 || mn - mn*tolerance < from) {
from = mn - mn*tolerance;
}
if (to == 0 || mx + mx*tolerance > to) {
to = mx + mx*tolerance;
}
dt[dtSize] = DetectorTiming{t, size, tolerance};
c[dtSize] = 0;
dtSize = dtSize + 1;
}
void Detector::checkDetected(bool *detected) {
int loops = 100;
while (iterator->hasNext()) {
int v = iterator->next();
for (int i=0;i<dtSize;i++) {
unsigned int c = currentIndexes[i];
DetectorTiming dt = detectorTimings[i];
unsigned int *sig = dt.timings;
if (c < dt.size) {
int mn = sig[c] - sig[c]*dt.tolerance;
int mx = sig[c] + sig[c]*dt.tolerance;
if (v < mx && v > mn) {
currentIndexes[i] = c + 1;
} else {
mn = sig[0] - sig[0]*dt.tolerance;
mx = sig[0] + sig[0]*dt.tolerance;
if (v < mx && v > mn) {
currentIndexes[i] = 1;
} else {
currentIndexes[i] = 0;
}
}
}
else {
detected[i] = true;
currentIndexes[i] = 0;
}
}
delay(0);
if (iterator->atHead()) {
Serial.println("reset");
for (int i=0;i<dtSize;i++) {
currentIndexes[i] = 0;
}
}
loops--;
}
}
void ICACHE_RAM_ATTR Detector::handleInterrupt() {
volatile unsigned long now = micros();
volatile unsigned int gap = now - last;
// otherwise ignore
if (gap > from && gap < to) {
if (!stream->addInt(gap)) {
Serial.println(".");
}
}
last = now;
}