-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFlamingoSwitch.cpp
393 lines (337 loc) · 10.3 KB
/
FlamingoSwitch.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// ---------------------------------------------------------------------------
// Flamingo Switch Library - v3.0
// ---------------------------------------------------------------------------
#include "FlamingoSwitch.h"
// microseconds
const int PULSE_LENGTH = 330;
unsigned long FlamingoSwitch::nReceivedValue = NULL;
unsigned int FlamingoSwitch::nReceivedBitlength = 0;
int FlamingoSwitch::nReceiveTolerance = 60;
unsigned int FlamingoSwitch::nReceivedDelay = 0;
unsigned int FlamingoSwitch::timings[FLAMINGO_MAX_CHANGES];
FlamingoSwitch::FlamingoSwitch()
{
this->nTransmitterPin = -1;
this->nReceiverInterrupt = -1;
FlamingoSwitch::nReceivedValue = NULL;
this->nPulseLength = PULSE_LENGTH;
}
/**
* Enable transmissions
*
* @param nTransmitterPin Arduino Pin to which the sender is connected to
*/
void FlamingoSwitch::enableTransmit(int nTransmitterPin)
{
this->nTransmitterPin = nTransmitterPin;
pinMode(this->nTransmitterPin, OUTPUT);
}
/**
* Disable transmissions
*/
void FlamingoSwitch::disableTransmit()
{
this->nTransmitterPin = -1;
}
// Disables the receiver and transmits the whole code before enabling the receiver again.
// The code must be right aligned: lower 28 bits.
void FlamingoSwitch::send(uint32_t code, uint8_t retries)
{
if (this->nTransmitterPin != -1)
{
boolean disabled_Receive = false;
int nReceiverInterrupt_backup = nReceiverInterrupt;
if (this->nReceiverInterrupt != -1)
{
this->disableReceive();
disabled_Receive = true;
}
{
for (int j = 0; j < retries; j++)
{
sendSync();
for (int i = 27; i >= 0; i--) // Flamingo command is only 28 bits: we take the lower ones.*/
{
if (code & (1L << i))
{
send1();
}
else
{
send0();
}
}
}
}
if (disabled_Receive)
{
this->enableReceive(nReceiverInterrupt_backup);
}
}
}
inline void FlamingoSwitch::transmit(int nHighPulses, int nLowPulses)
{
digitalWrite(this->nTransmitterPin, HIGH);
delayMicroseconds(this->nPulseLength * nHighPulses);
digitalWrite(this->nTransmitterPin, LOW);
delayMicroseconds(this->nPulseLength * nLowPulses);
}
/**
* Sends a "0" Bit
* _
* Waveform Protocol: | |___
*/
inline void FlamingoSwitch::send0()
{
this->transmit(1, 3);
}
/**
* Sends a "1" Bit
* ___
* Waveform Protocol : | |_
*/
inline void FlamingoSwitch::send1()
{
this->transmit(3, 1);
}
/**
* Sends a "Sync" Bit
* _
* Waveform Protocol: | |_______________
*/
inline void FlamingoSwitch::sendSync()
{
this->transmit(1, 15);
}
// ------------------------------------------------------------------------------------------------
void FlamingoSwitch::enableReceive(int interrupt)
{
this->nReceiverInterrupt = interrupt;
this->enableReceive();
}
void FlamingoSwitch::enableReceive()
{
if (this->nReceiverInterrupt != -1)
{
FlamingoSwitch::nReceivedValue = 0;
FlamingoSwitch::nReceivedBitlength = 0;
attachInterrupt(this->nReceiverInterrupt, handleInterrupt, CHANGE);
}
}
void FlamingoSwitch::disableReceive()
{
detachInterrupt(this->nReceiverInterrupt);
this->nReceiverInterrupt = -1;
}
bool FlamingoSwitch::available() const
{
return FlamingoSwitch::nReceivedValue != NULL;
}
void FlamingoSwitch::resetAvailable()
{
FlamingoSwitch::nReceivedValue = NULL;
}
unsigned long FlamingoSwitch::getReceivedValue() const
{
return FlamingoSwitch::nReceivedValue;
}
unsigned int FlamingoSwitch::getReceivedBitlength() const
{
return FlamingoSwitch::nReceivedBitlength;
}
unsigned int FlamingoSwitch::getReceivedDelay() const
{
return FlamingoSwitch::nReceivedDelay;
}
unsigned int* FlamingoSwitch::getReceivedRawdata() const
{
return FlamingoSwitch::timings;
}
bool FlamingoSwitch::receiveProtocol(unsigned int changeCount)
{
unsigned long code = 0;
unsigned long delay = FlamingoSwitch::timings[0] / 15;
unsigned long delayTolerance = delay * FlamingoSwitch::nReceiveTolerance * 0.01;
for (int i = 1; i < changeCount; i = i + 2)
{
if (
// 0: 1 3
FlamingoSwitch::timings[i] > delay - delayTolerance &&
FlamingoSwitch::timings[i] < delay + delayTolerance &&
FlamingoSwitch::timings[i + 1] > delay * 3 - delayTolerance &&
FlamingoSwitch::timings[i + 1] < delay * 3 + delayTolerance
)
{
code = code << 1;
}
else if (
// 1: 3 1
FlamingoSwitch::timings[i] > delay * 3 - delayTolerance &&
FlamingoSwitch::timings[i] < delay * 3 + delayTolerance &&
FlamingoSwitch::timings[i + 1] > delay - delayTolerance &&
FlamingoSwitch::timings[i + 1] < delay + delayTolerance
)
{
code += 1;
code = code << 1;
}
else
{
// Failed
i = changeCount;
code = 0;
}
}
code = code >> 1;
if (changeCount > 6)
{ // ignore < 4bit values as there are no devices sending 4bit values => noise
FlamingoSwitch::nReceivedValue = code;
FlamingoSwitch::nReceivedBitlength = changeCount / 2;
FlamingoSwitch::nReceivedDelay = delay;
}
if (code == 0)
{
return false;
}
else if (code != 0)
{
return true;
}
}
#define LIMIT_28BIT 4000 // the sync is about 4875 (15* 325us)
#define LIMIT_24BIT 6000
#define TOLERANCE 200
void FlamingoSwitch::handleInterrupt()
{
static unsigned int duration;
static unsigned int changeCount;
static unsigned long lastTime;
static unsigned int repeatCount;
long time = micros();
duration = time - lastTime;
// The device sends:
// 4 times a 28Bit code
// 6 times a 32Bit code
// 6 times a 24Bit code
// we are only interested in the first one (28Bit), so we filter the others away.
// This is done by taking only the code with the short sync of
if (duration > LIMIT_28BIT && duration < LIMIT_24BIT)
{
repeatCount++;
changeCount--;
if (repeatCount == 2)
{
if (receiveProtocol(changeCount) == false)
{
}
repeatCount = 0;
}
changeCount = 0;
}
else if (duration > LIMIT_24BIT)
{
changeCount = 0;
}
if (changeCount >= FLAMINGO_MAX_CHANGES)
{
changeCount = 0;
repeatCount = 0;
}
FlamingoSwitch::timings[changeCount++] = duration;
lastTime = time;
}
/*
Encode and decode routines are based on
http ://cpp.sh/9ye4
https ://github.com/r10r/he853-remote
Thanks to fuchks from the FHEM forum for providing this
see also https ://forum.fhem.de/index.php/topic,36399.60.html
*/
static uint8_t ikey[16] = { 5, 12, 6, 2, 8, 11, 1, 10, 3, 0, 4, 14, 7, 15, 9, 13 }; //invers cryptokey (exchanged index & value)
/*
Extracts from the input (28-Bit code needs to be aligned to the right: 0x0.......):
button = receiverId
value (0=OFF, 1=ON, DIM)
rolling code 0..n
transmitterId of the remote control.
*/
void FlamingoSwitch::decrypt(uint32_t input, uint16_t& receiverId, uint8_t& value, uint8_t& rollingCode, uint16_t& transmitterId)
{
uint8_t mn[7]; // message separated in nibbles
input = ((input << 2) & 0x0FFFFFFF) | ((input & 0xC000000) >> 0x1a); //shift 2 bits left & copy bit 27/28 to bit 1/2
mn[0] = input & 0x0000000F;
mn[1] = (input & 0x000000F0) >> 0x4;
mn[2] = (input & 0x00000F00) >> 0x8;
mn[3] = (input & 0x0000F000) >> 0xc;
mn[4] = (input & 0x000F0000) >> 0x10;
mn[5] = (input & 0x00F00000) >> 0x14;
mn[6] = (input & 0x0F000000) >> 0x18;
mn[6] = mn[6] ^ 9; // no decryption
//XOR decryption 2 rounds
for (uint8_t r = 0; r <= 1; r++)
{ // 2 decryption rounds
for (uint8_t i = 5; i >= 1; i--)
{ // decrypt 4 nibbles
mn[i] = ((ikey[mn[i]] - r) & 0x0F) ^ mn[i - 1]; // decrypted with predecessor & key
}
mn[0] = (ikey[mn[0]] - r) & 0x0F; //decrypt first nibble
}
//Output decrypted message
//uint32_t in = (~((input >> 2) | (((input & 3) << 0x1a))) << 4);
receiverId = (uint16_t)mn[0];
value = (((mn[1] >> 1) & 1) + (mn[6] & 0x7) + ((mn[6] & 0x8) >> 3));
rollingCode = (mn[1] >> 2);
transmitterId = (mn[5] << 12) + (mn[4] << 8) + (mn[3] << 4) + (mn[2] << 0);
}
static uint8_t key[17] = { 9, 6, 3, 8, 10, 0, 2, 12, 4, 14, 7, 5, 1, 15, 11, 13, 9 }; //cryptokey
/*
Encrypts the
button = receiverId
value (0=OFF, 1=ON; DIM)
rolling code 0..n
transmitterId of the remote control.
28-Bit code is aligned to the right (0x0.......)!
*/
uint32_t FlamingoSwitch::encrypt(uint8_t receiverId, uint8_t value, uint8_t rollingCode, uint16_t transmitterId)
{
uint8_t mn[7];
mn[0] = receiverId; // mn[0] = iiiib i=receiver-ID
mn[1] = (rollingCode << 2) & 15; // 2 lowest bits of rolling-code
if (value > 0)
{ // ON or OFF
mn[1] |= 2;
} // mn[1] = rrs0b r=rolling-code, s=ON/OFF, 0=const 0?
mn[2] = transmitterId & 15; // mn[2..5] = ttttb t=transmitterId in nibbles -> 4x ttttb
mn[3] = (transmitterId >> 4) & 15;
mn[4] = (transmitterId >> 8) & 15;
mn[5] = (transmitterId >> 12) & 15;
if (value >= 2 && value <= 9)
{ // mn[6] = dpppb d = dim ON/OFF, p=%dim/10 - 1
mn[6] = value - 2; // dim: 0=10%..7=80%
mn[6] |= 8; // dim: ON
}
else
{
mn[6] = 0; // dim: OFF
}
//XOR encryption 2 rounds
for (uint8_t r = 0; r <= 1; r++)
{ // 2 encryption rounds
mn[0] = key[mn[0] - r + 1]; // encrypt first nibble
for (uint8_t i = 1; i <= 5; i++)
{ // encrypt 4 nibbles
mn[i] = key[(mn[i] ^ mn[i - 1]) - r + 1];// crypted with predecessor & key
}
}
mn[6] = mn[6] ^ 9; // no encryption
uint32_t msg = 0; // copy the encrypted nibbles in output buffer
msg |= (uint32_t)mn[6] << 0x18;
msg |= (uint32_t)mn[5] << 0x14;
msg |= (uint32_t)mn[4] << 0x10;
msg |= (uint32_t)mn[3] << 0x0c;
msg |= (uint32_t)mn[2] << 0x08;
msg |= (uint32_t)mn[1] << 0x04;
msg |= (uint32_t)mn[0];
msg = (msg >> 2) | ((msg & 3) << 0x1a); // shift 2 bits right & copy lowest 2 bits of cbuf[0] in msg bit 27/28
return msg;
}