-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRF433.cpp
More file actions
230 lines (182 loc) · 5.11 KB
/
RF433.cpp
File metadata and controls
230 lines (182 loc) · 5.11 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
/*
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 "RF433.h"
#include "Arduino.h"
Encoding::Encoding() {}
void Encoding::setZero(int o1, int t1, int o2, int t2) {
zero[0] = t1;
zero[1] = t2;
zseq[0] = o1;
zseq[1] = o2;
}
void Encoding::setOne(int o1, int t1, int o2, int t2) {
one[0] = t1;
one[1] = t2;
oseq[0] = o1;
oseq[1] = o2;
}
int* Encoding::getTimingsZero() {
return zero;
}
int* Encoding::getOrderZero() {
return zseq;
}
int* Encoding::getTimingsOne() {
return one;
}
int* Encoding::getOrderOne() {
return oseq;
}
Wave::Wave() {
command = 0;
len = 0;
}
Wave::Wave(String s) {
command = 0;
len = s.length();
for (int i=0;i<len;i++) {
int index = len-i-1;
if (s[index] == '1') {
command |= 1UL << i;
}
}
}
Wave::Wave(unsigned long c, int l) {
command = c;
len = l;
}
String Wave::toString() {
String r = "";
for (int i=1;i<=len;i++) {
int b = (command >> (len-i)) & 1;
r = r + String(b);
}
return r;
}
void Wave::sendWave(int pin, Encoding& encoding) {
for (int i=1;i<=len;i++) {
int b = (command >> (len-i)) & 1;
int *delays = (b ? encoding.getTimingsOne() : encoding.getTimingsZero());
int *seq = (b ? encoding.getOrderOne() : encoding.getOrderZero());
digitalWrite(pin, seq[0]);
delayMicroseconds(delays[0]);
digitalWrite(pin, seq[1]);
delayMicroseconds(delays[1]);
}
digitalWrite(pin, LOW);
}
Signal::Signal() {
len = 0;
encoding = Encoding();
}
Signal::~Signal() {
if (len > 0) {delete [] delays;}
if (pulse.len > 0) {delete [] pulse.delays;}
if (pulse.len > 0) {delete [] pulse.seqs;}
}
void Signal::setEncoding(Encoding e) {
encoding = e;
}
void Signal::setPulse(Wave wave) {
if (pulse.len > 0) {delete [] pulse.delays;}
if (pulse.len > 0) {delete [] pulse.seqs;}
pulse.len = 1;
pulse.seqs = new Wave[1]{wave};
}
void Signal::setPulse(Wave wave1, int delay, Wave wave2) {
if (pulse.len > 0) {delete [] pulse.delays;}
if (pulse.len > 0) {delete [] pulse.seqs;}
pulse.len = 2;
pulse.delays = new int[1]{delay};
pulse.seqs = new Wave[2]{wave1, wave2};
}
void Signal::setPulse(Wave waves[], int delays[], int numWaves) {
if (pulse.len > 0) {delete [] pulse.delays;}
if (pulse.len > 0) {delete [] pulse.seqs;}
pulse.len = numWaves;
pulse.delays = new int[numWaves-1]();
memcpy(pulse.delays, delays, (numWaves-1)*sizeof(int));
pulse.seqs = new Wave[numWaves]();
memcpy(pulse.seqs, waves, numWaves*sizeof(Wave));
}
void Signal::setRepeatIntervals(int intervals[], int numIntervals) {
if (len > 0) {delete [] delays;}
len = numIntervals + 1;
delays = new int[numIntervals];
memcpy(delays, intervals, numIntervals*sizeof(int));
}
void Signal::sendPulse(int pin, Pulse& pulse) {
#if RF433_DEBUG == 1
Serial.println("Sending pulse");
Serial.print("Pulse length: ");
Serial.println(pulse.len);
for (int i=0;i<pulse.len;i++) {
Serial.println(pulse.seqs[i].toString());
}
#endif
for (int i=0;i<pulse.len;i++) {
int d = (i == pulse.len-1) ? 0 : pulse.delays[i];
pulse.seqs[i].sendWave(pin, encoding);
delayMicroseconds(d);
}
}
void Signal::sendSignal(int pin) {
#if RF433_DEBUG == 1
Serial.println("Sending signal");
Serial.println("Encoding ZERO:");
Serial.print(encoding.getTimingsZero()[0]);
Serial.print(" ");
Serial.print(encoding.getOrderZero()[0]);
Serial.println("");
Serial.print(encoding.getTimingsZero()[1]);
Serial.print(" ");
Serial.print(encoding.getOrderZero()[1]);
Serial.println("");
Serial.println("Encoding ONE:");
Serial.print(encoding.getTimingsOne()[0]);
Serial.print(" ");
Serial.print(encoding.getOrderOne()[0]);
Serial.println("");
Serial.print(encoding.getTimingsOne()[1]);
Serial.print(" ");
Serial.print(encoding.getOrderOne()[1]);
Serial.println("");
#endif
for (int i=0;i<len;i++) {
int sync = (i == len-1) ? 0 : delays[i];
sendPulse(pin, pulse);
#if RF433_DEBUG == 1
Serial.print("Waiting ms ");
Serial.println(sync);
#endif
keepDelay(sync);
}
}
void Signal::keepDelay(int sync) {
int decay = 20;
int remaining = sync;
while (remaining > 0) {
if (decay < remaining) {
delay(decay);
} else {
delay(remaining);
}
remaining = remaining - decay;
}
}