forked from JorjBauer/acurite_decoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoders.h
745 lines (679 loc) · 20.3 KB
/
decoders.h
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// Decoders.h, copied from the Jeelib RF12 example 'ookRelay2'
// Generalized decoder framework for 868 MHz and 433 MHz OOK signals.
// 2010-04-11 <[email protected]> http://opensource.org/licenses/mit-license.php
// Modified 2015-03-04 <[email protected]>, adding AcuRite temp sensor demod
#include <Arduino.h>
#include <util/crc16.h>
#define ACURITE_5N1_BITLEN 64
class DecodeOOK {
protected:
byte bits, flip, state, pos, data[25];
// the following fields are used to deal with duplicate packets
word lastCrc, lastTime;
byte repeats, minGap, minCount;
// gets called once per incoming pulse with the width in us
// return values: 0 = keep going, 1 = done, -1 = no match
virtual char decode (word width) =0;
// add one bit to the packet data buffer
void gotBit (char value) {
byte *ptr = data + pos;
*ptr = (*ptr >> 1) | (value << 7);
if (++bits >= 8) {
bits = 0;
if (++pos >= sizeof data) {
resetDecoder();
return;
}
}
state = OK;
}
// store a bit using Manchester encoding
void manchester (char value) {
flip ^= value; // manchester code, long pulse flips the bit
gotBit(flip);
}
// move bits to the front so that all the bits are aligned to the end
void alignTail (byte max =0) {
// align bits
if (bits != 0) {
data[pos] >>= 8 - bits;
for (byte i = 0; i < pos; ++i)
data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits));
bits = 0;
}
// optionally shift bytes down if there are too many of 'em
if (max > 0 && pos > max) {
byte n = pos - max;
pos = max;
for (byte i = 0; i < pos; ++i)
data[i] = data[i+n];
}
}
void reverseBits () {
for (byte i = 0; i < pos; ++i) {
byte b = data[i];
for (byte j = 0; j < 8; ++j) {
data[i] = (data[i] << 1) | (b & 1);
b >>= 1;
}
}
}
void reverseNibbles () {
for (byte i = 0; i < pos; ++i)
data[i] = (data[i] << 4) | (data[i] >> 4);
}
bool checkRepeats () {
// calculate the checksum over the current packet
word crc = ~0;
for (byte i = 0; i < pos; ++i)
crc = _crc16_update(crc, data[i]);
// how long was it since the last decoded packet
word now = millis() / 100; // tenths of seconds
word since = now - lastTime;
// if different crc or too long ago, this cannot be a repeated packet
if (crc != lastCrc || since > minGap)
repeats = 0;
// save last values and decide whether to report this as a new packet
lastCrc = crc;
lastTime = now;
return repeats++ == minCount;
}
public:
enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
DecodeOOK (byte gap =5, byte count =0)
: lastCrc (0), lastTime (0), repeats (0), minGap (gap), minCount (count)
{ resetDecoder(); }
bool nextPulse (word width) {
if (state != DONE)
switch (decode(width)) {
case -1: // decoding failed
resetDecoder();
break;
case 1: // decoding finished
while (bits)
gotBit(0); // padding
state = checkRepeats() ? DONE : UNKNOWN;
break;
}
return state == DONE;
}
const byte* getData (byte& count) const {
count = pos;
return data;
}
void resetDecoder () {
bits = pos = flip = 0;
state = UNKNOWN;
}
};
// 433 MHz decoders
#define SYNC_WIDTH 500 // min duration of the positive half of a sync bit (uS)
#define MAX_SYNC_WIDTH 670
#define ONE_WIDTH 380
#define ZERO_WIDTH 150
#define BIT_WIDTH 1200 // total duration of a one-bit pulse
#define NUM_SYNCS 4
class AcuRite5N1Decoder : public DecodeOOK {
public:
AcuRite5N1Decoder () {}
virtual char decode (word width) {
char ret = 0;
if (!width)
return -1;
switch (state) {
case UNKNOWN:
// 0-to-positive initial transition
state = OK;
break;
case OK:
// OK invoked at pos-to-0 transition. Look at width, figure out what
// kind of pulse this is (SYNC/0/1)
if (width > BIT_WIDTH) {
// Dunno what data we have; it's bad.
state = UNKNOWN;
ret = -1;
} else if (width >= SYNC_WIDTH) {
// sync bit
state = T0; // T0: expecting zero-pulse for SYNC of 584-600mS
} else if (width >= ONE_WIDTH) {
if (flip != NUM_SYNCS) {
state = T3;
return 0;
}
state = T1; // T1: expecting zero-pulse for 1-bit of 180-200mS
} else if (width >= ZERO_WIDTH) {
if (flip != NUM_SYNCS) {
state = T3;
return 0;
}
state = T2; // T2: expecting zero-pulse for 0-bit of 584-800mS
} else {
// Again, bad data.
state = UNKNOWN;
ret = -1;
}
break;
// States T0 - T2: receiving the zero-pulse half. Set to state = OK
// if we receive it as expected, and reset if not?
case T0: // Sync bits.
if (width > MAX_SYNC_WIDTH) {
// reject: the trailing edge is too long, so it's not a sync.
state = UNKNOWN;
return -1;
}
if (width < SYNC_WIDTH) {
// We might have missed a transition, and this is the start of a 1/0 -
// change state, increment sync counter, and re-process it
flip++;
state = OK;
return decode(width);
}
flip++; // use flip to count sync bits
state = OK;
break;
case T1:
gotBit(1);
if (pos >= (ACURITE_5N1_BITLEN / 8)) {
// Data ready to receive - packets are 7 bytes long
reverseBits();
return 1;
}
break;
case T2:
gotBit(0);
if (pos >= (ACURITE_5N1_BITLEN / 8)) {
// Data ready to receive - packets are 7 bytes long
reverseBits();
return 1;
}
break;
case T3:
// error condition; consume the 0-bit and then reset
return -1;
break;
}
return ret;
}
};
class OregonDecoder : public DecodeOOK {
public:
OregonDecoder () {}
virtual char decode (word width) {
if (200 <= width && width < 1200) {
byte w = width >= 700;
switch (state) {
case UNKNOWN:
if (w == 0)
++flip;
else if (10 <= flip && flip <= 50) {
flip = 1;
manchester(1);
} else
return -1;
break;
case OK:
if (w == 0)
state = T0;
else
manchester(1);
break;
case T0:
if (w == 0)
manchester(0);
else
return -1;
break;
}
return 0;
}
if (width >= 2500 && pos >= 9)
return 1;
return -1;
}
};
class CrestaDecoder : public DecodeOOK {
// http://members.upc.nl/m.beukelaar/Crestaprotocol.pdf
public:
CrestaDecoder () {}
virtual char decode (word width) {
if (200 <= width && width < 1300) {
byte w = width >= 750;
switch (state) {
case UNKNOWN:
if (w == 1)
++flip;
else if (2 <= flip && flip <= 10)
state = T0;
else
return -1;
break;
case OK:
if (w == 0)
state = T0;
else
gotBit(1);
break;
case T0:
if (w == 0)
gotBit(0);
else
return -1;
break;
}
return 0;
}
if (width >= 2500 && pos >= 7)
return 1;
return -1;
}
};
class KakuDecoder : public DecodeOOK {
public:
KakuDecoder () {}
virtual char decode (word width) {
if ((180 <= width && width < 450) || (950 <= width && width < 1250)) {
byte w = width >= 700;
switch (state) {
case UNKNOWN:
case OK:
if (w == 0)
state = T0;
else
return -1;
break;
case T0:
if (w)
state = T1;
else
return -1;
break;
case T1:
state += w + 1;
break;
case T2:
if (w)
gotBit(0);
else
return -1;
break;
case T3:
if (w == 0)
gotBit(1);
else
return -1;
break;
}
return 0;
}
if (width >= 2500 && 8 * pos + bits == 12) {
for (byte i = 0; i < 4; ++i)
gotBit(0);
alignTail(2);
return 1;
}
return -1;
}
};
class KakuADecoder : public DecodeOOK {
enum { Pu, P1, P5, P10 }; //pulsetypes: Pu = unknown, P1 = between 100 and 600uS, P5 = between 800 and 1800uS, P10 = between 2 and 3 mS
uint8_t backBuffer[4]; //we need to keep a 4 bit history
byte pulse;
public:
KakuADecoder () {
clearBackBuffer();
}
void clearBackBuffer()
{
backBuffer[0] = Pu;
backBuffer[1] = Pu;
backBuffer[2] = Pu;
backBuffer[3] = Pu;
}
virtual char decode (word width) {
if ((width >= 100) && (width <= 600))
pulse = P1;
else if ((width >=800) && (width <= 1800))
pulse = P5;
else if ((width >=2000) && (width <= 3000))
pulse = P10;
else
{
clearBackBuffer();//out-of-protocol pulsewidth, abort;
return -1; //reset decoder
}
backBuffer[3] = backBuffer[2];
backBuffer[2] = backBuffer[1];
backBuffer[1] = backBuffer[0];
backBuffer[0] = pulse;
switch(state)
{
case UNKNOWN:
if( backBuffer[2] == P1 && backBuffer[1] == P10 && backBuffer[0] == P1 ) //received start/sync signal
{
state = T0;
clearBackBuffer();
}
break;
case OK: //returning after receiving a good bit
case T0:
if( pulse == P10 ) //received start/sync signal
{
clearBackBuffer();
return -1; //reset decoder
}
if( backBuffer[3] != Pu ) //depending on the preceding pulsetypes we received a 1 or 0
{
if ( (backBuffer[3] == P5) && (backBuffer[2] == P1) && (backBuffer[1] == P1) && (backBuffer[0] == P1))
gotBit(1);
else
if ( (backBuffer[3] == P1) && (backBuffer[2] == P1) && (backBuffer[1] == P5) && (backBuffer[0] == P1))
gotBit(0);
else
{
state = UNKNOWN;
break;
}
clearBackBuffer();
if( pos >= 4 ) //we expect 4 bytes
return 1;
}
break;
default:
break;
}
return 0;
}
};
class XrfDecoder : public DecodeOOK {
public:
XrfDecoder () {}
// see also http://davehouston.net/rf.htm
virtual char decode (word width) {
if (width > 2000 && pos >= 4)
return 1;
if (width > 5000)
return -1;
if (width > 4000 && state == UNKNOWN) {
state = OK;
return 0;
}
if (350 <= width && width < 1800) {
byte w = width >= 720;
switch (state) {
case OK:
if (w == 0)
state = T0;
else
return -1;
break;
case T0:
gotBit(w);
break;
}
return 0;
}
return -1;
}
};
class HezDecoder : public DecodeOOK {
public:
HezDecoder () {}
// see also http://homeeasyhacking.wikia.com/wiki/Home_Easy_Hacking_Wiki
virtual char decode (word width) {
if (200 <= width && width < 1200) {
gotBit(width >= 600);
return 0;
}
if (width >= 5000 && pos >= 5) {
for (byte i = 0; i < 6; ++i)
gotBit(0);
alignTail(7); // keep last 56 bits
return 1;
}
return -1;
}
};
class ElroDecoder : public DecodeOOK {
public:
ElroDecoder () {}
virtual char decode (word width) {
if (50 <= width && width < 600) {
byte w = (width - 40) / 190; // 40 <= 0 < 230 <= 1 < 420 <= 2 < 610
switch (state) {
case UNKNOWN:
case OK:
if (w == 0)
state = T0;
else if (w == 2)
state = T2;
break;
case T0:
case T2:
if (w == 1)
++state;
else
return -1;
break;
case T1:
if (w == 0) { // sync pattern has 0-1-0-1 patterns
resetDecoder();
break;
}
if (w != 2)
return -1;
gotBit(0);
break;
case T3:
if (w != 0)
return -1;
gotBit(1);
break;
}
return 0;
}
if (pos >= 11)
return 1;
return -1;
}
};
// The following three decoders were contributed bij Gijs van Duimen:
// FlamingoDecoder = Flamingo FA15RF
// SmokeDecoder = Flamingo FA12RF
// ByronbellDecoder = Byron SX30T
// see http://www.domoticaforum.eu/viewtopic.php?f=17&t=4960&start=90#p51118
// there's some weirdness in this code, I've edited it a bit -jcw, 2011-10-16
class FlamingoDecoder : public DecodeOOK {
public:
FlamingoDecoder () {}
virtual char decode (word width) {
if ((width > 740 && width < 780) || (width > 2650 && width < 2750) ||
(width > 810 && width < 950) || (width > 1040 && width < 1450)) {
gotBit(width >= 950);
return 0;
}
// if (pos >= 4 && data[0] == 84 && data[1] == 85 &&
// data[2] == 85 && data[3] == 85)
if (pos >= 4)
return 1;
return -1;
}
};
class SmokeDecoder : public DecodeOOK {
public:
SmokeDecoder () {}
virtual char decode (word width) {
if ((width > 20000 && width < 21000) || (width > 6900 && width < 7000) ||
(width > 6500 && width < 6800)) {
gotBit(1);
// if (width > 3000 && width < 4000)
// byte w = width < 100;
return 0;
}
if (pos >= 4)
return pos = bits = 1;
return -1;
}
};
class ByronbellDecoder : public DecodeOOK {
public:
ByronbellDecoder () {}
virtual char decode (word width) {
if ((660 < width && width < 715) || (5100 < width && width < 5400)) {
gotBit(width > 1000);
return 0;
}
if (pos >= 8)
return pos = bits = 1;
return -1;
}
};
// 868 MHz decoders
class VisonicDecoder : public DecodeOOK {
public:
VisonicDecoder () {}
virtual char decode (word width) {
if (200 <= width && width < 1000) {
byte w = width >= 600;
switch (state) {
case UNKNOWN:
case OK:
state = w == 0 ? T0 : T1;
break;
case T0:
gotBit(!w);
if (w)
return 0;
break;
case T1:
gotBit(!w);
if (!w)
return 0;
break;
}
// sync error, flip all the preceding bits to resync
for (byte i = 0; i <= pos; ++i)
data[i] ^= 0xFF;
} else if (width >= 2500 && 8 * pos + bits >= 36 && state == OK) {
for (byte i = 0; i < 4; ++i)
gotBit(0);
alignTail(5); // keep last 40 bits
// only report valid packets
byte b = data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4];
if ((b & 0xF) == (b >> 4))
return 1;
} else
return -1;
return 0;
}
};
class EMxDecoder : public DecodeOOK {
public:
EMxDecoder () : DecodeOOK (30) {} // ignore packets repeated within 3 sec
// see also http://fhz4linux.info/tiki-index.php?page=EM+Protocol
virtual char decode (word width) {
if (200 <= width && width < 1000) {
byte w = width >= 600;
switch (state) {
case UNKNOWN:
if (w == 0)
++flip;
else if (flip > 20)
state = OK;
else
return -1;
break;
case OK:
if (w == 0)
state = T0;
else
return -1;
break;
case T0:
gotBit(w);
break;
}
} else if (width >= 1500 && pos >= 9)
return 1;
else
return -1;
return 0;
}
};
class KSxDecoder : public DecodeOOK {
public:
KSxDecoder () {}
// see also http://www.dc3yc.homepage.t-online.de/protocol.htm
virtual char decode (word width) {
if (200 <= width && width < 1000) {
byte w = width >= 600;
switch (state) {
case UNKNOWN:
gotBit(w);
bits = pos = 0;
if (data[0] != 0x95)
state = UNKNOWN;
break;
case OK:
state = w == 0 ? T0 : T1;
break;
case T0:
gotBit(1);
if (!w)
return -1;
break;
case T1:
gotBit(0);
if (w)
return -1;
break;
}
} else if (width >= 1500 && pos >= 6)
return 1;
else
return -1;
return 0;
}
};
class FSxDecoder : public DecodeOOK {
public:
FSxDecoder () {}
// see also http://fhz4linux.info/tiki-index.php?page=FS20%20Protocol
virtual char decode (word width) {
if (300 <= width && width < 775) {
byte w = width >= 500;
switch (state) {
case UNKNOWN:
if (w == 0)
++flip;
else if (flip > 20)
state = T1;
else
return -1;
break;
case OK:
state = w == 0 ? T0 : T1;
break;
case T0:
gotBit(0);
if (w)
return -1;
break;
case T1:
gotBit(1);
if (!w)
return -1;
break;
}
} else if (width >= 1500 && pos >= 5)
return 1;
else
return -1;
return 0;
}
};
// Dumb Arduino IDE pre-processing bug - can't put this in the main source file!
typedef struct {
char typecode;
const char* name;
DecodeOOK* decoder;
} DecoderInfo;