-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFernotron.cpp
252 lines (210 loc) · 7.32 KB
/
Fernotron.cpp
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
/*
Fernotron - Library for remote control of Fernotron shutters
The basis of this code has been taken from:
https://github.com/sui77/rc-switch
Project home: https://github.com/dasoho/fernotron-control
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 "Fernotron.h"
#include <stdio.h>
#include <string.h>
// Transmit Timimings
#define SHORT 400
#define LONG 800
#define XLONG 3200
#define XXLONG 58000
#define XXXLONG 70500
// Receive lower and upper threshold
#define SHORT_LT 250
#define SHORT_UT 550
#define LONG_LT 650
#define LONG_UT 1000
#define XLONG_LT 3000
#define XLONG_UT 3500
#define XXLONG_LT 57000
#define XXLONG_UT 59000
#define XXXLONG_LT 69500
#define XXXLONG_UT 71500
int Fernotron::timings[FERNOTRON_MAX_CHANGES];
int Fernotron::receivedCode[FERNOTRON_PROTOCOL_LENGTH];
bool Fernotron::codeAvailable = false;
Fernotron::Fernotron() {
nReceiverInterrupt = -1;
nTransmitterPin = -1;
setRepeatTransmit(1);
}
bool Fernotron::enableTransmit(int transmitterPin) {
if (nReceiverInterrupt == -1) {
nTransmitterPin = transmitterPin;
pinMode(nTransmitterPin, OUTPUT);
return true;
}
else {
return false;
}
}
void Fernotron::setRepeatTransmit(int repeatTransmit) {
nRepeatTransmit = repeatTransmit;
}
void Fernotron::send(char* code) {
if (nTransmitterPin == -1) {
printf("Transmit mode not enabled\n");
return;
}
else if (strlen(code) != FERNOTRON_PROTOCOL_LENGTH) {
printf("Input code has wrong length. Input: %d, Expected: %d\n", strlen(code), FERNOTRON_PROTOCOL_LENGTH);
return;
}
else {
int highLength = 0;
int lowLength = 0;
for (int r = 0; r < nRepeatTransmit; r++) {
for(int i = 0; i < FERNOTRON_PROTOCOL_LENGTH; i++) {
switch(code[i]){
case '1': // LS+HS
lowLength = SHORT;
highLength = SHORT;
break;
case '2': // LL+HS
lowLength = LONG;
highLength = SHORT;
break;
case '3': // LS+HL
lowLength = SHORT;
highLength = LONG;
break;
case '4': // LL+HL
lowLength = LONG;
highLength = LONG;
break;
case '5': // LXL+HS
lowLength = XLONG;
highLength=SHORT;
break;
case '6': // LXL+HL
lowLength = XLONG;
highLength = LONG;
break;
case '7': // LXXL+HS
lowLength = XXLONG;
highLength = SHORT;
break;
case '8': // LXXXL+HS
lowLength = XXXLONG;
highLength = SHORT;
break;
}
digitalWrite(nTransmitterPin,LOW);
delayMicroseconds(lowLength);
digitalWrite(nTransmitterPin, HIGH);
delayMicroseconds(highLength);
}
}
digitalWrite(nTransmitterPin, LOW);
}
}
bool Fernotron::enableReceive(int interrupt) {
if (nTransmitterPin == -1 && nReceiverInterrupt == -1) {
nReceiverInterrupt = interrupt;
Fernotron::codeAvailable = false;
wiringPiISR(nReceiverInterrupt, INT_EDGE_BOTH, &handleInterrupt);
return true;
}
else {
return false;
}
}
bool Fernotron::isAvailable() {
return Fernotron::codeAvailable;
}
void Fernotron::resetAvailable() {
Fernotron::codeAvailable = false;
}
int* Fernotron::getReceivedCode() {
return Fernotron::receivedCode;
}
int* Fernotron::getReceivedRawdata() {
return Fernotron::timings;
}
bool Fernotron::receiveProtocol(int changeCount) {
// Encoding used by the transmitter:
// LOW short + HIGH short 1
// LOW long + HIGH short 2
// LOW short + HIGH long 3
// LOW long + HIGH long 4
// LOW Xlong + HIGH short 5
// LOW Xlong + HIGH long 6
// LOW XXlong + HIGH short 7
// LOW XXXlong + HIGH short 8
bool success = true;
if (changeCount + 1 != FERNOTRON_PROTOCOL_LENGTH * 2) {
printf("Error in protocol length. Expected: %d, Received: %d\n", FERNOTRON_PROTOCOL_LENGTH * 2, changeCount );
success = false;
}
else {
int low, high;
for (int i = 0; i < changeCount && success ; i=i+2) {
low=Fernotron::timings[i];
high=Fernotron::timings[i+1];
if (low >= SHORT_LT && low <= SHORT_UT && high >= SHORT_LT && high <= SHORT_UT)
receivedCode[i/2] = 1;
else if (low >= LONG_LT && low <= LONG_UT && high >= SHORT_LT && high <= SHORT_UT)
receivedCode[i/2] = 2;
else if (low >= SHORT_LT && low <= SHORT_UT && high >= LONG_LT && high <= LONG_UT)
receivedCode[i/2] = 3;
else if (low >= LONG_LT && low <= LONG_UT && high >= LONG_LT && high <= LONG_UT)
receivedCode[i/2] = 4;
else if (low >= XLONG_LT && low <= XLONG_UT && high >= SHORT_LT && high <= SHORT_UT)
receivedCode[i/2] = 5;
else if (low >= XLONG_LT && low <= XLONG_UT && high >= LONG_LT && high <= LONG_UT)
receivedCode[i/2] = 6;
else if (low >= XXLONG_LT && low <= XXLONG_UT && high >= SHORT_LT && high <= SHORT_UT)
receivedCode[i/2] = 7;
else if (low >= XXXLONG_LT && low <= XXXLONG_UT && high >= SHORT_LT && high <= SHORT_UT)
receivedCode[i/2] = 8;
else {
// error
printf("Unexpected signal duration. Index: %d Low: %d, High: %d\n", i, low, high );
receivedCode[i/2] = 0;
success = false;
}
}
}
Fernotron::codeAvailable = success;
return Fernotron::codeAvailable;
}
void Fernotron::handleInterrupt() {
static int duration = 0;
static int changeCount = 0;
static long lastTime = 0;
static int repeatCount = 0;
long time = micros();
duration = time - lastTime;
if ( (duration > XXLONG_LT && duration < XXLONG_UT) || (duration > XXXLONG_LT && duration < XXXLONG_UT)) {
repeatCount++;
changeCount--;
if (repeatCount == 2 && Fernotron::codeAvailable == false) { // in case there is an existing code available, don't overwrite is until the client has read it.
if (receiveProtocol(changeCount) == false) {
//failed
}
repeatCount = 0;
}
changeCount = 0;
}
if (changeCount >= FERNOTRON_MAX_CHANGES) {
changeCount = 0;
repeatCount = 0;
}
Fernotron::timings[changeCount++] = duration;
lastTime = time;
}